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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> pii;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rept(i, x, y) for (int i = x; i <= y; i++)
#define per(i, x, y) for (int i = x; i >= y; i--)
#define all(x) x.begin(), x.end()
#define pb push_back
#define fi first
#define se second
#define mes(a, b) memset(a, b, sizeof a)
#define mp make_pair
#define dd(x) cout << #x << "=" << x << " "
#define de(x) cout << #x << "=" << x << "\\n"
#define debug() cout << "I love Miyamizu Mitsuha forever.\\n"
const ll inf = 1e18;
const int maxn = 55;
const int maxm = 2500;
ll dp[maxn][maxm << 1];
ll c[maxn], d[maxn];

class edge {
public:
  int to, money, t;
  edge(int v, int a, int b) {
    to = v;
    money = a;
    t = b;
  }
};
vector<edge> v[maxn];
bool in[maxn];
queue<int> q;
void push(int x) {
  if (in[x])
    return;
  in[x] = 1;
  q.push(x);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  rept(i, 1, 50) rept(j, 0, maxm) dp[i][j] = inf;
  int n, m, xx;
  cin >> n >> m >> xx;
  rep(i, 0, m) {
    int x, y, a, b;
    cin >> x >> y >> a >> b;
    v[x].pb(edge(y, a, b));
    v[y].pb(edge(x, a, b));
  }
  rept(i, 1, n) cin >> c[i] >> d[i];
  for (int i = 0;; i++) {
    if (i * c[i] + xx >= maxm) {
      dp[1][maxm] = i * d[1];
      break;
    } else
      dp[1][i * c[1] + xx] = i * d[1];
  }
  push(1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    in[x] = 0;
    rep(i, 0, v[x].size()) {
      int to = v[x][i].to, money = v[x][i].money, t = v[x][i].t;
      rept(j, money, maxm) {
        if (dp[to][j - money] > dp[x][j] + t) {
          dp[to][j - money] = dp[x][j] + t;
          push(to);
        }
      }
      rep(j, 0, maxm) {
        if (c[to] + j < maxm) {
          if (dp[to][c[to] + j] > dp[to][j] + d[to]) {
            push(to);
            dp[to][c[to] + j] = dp[to][j] + d[to];
          }
        } else {
          if (dp[to][maxm] > dp[to][j] + d[to]) {
            push(to);
            dp[to][maxm] = dp[to][j] + d[to];
          }
        }
      }
    }
  }
  rept(i, 2, n) {
    ll ans = inf;
    rept(j, 0, maxm) ans = min(ans, dp[i][j]);
    cout << ans << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> pii;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rept(i, x, y) for (int i = x; i <= y; i++)
#define per(i, x, y) for (int i = x; i >= y; i--)
#define all(x) x.begin(), x.end()
#define pb push_back
#define fi first
#define se second
#define mes(a, b) memset(a, b, sizeof a)
#define mp make_pair
#define dd(x) cout << #x << "=" << x << " "
#define de(x) cout << #x << "=" << x << "\\n"
#define debug() cout << "I love Miyamizu Mitsuha forever.\\n"
const ll inf = 1e18;
const int maxn = 55;
ll dp[maxn][20000];
ll c[maxn], d[maxn];

class edge {
public:
  int to, money, t;
  edge(int v, int a, int b) {
    to = v;
    money = a;
    t = b;
  }
};
vector<edge> v[maxn];
bool in[maxn];
queue<int> q;
void push(int x) {
  if (in[x])
    return;
  in[x] = 1;
  q.push(x);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  rept(i, 1, 50) rept(j, 0, 10000) dp[i][j] = inf;
  int n, m, xx;
  cin >> n >> m >> xx;
  rep(i, 0, m) {
    int x, y, a, b;
    cin >> x >> y >> a >> b;
    v[x].pb(edge(y, a, b));
    v[y].pb(edge(x, a, b));
  }
  rept(i, 1, n) cin >> c[i] >> d[i];
  for (int i = 0;; i++) {
    if (i * c[i] + xx >= 10000) {
      dp[1][10000] = i * d[1];
      break;
    } else
      dp[1][i * c[1] + xx] = i * d[1];
  }
  push(1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    in[x] = 0;
    rep(i, 0, v[x].size()) {
      int to = v[x][i].to, money = v[x][i].money, t = v[x][i].t;
      rept(j, money, 10000) {
        if (dp[to][j - money] > dp[x][j] + t) {
          dp[to][j - money] = dp[x][j] + t;
          push(to);
        }
      }
      rep(j, 0, 10000) {
        if (c[to] + j < 10000) {
          if (dp[to][c[to] + j] > dp[to][j] + d[to]) {
            push(to);
            dp[to][c[to] + j] = dp[to][j] + d[to];
          }
        } else {
          if (dp[to][10000] > dp[to][j] + d[to]) {
            push(to);
            dp[to][10000] = dp[to][j] + d[to];
          }
        }
      }
    }
  }
  rept(i, 2, n) {
    ll ans = inf;
    rept(j, 0, 10000) ans = min(ans, dp[i][j]);
    cout << ans << "\\n";
  }
  return 0;
}