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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

struct Edge {
  int to, d, c, next;
} edge[200005];

struct Node {
  int u, d;
  bool operator<(const Node &A) const { return d > A.d; }
} node[200005];
int vis[200005], use[200005];

int head[200004];

int dis[200005];
int to[100005];
priority_queue<Node> Q;
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m) && n + m) {
    while (!Q.empty())
      Q.pop();
    memset(vis, 0, sizeof(vis));
    memset(use, 0, sizeof(use));
    memset(dis, 0x3f, sizeof(dis));
    memset(head, -1, sizeof(head));
    memset(to, 0, sizeof(to));
    dis[1] = 0;

    int cnt = 1;
    int u, v, d, c;
    for (int i = 1; i <= m; ++i) {
      scanf("%d%d%d%d", &u, &v, &d, &c);
      edge[cnt].to = v, edge[cnt].d = d, edge[cnt].c = c,
      edge[cnt].next = head[u], head[u] = cnt++;
      edge[cnt].to = u, edge[cnt].d = d, edge[cnt].c = c,
      edge[cnt].next = head[v], head[v] = cnt++;
    }

    int tot = 0;
    Node A;
    A.d = 0, A.u = 1;
    Q.push(A);

    while (tot < n) {
      A = Q.top();
      int u = A.u;
      if (!vis[u]) {
        // cout << u << ' ' << dis[u] << endl;
        tot++;
        vis[u] = 1;
        for (int i = head[u]; i != -1; i = edge[i].next) {
          int v = edge[i].to;
          if (dis[v] > dis[u] + edge[i].d) {
            use[to[v]] = 0;
            dis[v] = edge[i].d + dis[u];
            to[v] = i;
            use[i] = 1;
            if (!vis[v]) {
              Node tmp;
              tmp.u = v, tmp.d = dis[v];
              Q.push(tmp);
            }
          } else if (dis[v] == dis[u] + edge[i].d) {
            if (edge[to[v]].c > edge[i].c) {
              use[to[v]] = 0;
              to[v] = i;
              use[i] = 1;
            }
          }
        }
      }
      Q.pop();
    }
    int ans = 0;
    for (int i = 1; i < cnt; ++i) {
      if (use[i]) {
        ans += edge[i].c;
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

struct Edge {
  int to, d, c, next;
} edge[20005];

struct Node {
  int u, d;
  bool operator<(const Node &A) const { return d > A.d; }
} node[10005];
int vis[10005], use[20005];

int head[10004];

int dis[10005];
int to[100005];
priority_queue<Node> Q;
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m) && n + m) {
    while (!Q.empty())
      Q.pop();
    memset(vis, 0, sizeof(vis));
    memset(use, 0, sizeof(use));
    memset(dis, 0x3f, sizeof(dis));
    memset(head, -1, sizeof(head));
    memset(to, 0, sizeof(to));
    dis[1] = 0;

    int cnt = 1;
    int u, v, d, c;
    for (int i = 1; i <= m; ++i) {
      scanf("%d%d%d%d", &u, &v, &d, &c);
      edge[cnt].to = v, edge[cnt].d = d, edge[cnt].c = c,
      edge[cnt].next = head[u], head[u] = cnt++;
      edge[cnt].to = u, edge[cnt].d = d, edge[cnt].c = c,
      edge[cnt].next = head[v], head[v] = cnt++;
    }

    int tot = 0;
    Node A;
    A.d = 0, A.u = 1;
    Q.push(A);

    while (tot < n) {
      A = Q.top();
      int u = A.u;
      if (!vis[u]) {
        // cout << u << ' ' << dis[u] << endl;
        tot++;
        vis[u] = 1;
        for (int i = head[u]; i != -1; i = edge[i].next) {
          int v = edge[i].to;
          if (dis[v] > dis[u] + edge[i].d) {
            use[to[v]] = 0;
            dis[v] = edge[i].d + dis[u];
            to[v] = i;
            use[i] = 1;
            if (!vis[v]) {
              Node tmp;
              tmp.u = v, tmp.d = dis[v];
              Q.push(tmp);
            }
          } else if (dis[v] == dis[u] + edge[i].d) {
            if (edge[to[v]].c > edge[i].c) {
              use[to[v]] = 0;
              to[v] = i;
              use[i] = 1;
            }
          }
        }
      }
      Q.pop();
    }
    int ans = 0;
    for (int i = 1; i < cnt; ++i) {
      if (use[i]) {
        ans += edge[i].c;
      }
    }
    cout << ans << endl;
  }
  return 0;
}