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 < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int INF = 1e18;

int dist[55][2525];

signed main() {
  int N, M, S;
  cin >> N >> M >> S;
  vector<vector<tuple<int, int, int>>> g(N);
  REP(i, M) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    --a;
    --b;
    g[a].emplace_back(b, c, d);
    g[b].emplace_back(a, c, d);
  }
  vector<int> c(N), d(N);
  REP(i, N) { cin >> c[i] >> d[i]; }
  if (S > 2500) {
    vector<int> d(N, INF);
    d[0] = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        que;
    que.push({0, 0});
    while (que.size()) {
      auto [cost, now] = que.top();
      que.pop();
      if (d[now] != cost)
        continue;
      for (auto [to, hoge, time] : g[now]) {
        if (CHMIN(d[to], d[now] + time)) {
          que.push({d[to], to});
        }
      }
    }
    FOR(i, 1, N) out(d[i]);
    return 0;
  }
  REP(i, 55) REP(j, 2525) dist[i][j] = INF;
  dist[0][S] = 0;
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      que;
  que.push({0, S, 0});
  while (que.size()) {
    auto [time, coin, now] = que.top();
    que.pop();
    if (dist[now][coin] != time)
      continue;
    if (CHMIN(dist[now][min(2500ll, coin + c[now])], time + d[now]))
      que.push({dist[now][min(2500ll, coin + c[now])],
                min(2500ll, coin + c[now]), now});
    for (auto [to, a, b] : g[now]) {
      if (coin - a >= 0 && CHMIN(dist[to][coin - a], time + b))
        que.push({dist[to][coin - a], coin - a, to});
    }
  }
  FOR(i, 1, N) {
    int ans = INF;
    REP(j, 2525) CHMIN(ans, dist[i][j]);
    out(ans);
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int INF = 1e18;

int dist[55][2525];

signed main() {
  int N, M, S;
  cin >> N >> M >> S;
  vector<vector<tuple<int, int, int>>> g(N);
  REP(i, M) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    --a;
    --b;
    g[a].emplace_back(b, c, d);
    g[b].emplace_back(a, c, d);
  }
  vector<int> c(N), d(N);
  REP(i, N) { cin >> c[i] >> d[i]; }
  if (S > 2500) {
    vector<int> d(N, INF);
    d[0] = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        que;
    que.push({0, 0});
    while (que.size()) {
      auto [cost, now] = que.top();
      que.pop();
      if (d[now] != cost)
        continue;
      for (auto [to, hoge, time] : g[now]) {
        if (CHMIN(d[to], d[now] + time)) {
          que.push({d[to], to});
        }
      }
    }
    FOR(i, 1, N) out(d[i]);
    return 0;
  }
  REP(i, 55) REP(j, 2525) dist[i][j] = INF;
  dist[0][S] = 0;
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      que;
  que.push({0, S, 0});
  while (que.size()) {
    auto [time, coin, now] = que.top();
    que.pop();
    if (dist[now][coin] != time)
      continue;
    while (coin <= 2500) {
      for (auto [to, a, b] : g[now]) {
        int ncoin = coin - a;
        int ntime = time + b;
        if (IN(0, ncoin, 2451) && CHMIN(dist[to][ncoin], ntime)) {
          que.push({dist[to][ncoin], ncoin, to});
        }
      }
      coin += c[now];
      time += d[now];
    }
  }
  FOR(i, 1, N) {
    int ans = INF;
    REP(j, 2525) CHMIN(ans, dist[i][j]);
    out(ans);
  }
}