Problem p03305 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 10005, MAXE = 10005;

int n, m;

struct Edge {
  int x, v1, v2;
  Edge *next;
  Edge() : next(NULL) {}
  Edge(int a, int b, int c) : x(a), v1(b), v2(c), next(NULL) {}
} edge[MAXE * 2], *ecnt = edge, *adj[MAXN];

void AddEdge(int u, int v, int a, int b) {
  *ecnt = Edge(v, a, b);
  ecnt->next = adj[u];
  adj[u] = ecnt++;
}

typedef pair<int, int> Po;
priority_queue<Po, vector<Po>, greater<Po>> Q;
long long dis1[MAXN], dis2[MAXN];

void Dijkstra1(int S) {
  memset(dis1, 0x3F, sizeof dis1);
  dis1[S] = 0;
  Q.push(make_pair(0, S));
  while (!Q.empty()) {
    Po t = Q.top();
    Q.pop();
    if (dis1[t.second] < t.first)
      continue;
    for (Edge *p = adj[t.second]; p; p = p->next)
      if (dis1[p->x] > dis1[t.second] + p->v1) {
        dis1[p->x] = dis1[t.second] + p->v1;
        Q.push(make_pair(dis1[p->x], p->x));
      }
  }
}
void Dijkstra2(int S) {
  memset(dis2, 0x3F, sizeof dis2);
  dis2[S] = 0;
  Q.push(make_pair(0, S));
  while (!Q.empty()) {
    Po t = Q.top();
    Q.pop();
    if (dis2[t.second] < t.first)
      continue;
    for (Edge *p = adj[t.second]; p; p = p->next)
      if (dis2[p->x] > dis2[t.second] + p->v2) {
        dis2[p->x] = dis2[t.second] + p->v2;
        Q.push(make_pair(dis2[p->x], p->x));
      }
  }
}

pair<long long, int> ans[MAXN];

int main() {
  int S, T;
  scanf("%d%d%d%d", &n, &m, &S, &T);
  for (int q = 1, u, v, a, b; q <= m; q++) {
    scanf("%d%d%d%d", &u, &v, &a, &b);
    AddEdge(u, v, a, b);
    AddEdge(v, u, a, b);
  }

  Dijkstra1(S);
  Dijkstra2(T);

  for (int i = 1; i <= n; i++)
    ans[i] = make_pair(dis1[i] + dis2[i], i);
  sort(ans + 1, ans + n + 1);
  int k = 1;

  for (int i = 0; i < n; i++) {
    while (ans[k].second <= i)
      k++;
    printf("%lld\n", 1000000000000000LL - ans[k].first);
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 100005, MAXE = 100005;

int n, m;

struct Edge {
  int x, v1, v2;
  Edge *next;
  Edge() : next(NULL) {}
  Edge(int a, int b, int c) : x(a), v1(b), v2(c), next(NULL) {}
} edge[MAXE * 2], *ecnt = edge, *adj[MAXN];

void AddEdge(int u, int v, int a, int b) {
  *ecnt = Edge(v, a, b);
  ecnt->next = adj[u];
  adj[u] = ecnt++;
}

typedef pair<int, int> Po;
priority_queue<Po, vector<Po>, greater<Po>> Q;
long long dis1[MAXN], dis2[MAXN];

void Dijkstra1(int S) {
  memset(dis1, 0x3F, sizeof dis1);
  dis1[S] = 0;
  Q.push(make_pair(0, S));
  while (!Q.empty()) {
    Po t = Q.top();
    Q.pop();
    if (dis1[t.second] < t.first)
      continue;
    for (Edge *p = adj[t.second]; p; p = p->next)
      if (dis1[p->x] > dis1[t.second] + p->v1) {
        dis1[p->x] = dis1[t.second] + p->v1;
        Q.push(make_pair(dis1[p->x], p->x));
      }
  }
}
void Dijkstra2(int S) {
  memset(dis2, 0x3F, sizeof dis2);
  dis2[S] = 0;
  Q.push(make_pair(0, S));
  while (!Q.empty()) {
    Po t = Q.top();
    Q.pop();
    if (dis2[t.second] < t.first)
      continue;
    for (Edge *p = adj[t.second]; p; p = p->next)
      if (dis2[p->x] > dis2[t.second] + p->v2) {
        dis2[p->x] = dis2[t.second] + p->v2;
        Q.push(make_pair(dis2[p->x], p->x));
      }
  }
}

pair<long long, int> ans[MAXN];

int main() {
  int S, T;
  scanf("%d%d%d%d", &n, &m, &S, &T);
  for (int q = 1, u, v, a, b; q <= m; q++) {
    scanf("%d%d%d%d", &u, &v, &a, &b);
    AddEdge(u, v, a, b);
    AddEdge(v, u, a, b);
  }

  Dijkstra1(S);
  Dijkstra2(T);

  for (int i = 1; i <= n; i++)
    ans[i] = make_pair(dis1[i] + dis2[i], i);
  sort(ans + 1, ans + n + 1);
  int k = 1;

  for (int i = 0; i < n; i++) {
    while (ans[k].second <= i)
      k++;
    printf("%lld\n", 1000000000000000LL - ans[k].first);
  }

  return 0;
}