Problem p01366 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
const int SIZE = 10010;
const int MAX = 0x3f3f3f3f;

typedef struct Road {
  int from;
  int to;
  int length;
  int cost;
  int next;
} Road;

int head[SIZE];
int dis[SIZE];
Road roads[SIZE * 5];
int front[SIZE];
bool visit[SIZE];

int city, road, capital;

void init() {
  memset(head, -1, sizeof(head));
  memset(front, -1, sizeof(front));
  memset(visit, false, sizeof(visit));
  for (int i = 0; i <= city; ++i) {
    dis[i] = MAX;
  }
  dis[1] = 0;
}

void dijkstra() {
  while (true) {
    int pos = -1;
    for (int i = 1; i <= city; ++i) {
      if (!visit[i] && (pos == -1 || dis[pos] > dis[i])) {
        pos = i;
      }
    }
    if (pos == -1) {
      break;
    }
    visit[pos] = true;
    for (int i = head[pos]; i != -1; i = roads[i].next) {
      int to = roads[i].to;
      if (dis[to] > dis[pos] + roads[i].length) {
        dis[to] = dis[pos] + roads[i].length;
        front[to] = i;
      } else if ((dis[to] == dis[pos] + roads[i].length) && front[to] != -1 &&
                 roads[i].cost < roads[front[to]].cost) {
        front[to] = i;
      }
    }
  }
}

int main() {
  while (true) {
    std::cin >> city >> road;
    if (city == 0 && road == 0) {
      break;
    }
    init();
    int en = 0;
    for (int i = 0; i < road; ++i) {
      std::cin >> roads[en].from >> roads[en].to >> roads[en].length >>
          roads[en].cost;
      roads[en].next = head[roads[en].from];
      head[roads[en].from] = en;
      en++;
      roads[en].from = roads[en - 1].to;
      roads[en].to = roads[en - 1].from;
      roads[en].length = roads[en - 1].length;
      roads[en].cost = roads[en - 1].cost;
      roads[en].next = head[roads[en].from];
      head[roads[en].from] = en;
      en++;
    }
    dijkstra();
    int result = 0;
    for (int i = 1; i <= city; ++i) {
      if (front[i] != -1) {
        result += roads[front[i]].cost;
      }
    }
    std::cout << result << std::endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>

using namespace std;

const int SIZE = 10010;
const int MAX = 1000000;

typedef struct Road {
  int length;
  int cost;
} Road;

Road map[SIZE][SIZE];
int mcost[SIZE];
int dis[SIZE];
int father[SIZE];
bool visit[SIZE];
int cost[SIZE];

int city, road, capital;

int min(int a, int b) { return a < b ? a : b; }

int max(int a, int b) { return a > b ? a : b; }

void dijkstra() {
  for (int i = 1; i <= city; ++i) {
    if (dis[i] > map[capital][i].length) {
      father[i] = capital;
      dis[i] = map[capital][i].length;
      cost[i] = map[capital][i].cost;
      mcost[i] = cost[i];
    }
  }
  visit[capital] = true;
  while (true) {
    int pos = -1;
    int mind = MAX + 1;
    int minc = MAX + 1;
    for (int i = 1; i <= city; ++i) {
      if (visit[i]) {
        continue;
      }
      if (dis[i] < mind) {
        pos = i;
        mind = dis[i];
        minc = mcost[i];
        continue;
      }
      if (dis[i] == mind && mcost[i] < minc) {
        pos = i;
        minc = mcost[i];
      }
    }
    if (pos == -1) {
      break;
    }
    visit[pos] = true;
    for (int i = 1; i <= city; ++i) {
      if (visit[i]) {
        continue;
      }
      int tmp = dis[i] - dis[pos] - map[pos][i].length;
      if (tmp > 0) {
        dis[i] = dis[pos] + map[pos][i].length;
        cost[i] = map[pos][i].cost;
        mcost[i] = mcost[pos] + cost[i];
        father[i] = pos;
        continue;
      }
      if (tmp == 0 && mcost[i] > map[pos][i].cost) {
        cost[i] = map[pos][i].cost;
        mcost[i] = mcost[pos] + cost[i];
        father[i] = pos;
      }
    }
  }
}

int main() {
  int start, end, length, scost;
  while (true) {
    cin >> city >> road;
    if (city == 0 && road == 0) {
      break;
    }
    for (int i = 1; i <= city; ++i) {
      for (int j = 0; j < i; ++j) {
        map[i][j].length = map[j][i].length = MAX;
        map[i][j].cost = map[j][i].cost = MAX;
      }
      map[i][i].length = 0;
      map[i][i].cost = 0;
      dis[i] = MAX;
      father[i] = -1;
      cost[i] = MAX;
      mcost[i] = MAX;
      visit[i] = false;
    }
    for (int i = 0; i < road; ++i) {
      cin >> start >> end >> length >> scost;
      map[end][start].length = map[start][end].length = length;
      map[end][start].cost = map[start][end].cost = scost;
    }
    capital = 1;
    dijkstra();
    int sum = 0;
    for (int i = 1; i <= city; ++i) {
      sum += (cost[i] == MAX ? 0 : cost[i]);
    }
    cout << sum << endl;
  }
  return 0;
}