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

/*
    Code written by Talant I.D.
*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2 * acos(0.0)
#define sz(v) int(v.size())
#define endl "\\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define do_not_disturb                                                         \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);

inline bool isvowel(char ch) {
  ch = tolower(ch);
  return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}

inline bool isprime(int n) {
  if (n < 2 || (n % 2 == 0 && n != 2))
    return false;
  for (int i = 3; i * i <= n; i += 2)
    if (n % i == 0)
      return false;
  return true;
}

const int INF = 1000000000;

ll dp[51][3001];
vector<vector<int>> graph(51);
int a[101], b[101], c[51], d[51], roadIndex[51][51];

void solve(int node, int coins) {
  queue<pii> K;
  K.push(mp(node, coins));
  dp[node][coins] = 0;

  while (sz(K)) {
    int v = K.front().first, bal = K.front().second;
    K.pop();

    for (auto to : graph[v]) {
      int ind = roadIndex[v][to];
      if (bal >= a[ind] && dp[to][bal - a[ind]] > dp[v][bal] + b[ind]) {
        dp[to][bal - a[ind]] = dp[v][bal] + b[ind];
        K.push(mp(to, bal - a[ind]));
      }
    }

    ll exCoins = min((ll)bal + c[v], 3000ll);
    if (dp[v][exCoins] > dp[v][bal] + d[v]) {
      dp[v][exCoins] = dp[v][bal] + d[v];
      K.push(mp(v, exCoins));
    }
  }
}

int main() {
  do_not_disturb

      int n,
      m, s, i, j;
  cin >> n >> m >> s;
  for (i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v >> a[i] >> b[i];
    roadIndex[u][v] = roadIndex[v][u] = i;
    graph[u].pb(v);
    graph[v].pb(u);
  }

  s = min(s, 3000);
  for (i = 1; i <= n; i++) {
    cin >> c[i] >> d[i];
    for (j = 0; j <= 3000; j++)
      dp[i][j] = ll(INF) * ll(INF);
  }
  dp[1][s] = 0;
  solve(1, s);

  for (i = 2; i <= n; i++) {
    ll ans = ll(INF) * ll(INF);
    for (j = 0; j < 3001; j++)
      ans = min(ans, dp[i][j]);
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

/*
    Code written by Talant I.D.
*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2 * acos(0.0)
#define sz(v) int(v.size())
#define endl "\\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define do_not_disturb                                                         \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);

inline bool isvowel(char ch) {
  ch = tolower(ch);
  return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}

inline bool isprime(int n) {
  if (n < 2 || (n % 2 == 0 && n != 2))
    return false;
  for (int i = 3; i * i <= n; i += 2)
    if (n % i == 0)
      return false;
  return true;
}

const int INF = 0x3f3f3f3f;

const int maxCoins = 2500, N = 51, M = 101;
ll dp[N][maxCoins];
vector<vector<int>> graph(N);
int a[M], b[M], c[N], d[N], roadIndex[N][N];

void solve(int node, int coins) {
  dp[node][coins] = 0;
  queue<pii> K;
  K.push(mp(node, coins));

  while (sz(K)) {
    int v = K.front().first, bal = K.front().second;
    K.pop();

    for (auto to : graph[v]) {
      int ind = roadIndex[v][to];
      if (bal >= a[ind] && dp[to][bal - a[ind]] > dp[v][bal] + (ll)b[ind]) {
        dp[to][bal - a[ind]] = dp[v][bal] + (ll)b[ind];
        K.push(mp(to, bal - a[ind]));
      }
    }

    int exCoins = min(bal + c[v], maxCoins);
    if (dp[v][exCoins] > dp[v][bal] + (ll)d[v]) {
      dp[v][exCoins] = dp[v][bal] + (ll)d[v];
      K.push(mp(v, exCoins));
    }
  }
}

int main() {
  do_not_disturb

      int n,
      m, s, i, j;
  cin >> n >> m >> s;
  for (i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v >> a[i] >> b[i];
    roadIndex[u][v] = roadIndex[v][u] = i;
    graph[u].pb(v);
    graph[v].pb(u);
  }

  for (i = 1; i <= n; i++) {
    cin >> c[i] >> d[i];
    for (j = 0; j <= maxCoins; j++)
      dp[i][j] = ll(INF) * ll(INF);
  }

  solve(1, s);

  for (i = 2; i <= n; i++) {
    ll ans = ll(INF) * ll(INF);
    for (j = 0; j <= maxCoins; j++)
      ans = min(ans, dp[i][j]);
    cout << ans << endl;
  }

  return 0;
}