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

// https://atcoder.jp/contests/abc164/tasks/abc164_e
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
#define MAX_N 55
#define MAX_M 105
#define MAX_A 55
struct EDGE {
  int to, cost, time;
};
struct NODE {
  int gain, time;
};
vector<EDGE> edge[MAX_N];
NODE node[MAX_N];
ll dp[MAX_N][MAX_N * MAX_A];
const ll INF = 1e18;
int main() {
  int n, m, s;
  cin >> n >> m >> s;
  int cmax = 0;
  rep(i, m) {
    int from, to, c, t;
    cin >> from >> to >> c >> t;
    from--;
    to--;
    edge[from].push_back((EDGE){to, c, t});
    edge[to].push_back((EDGE){from, c, t});
    if (c > cmax)
      cmax = c;
  }
  cmax *= n;
  rep(i, n) {
    int g, t;
    cin >> g >> t;
    node[i] = (NODE){g, t};
  }
  rep(i, n) rep(j, cmax) dp[i][j] = INF;

  priority_queue<pair<ll, P>, vector<pair<ll, P>>, greater<pair<ll, P>>> que;
  // rep(j,min(s+1,cmax)){
  // 	dp[0][j]=0;
  // 	que.emplace(0,P(0,j));
  // }
  dp[0][min(s, cmax - 1)] = 0;
  que.emplace(0, P(0, min(s, cmax - 1)));

  while (!que.empty()) {
    pair<ll, P> p = que.top();
    que.pop();
    int v = p.second.first, c = p.second.second;
    if (dp[v][c] < p.first)
      continue;

    int next_c = c + node[v].gain;
    ll stay_t = dp[v][c] + node[v].time;
    if (stay_t < dp[v][next_c]) {
      dp[v][next_c] = stay_t;
      que.emplace(stay_t, P(v, next_c));
    }

    rep(u, edge[v].size()) {
      int next_c = c - edge[v][u].cost;
      if (next_c < 0)
        continue;
      int next_v = edge[v][u].to;
      ll move_t = dp[v][c] + edge[v][u].time;
      if (move_t < dp[next_v][next_c]) {
        dp[next_v][next_c] = move_t;
        que.emplace(move_t, P(next_v, next_c));
      }
    }
  }
  for (int i = 1; i < n; i++) {
    ll t_min = INF;
    rep(j, cmax) t_min = min(t_min, dp[i][j]);
    cout << t_min << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc164/tasks/abc164_e
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
#define MAX_N 55
#define MAX_M 105
#define MAX_A 55
struct EDGE {
  int to, cost, time;
};
struct NODE {
  int gain, time;
};
vector<EDGE> edge[MAX_N];
NODE node[MAX_N];
ll dp[MAX_N][MAX_N * MAX_A];
const ll INF = 1e18;
int main() {
  int n, m, s;
  cin >> n >> m >> s;
  int cmax = 0;
  rep(i, m) {
    int from, to, c, t;
    cin >> from >> to >> c >> t;
    from--;
    to--;
    edge[from].push_back((EDGE){to, c, t});
    edge[to].push_back((EDGE){from, c, t});
    if (c > cmax)
      cmax = c;
  }
  cmax *= n;
  rep(i, n) {
    int g, t;
    cin >> g >> t;
    node[i] = (NODE){g, t};
  }
  rep(i, n) rep(j, cmax) dp[i][j] = INF;

  priority_queue<pair<ll, P>, vector<pair<ll, P>>, greater<pair<ll, P>>> que;
  rep(j, min(s + 1, cmax)) {
    dp[0][j] = 0;
    que.emplace(0, P(0, j));
  }

  while (!que.empty()) {
    pair<ll, P> p = que.top();
    que.pop();
    int v = p.second.first, c = p.second.second;
    if (dp[v][c] < p.first)
      continue;
    // for(int t=1; c + node[v].gain * (t-1)/node[v].time<=cmax;t++){
    // 	int next_c = min(cmax-1,c + node[v].gain * t/node[v].time);
    // 	ll stay_t = dp[v][c] + t;
    // 	if(stay_t < dp[v][next_c]){
    // 		dp[v][next_c] = stay_t;
    // 		que.emplace(stay_t,P(v,next_c));
    // 	}
    // }
    for (int t = 1; c + t * node[v].gain <= cmax; t++) {
      int next_c = c + t * node[v].gain;
      ll stay_t = dp[v][c] + t * node[v].time;
      if (stay_t < dp[v][next_c]) {
        dp[v][next_c] = stay_t;
        que.emplace(stay_t, P(v, next_c));
      }
    }
    rep(u, edge[v].size()) {
      int next_c = c - edge[v][u].cost;
      if (next_c < 0)
        continue;
      int next_v = edge[v][u].to;
      ll move_t = dp[v][c] + edge[v][u].time;
      if (move_t < dp[next_v][next_c]) {
        dp[next_v][next_c] = move_t;
        que.emplace(move_t, P(next_v, next_c));
      }
    }
  }
  for (int i = 1; i < n; i++) {
    ll t_min = INF;
    rep(j, cmax) t_min = min(t_min, dp[i][j]);
    cout << t_min << endl;
  }

  return 0;
}