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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 10050;
const int maxe = 40414;
const int INF = 0x3f3f3f3f;
struct note {
  int v;
  int d;
  int c;
  int next;
};
note edge[maxe];
int head[maxn];
int cnt;
void init() {
  memset(head, -1, sizeof(head));
  cnt = 0;
}
void addedge(int u, int v, int d, int c) {
  edge[cnt].v = v;
  edge[cnt].c = c;
  edge[cnt].d = d;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
int cost[maxn];
int dis[maxn];
bool vis[maxn];
queue<int> q;
void spfa(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[1] = 0;
  cost[1] = 0;
  vis[1] = true;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (int i = head[u]; i != -1; i = edge[i].next) {
      int v = edge[i].v;
      int val = edge[i].d;
      if (dis[v] > dis[u] + val ||
          (dis[v] == dis[u] + val &&
           cost[v] > edge[i].c)) // 有多条最短路时，取花费最小的
      {
        dis[v] = dis[u] + val;
        cost[v] = edge[i].c;
        if (!vis[v]) {
          q.push(v);
          vis[v] = true;
        }
      }
    }
  }
}
int main() {
  int n, m;
  int u, v, d, 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, &d);
      addedge(u, v, c, d);
      addedge(v, u, c, d);
    }
    spfa(n);
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += cost[i];
    printf("%d\\n", ans);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 10050;
const int maxe = 40414;
const int inf = 0x3f3f3f3f;
struct note {
  int v;
  int d;
  int c;
  int next;
};
note edge[maxe];
int head[maxn];
int cnt;
void init() {
  memset(head, -1, sizeof(head));
  cnt = 0;
}
void addedge(int u, int v, int d, int c) {
  edge[cnt].v = v;
  edge[cnt].c = c;
  edge[cnt].d = d;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
int cost[maxn];
int dis[maxn];
bool vis[maxn];
int op, tp, link[maxn];
void spfa(int n) {
  int i, u, v, d, c;
  memset(vis, 0, sizeof(vis));
  memset(dis, inf, sizeof(dis));
  memset(cost, inf, sizeof(cost));
  op = tp = 0;
  vis[1] = 1, dis[1] = 0, cost[1] = 0;
  link[tp++] = 1;
  while (op < tp) {
    u = link[op++];
    vis[u] = 0;
    for (i = head[u]; ~i; i = edge[i].next) {
      v = edge[i].v;
      d = edge[i].d;
      c = edge[i].c;
      if (dis[v] > dis[u] + d || (dis[v] == dis[u] + d && cost[v] > c)) {
        dis[v] = dis[u] + d;
        cost[v] = c;
        if (!vis[v]) {
          vis[v] = 1;
          link[tp++] = v;
        }
      }
    }
  }
}
int main() {
  int n, m;
  int u, v, d, 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, &d);
      addedge(u, v, c, d);
      addedge(v, u, c, d);
    }
    spfa(n);
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += cost[i];
    printf("%d\\n", ans);
  }
  return 0;
}