Problem p02703 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define INF 1e+15
#define MAX_V 1000000

struct edge {
  long long to;
  long long cost;
};

// <最短距離, 頂点の番号>
using P = pair<long long, long long>;

long long V;
vector<edge> G[MAX_V];
long long d[MAX_V];

void dijkstra(long long s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    long long v = p.second;
    if (d[v] < p.first)
      continue;
    // if prim,
    // d[v] = -d[v];
    // for keep tree
    for (long long i = 0; i < (long long)G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  long long N;
  cin >> N;
  V = 1000000;
  long long E;
  cin >> E;
  long long S;
  cin >> S;
  long long U, V, A, B;
  for (long long i = 0; i < E; ++i) {
    cin >> U >> V >> A >> B;
    for (long long j = 5000; j >= A; j--) {
      edge e = {10000 * V + j - A, B};
      G[10000 * U + j].push_back(e);
      edge e2 = {10000 * U + j - A, B};
      G[10000 * V + j].push_back(e2);
    }
  }
  long long C, D;
  for (long long i = 1; i <= N; i++) {
    cin >> C >> D;
    for (long long j = 0; j <= 5000; j++) {
      if (j + C <= 5000) {
        edge e3 = {10000 * i + j + C, D};
        G[10000 * i + j].push_back(e3);
      } else {
        edge e4 = {10000 * i + 5000, D};
        G[10000 * i + j].push_back(e4);
      }
    }
  }
  dijkstra(10000 + S);
  long long ans[51];
  fill(ans, ans + 51, INF);
  for (long long i = 2; i <= N; i++) {
    for (long long j = 0; j <= 5000; j++) {
      ans[i] = min(ans[i], d[10000 * i + j]);
    }
  }
  for (long long i = 2; i <= N; i++) {
    cout << ans[i] << endl;
  }
  system("pause");
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define INF 1e+15
#define MAX_V 1000000

struct edge {
  long long to;
  long long cost;
};

// <最短距離, 頂点の番号>
using P = pair<long long, long long>;

long long V;
vector<edge> G[MAX_V];
long long d[MAX_V];

void dijkstra(long long s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    long long v = p.second;
    if (d[v] < p.first)
      continue;
    // if prim,
    // d[v] = -d[v];
    // for keep tree
    for (long long i = 0; i < (long long)G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  long long N;
  cin >> N;
  V = 1000000;
  long long E;
  cin >> E;
  long long S;
  cin >> S;
  long long U, V, A, B;
  for (long long i = 0; i < E; ++i) {
    cin >> U >> V >> A >> B;
    for (long long j = 5000; j >= A; j--) {
      edge e = {10000 * V + j - A, B};
      G[10000 * U + j].push_back(e);
      edge e2 = {10000 * U + j - A, B};
      G[10000 * V + j].push_back(e2);
    }
  }
  long long C, D;
  for (long long i = 1; i <= N; i++) {
    cin >> C >> D;
    for (long long j = 0; j <= 5000; j++) {
      if (j + C <= 5000) {
        edge e3 = {10000 * i + j + C, D};
        G[10000 * i + j].push_back(e3);
      } else {
        edge e4 = {10000 * i + 5000, D};
        G[10000 * i + j].push_back(e4);
      }
    }
  }
  if (S > 5000) {
    S = 5000;
  }
  dijkstra(10000 + S);
  long long ans[51];
  fill(ans, ans + 51, INF);
  for (long long i = 2; i <= N; i++) {
    for (long long j = 0; j <= 5000; j++) {
      ans[i] = min(ans[i], d[10000 * i + j]);
    }
  }
  for (long long i = 2; i <= N; i++) {
    cout << ans[i] << endl;
  }
  system("pause");
  return 0;
}