Problem p02703 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define mod 1000000007
#define MOD 998244353
#define INF 40000000000000000
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef tuple<int, int, int> TT;
signed main() {
  int N, M, S;
  cin >> N >> M >> S;
  vector<int> Graph[N];
  int A[N][N], B[N][N];
  rep(i, M) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    Graph[u].push_back(v);
    Graph[v].push_back(u);
    int a;
    cin >> a;
    A[u][v] = a;
    A[v][u] = a;
    int b;
    cin >> b;
    B[u][v] = b;
    B[v][u] = b;
  }
  int C[N], D[N];
  rep(i, N) cin >> C[i] >> D[i];
  vector<vector<int>> dis(N, vector<int>(2501, INF));
  S = min(S, (int)2500);
  dis[0][S] = 0;
  priority_queue<TT, vector<TT>, greater<TT>> que;
  que.push(make_tuple(0, 0, S));
  while (!que.empty()) {
    TT t = que.top();
    que.pop();
    int time, now, money;
    tie(time, now, money) = t;
    // cout << time << ' ' << now << ' ' << money << endl;
    if (dis[now][money] < time)
      continue;
    int nextmoney = min((int)2500, money + C[now]);
    if (dis[now][nextmoney] > time + D[now]) {
      dis[now][nextmoney] = time + D[now];
      que.push(make_tuple(time + D[now], now, nextmoney));
    }
    for (auto next : Graph[now]) {
      int a = A[now][next], b = B[now][next];
      if (money < a)
        continue;
      if (dis[next][money - a] > time + b) {
        dis[next][money - a] = time + b;
        que.push(make_tuple(time + b, next, money - a));
      }
    }
  }

  for (int i = 1; i <= N - 1; i++) {
    int ans = INF;
    rep(j, 2501) chmin(ans, dis[i][j]);
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define mod 1000000007
#define MOD 998244353
#define INF 40000000000000000
typedef tuple<int, int, int> TT;
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int cost[55][55];
int minu[55][55];
int C[55], D[55];
int N, M, S;
signed main() {
  cin >> N >> M >> S;
  rep(i, 55) rep(j, 55) minu[i][j] = -1;
  vector<int> Graph[N];
  rep(i, M) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    cost[u][v] = a;
    cost[v][u] = a;
    minu[u][v] = b;
    minu[v][u] = b;
    Graph[u].push_back(v);
    Graph[v].push_back(u);
  }
  rep(i, N) cin >> C[i] >> D[i];

  vector<vector<int>> dp(N, vector<int>(100000, INF));
  vector<vector<int>> d(N, vector<int>(N, INF));
  rep(i, N) rep(j, N) {
    if (minu[i][j] == -1)
      continue;
    d[i][j] = minu[i][j];
  }
  for (int k = 0; k < N; k++) {
    rep(i, N) rep(j, N) chmin(d[i][j], d[i][k] + d[k][j]);
  }
  if (S >= 100000) {
    rep(i, N - 1) cout << d[0][i + 1] << endl;
    return 0;
  }

  priority_queue<TT, vector<TT>, greater<TT>> que;
  vector<int> ans(N, INF);
  dp[0][S] = 0;
  que.push(make_tuple(0, 0, S));

  while (!que.empty()) {
    TT t = que.top();
    que.pop();
    int ti, now, money;
    tie(ti, now, money) = t;
    if (dp[now][money] < ti)
      continue;
    int cnow = C[now], dnow = D[now];
    if (money + cnow >= 100000) {
      rep(i, N) chmin(ans[i], ti + dnow);
    } else {
      if (dp[now][money + cnow] > ti + dnow) {
        dp[now][money + cnow] = ti + dnow;
        que.push(make_tuple(ti + dnow, now, money + cnow));
      }
    }
    for (auto next : Graph[now]) {
      // cout<<next<<' '<<now<<' '<<money<<endl;
      if (cost[now][next] <= money) {
        if (dp[next][money - cost[now][next]] > ti + minu[now][next]) {
          dp[next][money - cost[now][next]] = ti + minu[now][next];
          que.push(
              make_tuple(ti + minu[now][next], next, money - cost[now][next]));
        }
      }
    }
  }
  for (int i = 1; i < N; i++) {
    int aa = ans[i];
    rep(j, 100000) { chmin(aa, dp[i][j]); }
    cout << aa << endl;
  }
}