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

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> T;
struct edge {
  int to, dist, cost;
};
const int MAX_N = 10005;
const int MAX_D = 1005;
const int INF = 2 * MAX_N * MAX_D;
int N, M; // N=都市の数,M=当初の道路の数
// int dist[MAX_N][MAX_N];
// int cost[MAX_N][MAX_N];
vector<edge> G[MAX_N];
int addi[MAX_N];
int d[MAX_N];

bool exist(int v, int pv) {
  bool ret = false;
  for (int i = 0; i < (int)G[pv].size(); i++) {
    edge e = G[pv][i];
    if (e.to == v) {
      ret = true;
      break;
    }
  }
  return ret;
}
int findCost(int v, int pv) {
  int ret = INF;
  for (int i = 0; i < (int)G[pv].size(); i++) {
    edge e = G[pv][i];
    if (e.to == v) {
      ret = e.cost;
      break;
    }
  }
  return ret;
}

int findDist(int v, int pv) {
  int ret = INF;
  for (int i = 0; i < (int)G[pv].size(); i++) {
    edge e = G[pv][i];
    if (e.to == v) {
      ret = e.dist;
    }
  }
  return ret;
}

int dijkstra(int s) { // 1~各地点間の最短の道路建設に必要な追加費用
  fill(d, d + N + 1, INF);
  fill(addi, addi + N + 1, INF);
  d[s] = 0;
  addi[s] = 0;
  priority_queue<T, vector<T>, greater<T>> pque;

  pque.push(T(0, P(s, s))); // 距離、現在地、前の点
  while (!pque.empty()) {
    T t = pque.top();
    pque.pop();
    int v = t.second.first;
    if (d[v] < t.first)
      continue;
    int pv = t.second.second;
    // 最短経路が複数あった場合は、追加のコストが小さい方を選ぶ
    if (addi[v] != 0 && findCost(v, pv) < addi[v]) {
      addi[v] = findCost(v, pv);
    }
    for (int i = 0; i < (int)G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] >= t.first + e.dist) { // 最短距離の更新
        d[e.to] = t.first + e.dist;
        pque.push(T(d[e.to], P(e.to, v)));
      }
    }
  }
  int total = 0;
  for (int i = 2; i <= N; i++) {
    total += addi[i];
  }
  return total;
}

int main(int argc, const char *argv[]) {
  while (true) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;

    for (int i = 0; i < M; i++) {
      int u, v, d, c;
      scanf("%d %d %d %d", &u, &v, &d, &c);
      edge e1, e2;
      e1.to = v;
      e2.to = u;
      e1.dist = e2.dist = d;
      e1.cost = e2.cost = c;
      G[u].push_back(e1);
      G[v].push_back(e2);
      // dist[u][v] = dist[v][u] = d;
      // cost[u][v] = cost[v][u] = c;
    }
    int ret = 0;
    ret = dijkstra(1);
    printf("%d\\n", ret);
    for (int i = 1; i <= N; i++) {
      G[i].clear();
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> T;

const int MAX_N = 10005;
const int MAX_D = 1005;
const int INF = 2 * MAX_N * MAX_D;
int N, M; // N=都市の数,M=当初の道路の数
int dist[MAX_N][MAX_N];
int cost[MAX_N][MAX_N];
int addi[MAX_N];
int d[MAX_N];
int dijkstra(int s) { // 1~各地点間の最短の道路建設に必要な追加費用
  fill(d, d + N + 1, INF);
  fill(addi, addi + N + 1, INF);
  d[s] = 0;
  addi[s] = 0;
  priority_queue<T, vector<T>, greater<T>> pque;

  pque.push(T(0, P(s, s))); // 距離、現在地、前の点
  while (!pque.empty()) {
    T t = pque.top();
    pque.pop();
    int v = t.second.first;
    if (d[v] < t.first)
      continue;
    int pv = t.second.second;
    // 最短経路が複数あった場合は、追加のコストが小さい方を選ぶ
    if (cost[pv][v] < addi[v]) {
      addi[v] = cost[pv][v];
    }
    for (int i = 1; i <= N; i++) {
      if (i != v && dist[v][i] != INF) {
        if (d[i] >= t.first + dist[v][i]) { // 最短距離の更新
          d[i] = t.first + dist[v][i];
          pque.push(T(d[i], P(i, v)));
        }
      }
    }
  }
  int total = 0;
  for (int i = 2; i <= N; i++) {
    total += addi[i];
  }
  return total;
}

int main(int argc, const char *argv[]) {
  while (true) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        if (i == j)
          dist[i][j] = cost[i][j] = 0;
        else
          dist[i][j] = cost[i][j] = INF;
      }
    }
    for (int i = 0; i < M; i++) {
      int u, v, d, c;
      scanf("%d %d %d %d", &u, &v, &d, &c);
      dist[u][v] = dist[v][u] = d;
      cost[u][v] = cost[v][u] = c;
    }
    int ret = 0;
    ret = dijkstra(1);
    printf("%d\\n", ret);
  }
  return 0;
}