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

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define GET_ARG(a, b, c, F, ...) F
#define REP3(i, s, e) for (i = s; i <= e; i++)
#define REP2(i, n) REP3(i, 0, (int)(n)-1)
#define REP(...) GET_ARG(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define RREP3(i, s, e) for (i = s; i >= e; i--)
#define RREP2(i, n) RREP3(i, (int)(n)-1, 0)
#define RREP(...) GET_ARG(__VA_ARGS__, RREP3, RREP2)(__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl

const int INF = 1e8;

vector<pair<int, pair<int, int>>> e[10000];
int dist[10000];

int main(void) {
  while (true) {
    int i, n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    REP(i, n) e[i].clear();
    REP(i, m) {
      int u, v, d, c;
      cin >> u >> v >> d >> c;
      u--;
      v--;
      e[u].push_back({v, {d, c}});
      e[v].push_back({u, {d, c}});
    }
    priority_queue<pair<int, int>> q;
    q.push({0, 0});
    REP(i, n) dist[i] = INF;
    dist[0] = 0;
    while (!q.empty()) {
      auto p = q.top();
      q.pop();
      int u = p.second;
      for (auto x : e[u]) {
        int v = x.first;
        int d = x.second.first;
        if (dist[v] > dist[u] + d) {
          dist[v] = dist[u] + d;
          q.push({-dist[v], v});
        }
      }
    }
    int ans = 0;
    REP(i, 1, n - 1) {
      int mn = INF;
      for (auto p : e[i]) {
        int v = p.first;
        int d = p.second.first;
        int c = p.second.second;
        if (dist[i] == dist[v] + d)
          mn = min(mn, c);
      }
      ans += mn;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define GET_ARG(a, b, c, F, ...) F
#define REP3(i, s, e) for (i = s; i <= e; i++)
#define REP2(i, n) REP3(i, 0, (int)(n)-1)
#define REP(...) GET_ARG(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define RREP3(i, s, e) for (i = s; i >= e; i--)
#define RREP2(i, n) RREP3(i, (int)(n)-1, 0)
#define RREP(...) GET_ARG(__VA_ARGS__, RREP3, RREP2)(__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl

const int INF = 1e8;

vector<pair<int, pair<int, int>>> e[10000];
int dist[1000];

int main(void) {
  while (true) {
    int i, n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    REP(i, n) e[i].clear();
    REP(i, m) {
      int u, v, d, c;
      cin >> u >> v >> d >> c;
      u--;
      v--;
      e[u].push_back({v, {d, c}});
      e[v].push_back({u, {d, c}});
    }
    priority_queue<pair<int, int>> q;
    q.push({0, 0});
    REP(i, n) dist[i] = INF;
    dist[0] = 0;
    while (!q.empty()) {
      auto p = q.top();
      q.pop();
      int u = p.second;
      for (auto x : e[u]) {
        int v = x.first;
        int d = x.second.first;
        if (dist[v] > dist[u] + d) {
          dist[v] = dist[u] + d;
          q.push({-dist[v], v});
        }
      }
    }
    int ans = 0;
    REP(i, 1, n - 1) {
      int mn = INF;
      for (auto p : e[i]) {
        int v = p.first;
        int d = p.second.first;
        int c = p.second.second;
        if (dist[i] == dist[v] + d)
          mn = min(mn, c);
      }
      ans += mn;
    }
    cout << ans << endl;
  }
  return 0;
}