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

#include <cstring>
#include <iostream>
#include <queue>
#define inf 0x3f3f3f3f
#define maxm 10005
#define maxn 40005

using namespace std;

int m, n;
long long edgenum, head[maxm], dis[maxm], num[maxm], ans;
bool vis[maxm];

struct node {
  int from, to, len, cost, next;
} edge[maxn];

void init() {
  memset(head, -1, sizeof(head));
  memset(vis, 0, sizeof(vis));
  memset(dis, inf, sizeof(dis));
  memset(num, inf, sizeof(num));
  edgenum = 0;
}

void add(int u, int v, int len, int cost) {
  node tp = {u, v, len, cost, head[u]};
  edge[edgenum] = tp;
  head[u] = edgenum++;
}

long long SPFA(int st) {
  queue<int> q;
  q.push(st);
  vis[st] = 1;
  dis[st] = 0;
  num[st] = 0;
  ans = 0;
  while (!q.empty()) {
    int tmp = q.front();
    q.pop();
    vis[tmp] = 0;
    for (int i = head[tmp]; i != -1; i = edge[i].next) {
      if ((dis[edge[i].to] > dis[tmp] + edge[i].len) ||
          ((dis[edge[i].to] == dis[tmp] + edge[i].len) &&
           (num[edge[i].to] > edge[i].cost))) {
        dis[edge[i].to] = dis[tmp] + edge[i].len;
        num[edge[i].to] = edge[i].cost;
        if (!vis[edge[i].to]) {
          q.push(edge[i].to);
          vis[edge[i].to] = 1;
        }
      }
    }
  }
  for (int i = 1; i <= m; i++)
    ans += num[i];
  return ans;
}

int main() {
  int u, v, len, cost;
  while (cin >> m >> n && m) {
    init();
    for (int i = 1; i <= n; i++) {
      cin >> u >> v >> len >> cost;
      add(u, v, len, cost);
      add(v, u, len, cost);
    }
    cout << SPFA(1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 10005;
bool used[maxn];
long long d[maxn];
int n, m;
int len;
struct Tree {
  int to;
  int from;
  int value;
  int next;
  int cost;

} tree[maxn * 2];
int head[maxn];
long long cost[maxn];
void add(int u, int v, int c, int d) {
  tree[len].from = u;
  tree[len].to = v;
  tree[len].value = c;
  tree[len].next = head[u];
  tree[len].cost = d;
  head[u] = len++;
}
void init() {
  memset(head, -1, sizeof(head));
  len = 0;
  memset(cost, 0x3f, sizeof(cost));
}
int spfa() {
  queue<int> q;
  // int sum=0;
  memset(d, 0x3f, sizeof(d));
  memset(used, false, sizeof(used));
  d[0] = 0;
  cost[0] = 0;
  while (!q.empty())
    q.pop();
  used[1] = true;
  q.push(1);
  d[1] = 0;
  cost[1] = 0;
  while (!q.empty()) { // ceshi=false;
    int u = q.front();
    used[u] = false;
    q.pop();
    for (int i = head[u]; i != -1; i = tree[i].next) {
      int s = tree[i].to;
      int w = tree[i].cost;
      if (d[s] > d[u] + tree[i].value) {
        if (!used[s]) {
          used[s] = true;
          q.push(s);
        }
        d[s] = d[u] + tree[i].value;
        cost[s] = w;

      } else if (d[s] == d[u] + tree[i].value) {
        if (cost[s] > w)
          cost[s] = w;
      }
    }
    // q.pop();
  }
  for (int i = 1; i <= n; i++)
    cost[0] += cost[i];
  printf("%lld\\n", cost[0]);
  return 0;
}

int main() {
  int a, b, c, d;
  while (~scanf("%d%d", &n, &m), m + n) {
    init();
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d%d", &a, &b, &c, &d);
      add(a, b, c, d);
      add(b, a, c, d);
    }
    spfa();
    // printf("%d\\n",spfa());
  }

  return 0;
}