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

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

struct edge {
  int to, d, c;
};
bool operator<(const edge &a, const edge &b) { return a.to < b.to; }

struct node {
  int d, from, cost;
};
bool operator<(const node &a, const node &b) {
  return a.d != b.d ? a.d > b.d : a.cost > b.cost;
}

int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) != EOF) {
    if (n == 0 && m == 0)
      break;

    // read map
    map<int, set<edge>> graph;
    for (int i = 0, u, v, d, c; i < m; i++) {
      scanf("%d%d%d%d", &u, &v, &d, &c);
      if (graph.count(u) == 0)
        graph[u] = set<edge>();
      graph[u].insert(edge{v, d, c});
      if (graph.count(v) == 0)
        graph[v] = set<edge>();
      graph[v].insert(edge{u, d, c});
    }

    // recod the mininum path
    const int INF = 10000000;
    vector<int> d(n + 1, INF);
    vector<int> c(n + 1, 0);
    int result = 0;
    priority_queue<node> que;

    d[1] = c[1] = 0;
    que.push(node{0, 1, 0});
    while (!que.empty()) {
      int from = que.top().from;
      que.pop();
      if (graph[from].empty())
        continue;
      for (auto &i : graph[from]) {
        if (d[i.to] > d[from] + i.d) {
          d[i.to] = d[from] + i.d;
          result += i.c - c[i.to];
          c[i.to] = i.c;
          que.push(node{d[i.to], i.to, c[i.to]});
        } else if (d[i.to] == d[from] + i.d) {
          if (c[i.to] > i.c) {
            result += i.c - c[i.to];
            c[i.to] = i.c;
            que.push(node{d[i.to], i.to, c[i.to]});
          }
        }
      }
    }
#ifdef NDEBUG
    for (int i = 1; i <= n; i++)
      cout << c[i] << ' ';
    cout << endl;
#endif
    cout << result << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 1005;
const int maxe = 20050;
const int INF = 1e9;
struct note {
  int to;
  int w;
  int c;
  int next;
};
note edge[maxe];
int head[maxn];
int ip;
void init() {
  memset(head, -1, sizeof(head));
  ip = 0;
}
void addedge(int u, int v, int w, int c) {
  edge[ip].to = v;
  edge[ip].c = c;
  edge[ip].w = w;
  edge[ip].next = head[u];
  head[u] = ip++;
}
int cost[maxn];
int dis[maxn];
bool vis[maxn];
queue<int> q;
void spfa(int s, int n) {
  while (!q.empty())
    q.pop();
  for (int i = 1; i <= n; i++)
    cost[i] = dis[i] = INF;
  memset(vis, false, sizeof(vis));
  dis[s] = 0;
  cost[s] = 0;
  vis[s] = true;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (int i = head[u]; i != -1; i = edge[i].next) {
      int to = edge[i].to;
      int val = edge[i].w;
      if (dis[to] > dis[u] + val ||
          (dis[to] == dis[u] + val &&
           cost[to] > edge[i].c)) // 有多条最短路?，取花?最小的
      {
        dis[to] = dis[u] + val;
        cost[to] = edge[i].c;
        if (!vis[to]) {
          q.push(to);
          vis[to] = true;
        }
      }
    }
  }
}
int main() {
  // freopen("cin.txt","r",stdin);
  int n, m;
  int u, v, w, c;
  while (~scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;
    init();
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d%d", &u, &v, &c, &w);
      addedge(u, v, c, w);
      addedge(v, u, c, w);
    }
    spfa(1, n);
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += cost[i];
    printf("%d\\n", ans);
  }
  return 0;
}