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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
using namespace std;
const int mod = 1e9 + 7;
const int N = 100000 + 5;
const int M = N * 2;
int n, m;
typedef pair<int, int> pii;
struct Edge {
  int v, d, c;
  int next;
} edge[M];
int head[N], d[N], tot;
int cost[N];
int vis[N];
void addedge(int u, int v, int d, int c) {
  edge[tot].v = v;
  edge[tot].d = d;
  edge[tot].c = c;
  edge[tot].next = head[u];
  head[u] = tot++;
}
void dij(int s) {
  priority_queue<pii, vector<pii>, greater<pii>> Q;
  for (int i = 1; i <= n; i++)
    d[i] = 10000000 + 5;
  d[s] = 0;
  cost[s] = 0;
  memset(vis, 0, sizeof(vis));
  Q.push(make_pair(d[s], s));
  while (!Q.empty()) {
    pii tmp = Q.top();
    Q.pop();
    int x = tmp.second;
    if (vis[x])
      continue;
    vis[x] = true;
    for (int i = head[x]; i + 1; i = edge[i].next) {
      if (d[edge[i].v] > d[x] + edge[i].d) {
        d[edge[i].v] = d[x] + edge[i].d;
        cost[edge[i].v] = edge[i].c;
        Q.push(make_pair(d[edge[i].v], edge[i].v));
      }
      if (d[edge[i].v] == (d[x] + edge[i].d)) {
        cost[edge[i].v] = min(cost[edge[i].v], edge[i].c);
      }
    }
  }
}

int main() {
  while (scanf("%d%d", &n, &m)) {
    if (m == 0 && n == 0)
      break;
    memset(head, -1, sizeof(head));
    tot = 1;
    for (int i = 0; i < m; i++) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      addedge(u, v, d, c);
      addedge(v, u, d, c);
    }
    dij(1);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      ans += cost[i];
    }
    cout << ans << endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef pair<int, int> pii;
const double PI = acos(-1.0);
const int maxn = 1e4 + 5;
int l[maxn][maxn], c[maxn][maxn];
int main() {
  int n, m;
  while (cin >> n >> m && (n || m)) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        l[i][j] = c[i][j] = INF;
    for (int i = 0; i < m; i++) {
      int x, y, z, zz;
      cin >> x >> y >> z >> zz;
      l[x][y] = l[y][x] = z;
      c[x][y] = c[y][x] = zz;
      l[x][x] = l[y][y] = c[x][x] = c[y][y] = 0;
    }
    int cc[maxn], d[maxn], f[maxn];
    bool visit[maxn];
    memset(visit, false, sizeof(visit));
    memset(cc, INF, sizeof(cc));
    memset(d, INF, sizeof(d));
    for (int i = 1; i <= n; i++)
      d[i] = l[1][i];
    for (int i = 1; i <= n; i++)
      cc[i] = c[1][i];
    visit[1] = 1;
    for (int i = 1; i <= n; i++) {
      int minn = INF, index;
      for (int j = 1; j <= n; j++)
        if (!visit[j] && minn > d[j]) {
          minn = d[j];
          index = j;
        }
      visit[index] = 1;
      for (int j = 1; j <= n; j++) {
        if (!visit[j] && d[j] > d[index] + l[index][j]) {
          d[j] = d[index] + l[index][j];
          cc[j] = c[index][j];
        }
        if (!visit[j] && d[j] == d[index] + l[index][j])
          cc[j] = min(cc[j], c[index][j]);
      }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += cc[i];
    cout << ans << endl;
  }
}