Problem p01366 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
// typedef pair<int, int> P;
struct edge {
  int num, d, c;
  bool operator<(const edge &x) const {
    if (d != x.d)
      return d > x.d;
    else
      return c > x.c;
  }
};
const int inf = 1e9;
const int maxn = 1e4 + 5;
pair<int, int> d[maxn];
std::vector<edge> G[maxn];

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  while (cin >> n >> m && n) {
    int u, v, w, c;
    // cout << n << ' ' << m << endl;
    for (int i = 1; i <= n; i++) {
      G[i].clear();
    }
    for (int i = 0; i < m; i++) {
      cin >> u >> v >> w >> c;
      G[u].push_back({v, w, c});
      G[v].push_back({u, w, c});
    }
    for (int i = 1; i <= n; i++) {
      d[i] = make_pair(inf, inf);
    }
    // int d[1] = 0;
    priority_queue<edge> q;
    q.push({1, 0, 0});
    int ans = 0;
    while (!q.empty()) {
      edge cur = q.top();
      q.pop();
      // cout << cur.d << endl;
      if (d[cur.num] < make_pair(cur.d, cur.c))
        continue;
      int k = cur.num;
      d[k] = make_pair(cur.d, cur.c);
      for (edge i : G[k]) {
        pair<int, int> temp = d[k];
        temp = make_pair(d[k].first + i.d, i.c);
        if (temp < d[i.num]) {
          d[i.num] = temp;
          q.push({i.num, d[i.num].first, i.c});
          // cout << d[k]+i.d << ' ';
        }
      }
      // cout << endl;
      ans += cur.c;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
// typedef pair<int, int> P;
struct edge {
  int num, d, c;
  bool operator<(const edge &x) const {
    if (d != x.d)
      return d > x.d;
    else
      return c > x.c;
  }
};
const int inf = 1e9;
const int maxn = 1e4 + 5;
pair<int, int> d[maxn];

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  while (cin >> n >> m && n) {
    int u, v, w, c;
    std::vector<edge> G[maxn];

    // cout << n << ' ' << m << endl;
    for (int i = 0; i < m; i++) {
      cin >> u >> v >> w >> c;
      G[u].push_back({v, w, c});
      G[v].push_back({u, w, c});
    }
    for (int i = 1; i <= n; i++) {
      d[i] = make_pair(inf, inf);
    }
    // int d[1] = 0;
    priority_queue<edge> q;
    q.push({1, 0, 0});
    int ans = 0;
    while (!q.empty()) {
      edge cur = q.top();
      q.pop();
      // cout << cur.d << endl;
      if (d[cur.num] < make_pair(cur.d, cur.c))
        continue;
      int k = cur.num;
      d[k] = make_pair(cur.d, cur.c);
      for (edge i : G[k]) {
        pair<int, int> temp = d[k];
        temp = make_pair(d[k].first + i.d, i.c);
        if (temp < d[i.num]) {
          d[i.num] = temp;
          q.push({i.num, d[i.num].first, i.c});
          // cout << d[k]+i.d << ' ';
        }
      }
      // cout << endl;
      ans += cur.c;
      for (int i = 1; i <= n; i++) {
        // cout << d[i] << ' ';
      }
      // cout << endl;
    }
    cout << ans << endl;
  }
  return 0;
}