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

#include <bits/stdc++.h>

#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 reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
#define sz(v) ((int)v.size())

using namespace std;

using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() {
  cout << "Yes" << endl;
  exit(0);
}
inline void no() {
  cout << "No" << endl;
  exit(0);
}
template <typename T>
class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T>
class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> inline void amax(T &x, T y) { x = max(x, y); }
template <typename T> inline void amin(T &x, T y) { x = min(x, y); }
template <typename T> inline T exp(T x, i64 n, T e = 1) {
  T r = e;
  while (n > 0) {
    if (n & 1)
      r *= x;
    x *= x;
    n >>= 1;
  }
  return r;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  each(x, v) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  rep(i, v.size()) {
    if (i)
      os << ' ';
    os << v[i];
  }
  return os;
}
void solve();
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(16);
  solve();
  return 0;
}

struct Edge {
  int to, cost, time;
  Edge(int to, int cost, int time) : to(to), cost(cost), time(time) {}
};

i64 dp[50][2551];

void solve() {
  int n, m, s;
  cin >> n >> m >> s;
  amin(s, 2500);
  vector<vector<Edge>> g(n);
  rep(i, m) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--, v--;
    g[u].emplace_back(v, a, b);
    g[v].emplace_back(u, a, b);
  }
  vi32 cs(n), ds(n);
  rep(i, n) {
    int c, d;
    cin >> c >> d;
    cs[i] = c, ds[i] = d;
  }
  rep(i, 50) repc(j, 2550) dp[i][j] = 1e18;
  pqasc<tuple<i64, int, int>> q;
  q.emplace(0, s, 0);
  while (sz(q)) {
    i64 t;
    int s, u;
    tie(t, s, u) = q.top(), q.pop();
    if (dp[u][s] != 1e18)
      continue;
    dp[u][s] = t;
    if (s + cs[u] <= 2550 && dp[u][s + cs[u]] == 1e18) {
      q.emplace(t + ds[u], s + cs[u], u);
    }
    each(e, g[u]) {
      if (s - e.cost < 0)
        continue;
      if (dp[e.to][s - e.cost] != 1e18)
        continue;
      q.emplace(t + e.time, s - e.cost, e.to);
    }
  }
  reps(i, n - 1) {
    i64 ans = 1e18;
    repc(j, 2550) amin(ans, dp[i][j]);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#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 reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
#define sz(v) ((int)v.size())

using namespace std;

using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() {
  cout << "Yes" << endl;
  exit(0);
}
inline void no() {
  cout << "No" << endl;
  exit(0);
}
template <typename T>
class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T>
class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> inline void amax(T &x, T y) { x = max(x, y); }
template <typename T> inline void amin(T &x, T y) { x = min(x, y); }
template <typename T> inline T exp(T x, i64 n, T e = 1) {
  T r = e;
  while (n > 0) {
    if (n & 1)
      r *= x;
    x *= x;
    n >>= 1;
  }
  return r;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  each(x, v) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  rep(i, v.size()) {
    if (i)
      os << ' ';
    os << v[i];
  }
  return os;
}
void solve();
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(16);
  solve();
  return 0;
}

struct Edge {
  int v, cost, time;
  Edge(int v, int cost, int time) : v(v), cost(cost), time(time) {}
};

i64 dp[50][50][5001];

void solve() {
  int n, m, S;
  cin >> n >> m >> S;
  S = min(5000, S);
  vector<vector<Edge>> g(n);
  rep(i, m) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--, v--;
    g[u].emplace_back(v, a, b);
    g[v].emplace_back(u, a, b);
  }
  vector<pair<int, int>> charge;
  rep(i, n) {
    int c, d;
    cin >> c >> d;
    charge.emplace_back(c, d);
  }
  rep(i, 50) rep(j, 50) repc(k, 5000) { dp[i][j][k] = 1e18; }
  rep(i, 50) dp[i][i][0] = 0;
  rep(u, n) {
    pqasc<tuple<i64, int, int>> q;
    q.emplace(0, 0, u);
    while (q.size()) {
      i64 t;
      int c, v;
      tie(t, c, v) = q.top(), q.pop();
      if (dp[u][v][c] != t)
        continue;
      each(e, g[v]) {
        if (c + e.cost > 5000)
          continue;
        if (dp[u][e.v][c + e.cost] <= t + e.time)
          continue;
        dp[u][e.v][c + e.cost] = t + e.time;
        q.emplace(t + e.time, c + e.cost, e.v);
      }
    }
  }
  vi64 ans(n, 1e18);
  pqasc<tuple<i64, int, int>> q;
  q.emplace(0, S, 0);
  while (q.size()) {
    i64 time;
    int s, u;
    tie(time, s, u) = q.top(), q.pop();
    if (ans[u] != 1e18)
      continue;
    ans[u] = time;
    repc(i, 5000) rep(v, n) {
      if (ans[v] != 1e18)
        continue;
      if (dp[u][v][i] == 1e18)
        continue;
      int c = max(0, i - s);
      int C = (c + charge[u].first - 1) / charge[u].first;
      i64 t = dp[u][v][i] + (i64)C * charge[u].second;
      q.emplace(time + t, s + C - i, v);
    }
  }
  reps(i, n - 1) { cout << ans[i] << endl; }
}