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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

long long d[50][5001];
long long C[50];
long long D[50];

vector<pair<pair<long long, long long>, int>> edges[50];

void dijikstra(int S, int N, long long max_A, long long S_coin) {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= max_A; j++) {
      d[i][j] = LLONG_MAX;
    }
  }

  priority_queue<pair<long long, pair<long long, int>>,
                 vector<pair<long long, pair<long long, int>>>,
                 greater<pair<long long, pair<long long, int>>>>
      pq;
  d[S][min(max_A, S_coin)] = 0;
  pq.push(make_pair(0, make_pair(min(max_A, S_coin), S)));
  while (!pq.empty()) {
    auto cur = pq.top();
    long long cur_cost = cur.first;
    int cur_node = cur.second.second;
    long long cur_coin_no = cur.second.first;
    pq.pop();
    // cout << cur_node << " " << cur_coin_no << endl;
    if (d[cur_node][cur_coin_no] < cur_cost) {
      continue;
    }

    long long next_coin_no;
    if (cur_coin_no + C[cur_node] <= max_A) {
      next_coin_no = cur_coin_no + C[cur_node];
    } else {
      next_coin_no = max_A;
    }
    long long next_cost = cur_cost + D[cur_node];
    if (d[cur_node][next_coin_no] > next_cost) {
      d[cur_node][next_coin_no] = next_cost;
      pq.push(make_pair(next_cost, make_pair(next_coin_no, cur_node)));
    }

    for (int i = 0; i < edges[cur_node].size(); i++) {
      auto next = edges[cur_node][i];
      auto next_node = next.second;
      auto cost = next.first.second;
      auto coin_cost = next.first.first;
      long long next_d = d[cur_node][cur_coin_no] + cost;
      long long next_coin_no = cur_coin_no - coin_cost;
      if (next_coin_no >= 0 && d[next_node][next_coin_no] > next_d) {
        d[next_node][next_coin_no] = next_d;
        pq.push(make_pair(next_d, make_pair(next_coin_no, next_node)));
      }
    }
  }
}
int main() {
  int N;
  int M;
  long long S;
  cin >> N >> M >> S;
  long long max_A = 0;
  for (int i = 0; i < M; i++) {
    long long U;
    long long V;
    long long A;
    long long B;
    cin >> U >> V >> A >> B;
    edges[U - 1].push_back(make_pair(make_pair(A, B), V - 1));
    edges[V - 1].push_back(make_pair(make_pair(A, B), U - 1));
    max_A += A;
  }

  for (int i = 0; i < N; i++) {
    cin >> C[i] >> D[i];
  }

  dijikstra(0, N, max_A, S);

  for (int i = 1; i < N; i++) {
    long long ans = LLONG_MAX;
    for (int j = 0; j <= max_A; j++) {
      ans = min(ans, d[i][j]);
    }
    cout << ans << endl;
  }

  /*
  for(int i = 0; i < N; i++){
    for(int j = 0; j <= max_A; j++){
      cout << d[i][j] << " ";
    }
    cout << endl;
  }
  */
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

long long d[50][2501];
long long C[50];
long long D[50];

vector<pair<pair<long long, long long>, int>> edges[50];

void dijikstra(int S, int N, long long max_A, long long S_coin) {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= max_A; j++) {
      d[i][j] = LLONG_MAX;
    }
  }

  priority_queue<pair<long long, pair<long long, int>>,
                 vector<pair<long long, pair<long long, int>>>,
                 greater<pair<long long, pair<long long, int>>>>
      pq;
  d[S][min(max_A, S_coin)] = 0;
  pq.push(make_pair(0, make_pair(min(max_A, S_coin), S)));
  while (!pq.empty()) {
    auto cur = pq.top();
    long long cur_cost = cur.first;
    int cur_node = cur.second.second;
    long long cur_coin_no = cur.second.first;
    pq.pop();
    // cout << cur_node << " " << cur_coin_no << endl;
    if (d[cur_node][cur_coin_no] < cur_cost) {
      continue;
    }

    long long next_coin_no;
    if (cur_coin_no + C[cur_node] <= max_A) {
      next_coin_no = cur_coin_no + C[cur_node];
    } else {
      next_coin_no = max_A;
    }
    long long next_cost = cur_cost + D[cur_node];
    if (d[cur_node][next_coin_no] > next_cost) {
      d[cur_node][next_coin_no] = next_cost;
      pq.push(make_pair(next_cost, make_pair(next_coin_no, cur_node)));
    }

    for (int i = 0; i < edges[cur_node].size(); i++) {
      auto next = edges[cur_node][i];
      auto next_node = next.second;
      auto cost = next.first.second;
      auto coin_cost = next.first.first;
      long long next_d = d[cur_node][cur_coin_no] + cost;
      long long next_coin_no = cur_coin_no - coin_cost;
      if (next_coin_no >= 0 && d[next_node][next_coin_no] > next_d) {
        d[next_node][next_coin_no] = next_d;
        pq.push(make_pair(next_d, make_pair(next_coin_no, next_node)));
      }
    }
  }
}
int main() {
  int N;
  int M;
  long long S;
  cin >> N >> M >> S;
  long long max_A = 0;
  for (int i = 0; i < M; i++) {
    long long U;
    long long V;
    long long A;
    long long B;
    cin >> U >> V >> A >> B;
    edges[U - 1].push_back(make_pair(make_pair(A, B), V - 1));
    edges[V - 1].push_back(make_pair(make_pair(A, B), U - 1));
    max_A += A;
  }

  for (int i = 0; i < N; i++) {
    cin >> C[i] >> D[i];
  }

  dijikstra(0, N, max_A, S);

  for (int i = 1; i < N; i++) {
    long long ans = LLONG_MAX;
    for (int j = 0; j <= max_A; j++) {
      ans = min(ans, d[i][j]);
    }
    cout << ans << endl;
  }

  /*
  for(int i = 0; i < N; i++){
    for(int j = 0; j <= max_A; j++){
      cout << d[i][j] << " ";
    }
    cout << endl;
  }
  */
  return 0;
}