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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\\n'
#define all(x) (x).begin(), (x).end()
const int INF = 1e9 + 5;
const int N = 200000 + 5;
const ll oo = 1e18 + 5;
const ll mod = 998244353;

vector<pair<int, int>> v[55];
ll dp[55][2505];

struct node {
  ll val;
  int id, sum;
  node(ll val, int id, int sum) : val(val), id(id), sum(sum) {}
};

void min_self(int &a, int b) {
  if (a > b)
    a = b;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  const int maxx = 2500;
  int n, m, s;
  cin >> n >> m >> s;
  vector<int> a(105), b(105), c(55), d(55);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y >> a[i] >> b[i];
    --x;
    --y;
    v[x].push_back({y, i});
    v[y].push_back({x, i});
  }
  for (int i = 0; i < n; ++i)
    cin >> c[i] >> d[i];
  min_self(s, maxx);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= maxx; ++j)
      dp[i][j] = oo;
  }
  queue<node> Q;
  dp[0][s] = 0;
  Q.push({0, 0, s});
  while (!Q.empty()) {
    node ele = Q.front();
    Q.pop();
    int idx = ele.id;
    int coin = ele.sum;
    if (ele.val > dp[idx][coin])
      continue;
    int n_coin = min(maxx, coin + c[idx]);
    if (dp[idx][n_coin] > dp[idx][coin] + d[idx]) {
      dp[idx][n_coin] = dp[idx][coin] + d[idx];
      Q.push({dp[idx][n_coin], idx, n_coin});
    }
    for (pair<int, int> x : v[idx]) {
      int xx = x.first;
      int idd = x.second;
      if (coin < a[idd])
        continue;
      if (dp[xx][coin - a[idd]] > dp[idx][coin] + b[idd]) {
        dp[xx][coin - a[idd]] = dp[idx][coin] + b[idd];
        Q.push({dp[xx][coin - a[idd]], xx, coin - a[idd]});
      }
    }
  }
  for (int i = 1; i < n; ++i) {
    ll ans = oo;
    for (int coin = 0; coin <= maxx; ++coin) {
      ans = min(ans, dp[i][coin]);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\\n'
#define all(x) (x).begin(), (x).end()
const int INF = 1e9 + 5;
const int N = 200000 + 5;
const ll oo = 1e18 + 5;
const ll mod = 998244353;

vector<pair<int, int>> v[55];
ll dp[55][2505];

struct node {
  ll val;
  int id, sum;
  node(ll val, int id, int sum) : val(val), id(id), sum(sum) {}
};

void min_self(int &a, int b) {
  if (a > b)
    a = b;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  const int maxx = 2500;
  int n, m, s;
  cin >> n >> m >> s;
  vector<int> a(55), b(55), c(55), d(55);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y >> a[i] >> b[i];
    --x;
    --y;
    v[x].push_back({y, i});
    v[y].push_back({x, i});
  }
  for (int i = 0; i < n; ++i)
    cin >> c[i] >> d[i];
  min_self(s, maxx);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= maxx; ++j)
      dp[i][j] = oo;
  }
  queue<node> Q;
  dp[0][s] = 0;
  Q.push({0, 0, s});
  while (!Q.empty()) {
    node ele = Q.front();
    Q.pop();
    int idx = ele.id;
    int coin = ele.sum;
    if (ele.val > dp[idx][coin])
      continue;
    int n_coin = min(maxx, coin + c[idx]);
    if (dp[idx][n_coin] > dp[idx][coin] + d[idx]) {
      dp[idx][n_coin] = dp[idx][coin] + d[idx];
      Q.push({dp[idx][n_coin], idx, n_coin});
    }
    for (pair<int, int> x : v[idx]) {
      int xx = x.first;
      int idd = x.second;
      if (coin < a[idd])
        continue;
      if (dp[xx][coin - a[idd]] > dp[idx][coin] + b[idd]) {
        dp[xx][coin - a[idd]] = dp[idx][coin] + b[idd];
        Q.push({dp[xx][coin - a[idd]], xx, coin - a[idd]});
      }
    }
  }
  for (int i = 1; i < n; ++i) {
    ll ans = oo;
    for (int coin = 0; coin <= maxx; ++coin) {
      ans = min(ans, dp[i][coin]);
    }
    cout << ans << endl;
  }
  return 0;
}