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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
#define E exp(double(1))
#define maxn 51000
using namespace std;

struct Edge {
  int from, to, dis, next;
  int w;
} edge[maxn];

int b[maxn];
int head[maxn];
int vis[maxn];
int dis[maxn];
int tt[maxn];
int p[maxn];
int cnt;
int n;
int ll;
void add(int q, int h, int len, int w) {
  edge[cnt].from = q;
  edge[cnt].to = h;
  edge[cnt].dis = len;
  edge[cnt].w = w;
  edge[cnt].next = head[q];
  head[q] = cnt;
  cnt++;
}

vector<int> vct[maxn];

int Dijkstra() {
  int i, j, k, tmp, v;
  int flag;
  int yy;
  memset(dis, inf, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  memset(tt, 0, sizeof(tt));
  memset(p, 0, sizeof(p));
  dis[1] = 0;
  vis[1] = 1;
  int next;
  int s = 1;
  yy = 0;
  for (int i = 0; i < maxn; i++)
    vct[i].clear();
  while (1) {
    tmp = inf;
    next = -1;
    for (int i = head[s]; i != -1; i = edge[i].next) {
      v = edge[i].to;
      if (vis[v])
        continue;
      if (edge[i].dis + dis[s] < dis[v]) {
        dis[v] = edge[i].dis + dis[s];
        tt[v] = edge[i].w;
        p[v] = 1;
        vct[v].clear();
        vct[v].push_back(i);
      } else if (edge[i].dis + dis[s] == dis[v]) {
        tt[v] = max(tt[v], edge[i].w);
        p[v]++;
        vct[v].push_back(i);
      }
    }
    for (int i = 1; i <= n; i++) {
      if (vis[i])
        continue;
      ;
      if (dis[i] < tmp) {
        tmp = dis[i];
        next = i;
      }
    }
    if (next == -1)
      break;

    for (i = 0; i < vct[next].size(); i++) {
      //   printf("%d __>  %d\\n",edge[vct[next][i]].from,edge[vct[next][i]].to);
      if (b[vct[next][i]])
        continue;
      ll += edge[vct[next][i]].w;
      b[vct[next][i]] = 1;
    }
    vis[next] = 1;
    s = next;
    if (p[next] >= 2) {
      yy += tt[next];
    }
  }
  return yy;
}

int main() {
  int m;
  int q, h, len, w, i, j;
  int ans;
  int ff;
  while (scanf("%d%d", &n, &m) != EOF, n + m) {
    ll = 0;
    cnt = 0;
    memset(b, 0, sizeof(b));
    memset(head, -1, sizeof(head));
    for (i = 0; i < m; i++) {
      scanf("%d%d%d%d", &q, &h, &len, &w);
      add(q, h, len, w);
      add(h, q, len, w);
    }

    ff = Dijkstra();
    //   printf("%d\\n",ll);
    printf("%d\\n", ll - ff);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int maxn = 10005, maxm = 10005 * 2;
class ljlj {
public:
  int to, next, c, v;
} mp[maxm];
int tot;
int head[maxn];
void insert(int s, int t, int v, int c) {
  mp[++tot].to = t;
  mp[tot].next = head[s];
  mp[tot].v = v;
  mp[tot].c = c;
  head[s] = tot;
}
int dis[maxn];
bool inq[maxn];
queue<int> Q;
void spfa(int S) {
  memset(dis, 120, sizeof(dis));
  memset(inq, 0, sizeof(inq));
  while (!Q.empty())
    Q.pop();
  Q.push(S);
  dis[S] = 0;
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();
    inq[x] = 0;
    for (int i = head[x]; i != -1; i = mp[i].next)
      if (dis[mp[i].to] > dis[x] + mp[i].v) {
        dis[mp[i].to] = dis[x] + mp[i].v;
        if (!inq[mp[i].to]) {
          inq[mp[i].to] = 1;
          Q.push(mp[i].to);
        }
      }
  }
}
int dp[maxn], rd[maxn];
int main() {
  ios::sync_with_stdio(0);
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      return 0;
    int i;
    memset(head, -1, sizeof(head));
    tot = 0;
    for (i = 1; i <= m; i++) {
      int a, b, c, v;
      cin >> a >> b >> v >> c;
      insert(a, b, v, c);
      insert(b, a, v, c);
    }
    spfa(1);
    memset(rd, 0, sizeof(rd));
    for (i = 1; i <= n; i++)
      for (int j = head[i]; j != -1; j = mp[j].next)
        if (dis[i] + mp[j].v == dis[mp[j].to])
          rd[mp[j].to]++;
    memset(dp, 127, sizeof(dp));
    dp[1] = 0;
    while (!Q.empty())
      Q.pop();
    Q.push(1);
    while (!Q.empty()) {
      int x = Q.front();
      Q.pop();
      for (i = head[x]; i != -1; i = mp[i].next)
        if (dis[x] + mp[i].v == dis[mp[i].to] && dp[mp[i].to] > mp[i].c) {
          dp[mp[i].to] = mp[i].c;
          rd[mp[i].to]--;
          if (rd[mp[i].to] == 0)
            Q.push(mp[i].to);
        }
    }
    int ans = 0;
    for (i = 1; i <= n; i++)
      ans += dp[i];
    cout << ans << endl;
  }
  return 0;
}