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;
ll dp[maxn][3005];
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, 2500) 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];
  if (xx < 2500) {
    dp[1][xx] = 0;
    for (int i = 1;; i++) {
      if (i * c[i] + xx >= 2500) {
        dp[1][2500] = i * d[1];
        break;
      }
      dp[1][i * c[1] + xx] = i * d[1];
    }
  } else
    rept(i, 0, 2500) dp[1][i] = 0;
  q.push(1);
  in[1] = 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, 2500) {
        if (dp[to][j - money] > dp[x][j] + t) {
          dp[to][j - money] = dp[x][j] + t;
          push(to);
        }
      }
      rept(j, 0, 2500) {
        int plus = 1;
        while (plus * c[to] + j <= 2500) {
          if (dp[to][plus * c[to] + j] > dp[to][j] + plus * d[to]) {
            push(to);
            dp[to][plus * c[to] + j] = dp[to][j] + plus * d[to];
          }
          plus++;
        }
        if (dp[to][2500] > dp[to][j] + plus * d[to]) {
          push(to);
          dp[to][2500] = dp[to][j] + plus * d[to];
        }
      }
    }
  }
  rept(i, 2, n) {
    ll ans = inf;
    rept(j, 0, 2500) ans = min(ans, dp[i][j]);
    cout << ans << "\\n";
  }
  return 0;
}

Error: TLE

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][5005];
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, 5000) 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];
  if (xx < 5000) {
    dp[1][xx] = 0;
    for (int i = 1;; i++) {
      if (i * c[i] + xx >= 5000) {
        dp[1][5000] = i * d[1];
        break;
      }
      dp[1][i * c[1] + xx] = i * d[1];
    }
  } else {
    rept(i, 0, 5000) dp[1][i] = 0;
  }
  // spfa
  q.push(1);
  in[1] = 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, 5000) {
        if (dp[to][j - money] > dp[x][j] + t) {
          dp[to][j - money] = dp[x][j] + t;
          push(to);
        }
      }
      rept(j, 0, 5000) {
        int plus = 1;
        while (plus * c[to] + j <= 5000) {
          if (dp[to][plus * c[to] + j] > dp[to][j] + plus * d[to]) {
            push(to);
            dp[to][plus * c[to] + j] = dp[to][j] + plus * d[to];
          }
          plus++;
        }
        if (dp[to][5000] > dp[to][j] + plus * d[to]) {
          push(to);
          dp[to][5000] = dp[to][j] + plus * d[to];
        }
      }
    }
  }
  rept(i, 2, n) {
    ll ans = inf;
    rept(j, 0, 5000) ans = min(ans, dp[i][j]);
    cout << ans << "\\n";
  }
  return 0;
}