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

// AOJ 2249 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2249)
#include <cstdio>
#include <cstring>
#include <limits>
#include <queue>
#include <set>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define clr(a, v) memset((a), (v), sizeof(a))

typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;

const int MAX_N = 10000, INF = std::numeric_limits<int>::max() >> 2;

int N, M;
std::vector<PP> G[MAX_N];
bool used[MAX_N];

int main() {
  while (scanf("%d%d", &N, &M), N | M) {
    rep(i, N) G[i].clear();

    clr(used, 0);

    rep(i, M) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      --u;
      --v;
      G[u].push_back(PP(P(d, c), v));
      G[v].push_back(PP(P(d, c), u));
    }

    std::priority_queue<PP> pque;
    std::vector<P> d(N, P(INF, INF));
    pque.push(PP(P(0, 0), 0));
    d[0] = P(0, 0);

    int ans = 0;

    while (!pque.empty()) {
      PP p = pque.top();
      pque.pop();
      int dist = -p.first.first, cost = -p.first.second, v = p.second;

      if (used[v])
        continue;

      used[v] = true;
      ans += cost;

      rep(i, G[v].size()) {
        PP &e = G[v][i];

        if (d[e.second] > P(dist + e.first.first, e.first.second)) {
          d[e.second] = P(dist + e.first.first, e.first.second);
          pque.push(PP(P(-d[e.second].first, -e.first.second), e.second));
        }
      }
    }

    printf("%d\\n", ans);
  }

  return 0;
}

Error: MLE

Fixed submission:

// AOJ 2249 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2249)
#include <cstdio>
#include <cstring>
#include <limits>
#include <queue>
#include <set>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define clr(a, v) memset((a), (v), sizeof(a))

typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;

const int MAX_N = 10000, INF = std::numeric_limits<int>::max() >> 2;

int N, M;
std::vector<PP> G[MAX_N];
int cost[MAX_N][MAX_N];
bool used[MAX_N];

int main() {
  while (scanf("%d%d", &N, &M), N | M) {
    rep(i, N) G[i].clear();

    clr(cost, 0);
    clr(used, 0);

    rep(i, M) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      --u;
      --v;
      G[u].push_back(PP(P(d, c), v));
      G[v].push_back(PP(P(d, c), u));
      cost[u][v] = cost[v][u] = c;
    }

    std::priority_queue<PP> pque;
    std::vector<P> d(N, P(INF, INF));
    pque.push(PP(P(0, 0), 0));
    d[0] = P(0, 0);

    int ans = 0;

    while (!pque.empty()) {
      PP p = pque.top();
      pque.pop();
      int dist = -p.first.first, cost = -p.first.second, v = p.second;

      if (used[v])
        continue;

      used[v] = true;
      ans += cost;

      rep(i, G[v].size()) {
        PP &e = G[v][i];

        if (d[e.second] > P(dist + e.first.first, e.first.second)) {
          d[e.second] = P(dist + e.first.first, e.first.second);
          pque.push(PP(P(-d[e.second].first, -e.first.second), e.second));
        }
      }
    }

    printf("%d\\n", ans);
  }

  return 0;
}