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++)

using namespace std;
using LL = long long;
const int Max_A = 5010;
const int Max_N = 55;
const LL INF = 1e18;

struct node {
  int silver, num;
  LL now_time;
  node(int s, int v, LL t) : silver(s), num(v), now_time(t) {}
  bool operator>(const node &v) const {
    if (now_time == v.now_time)
      return silver > v.silver;
    return now_time > v.now_time;
  }
};

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

LL dist[Max_A][Max_N];
vector<vector<edge>> G;
vector<int> C;
vector<LL> D;
priority_queue<node, vector<node>, greater<node>> q;

void dijkstra() {
  while (!q.empty()) {
    node v = q.top();
    q.pop();
    int S = v.silver, n = v.num;
    LL T = v.now_time;
    if (dist[S][n] < T)
      continue;
    int nS = min(Max_A - 1, S + C[n]);
    if (dist[nS][n] > T + D[n]) {
      node v_plus(nS, n, T + D[n]);
      dist[nS][n] = T + D[n];
      q.push(v_plus);
    }
    rep(i, G[n].size()) {
      int next = G[n][i].to, c = G[n][i].cost;
      LL t = G[n][i].time;
      if (S < c)
        continue;
      if (dist[S - c][next] <= T + t)
        continue;
      dist[S - c][next] = T + t;
      node nv(S - c, next, T + t);
      q.push(nv);
    }
  }
}

int main() {
  int N, M, S;
  cin >> N >> M >> S;
  G.resize(N);
  rep(i, M) {
    int u, v, a;
    LL b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    G[u].emplace_back(v, a, b);
    G[v].emplace_back(u, a, b);
  }
  C.resize(N), D.resize(N);
  rep(i, N) cin >> C[i] >> D[i];
  rep(i, Max_A) { rep(j, Max_N) dist[i][j] = INF; }
  S = min(S, Max_A);
  dist[S][0] = 0;
  node st(S, 0, 0);
  q.push(st);
  dijkstra();
  vector<LL> ans(N - 1, INF);
  rep(i, Max_A) { rep(j, N - 1) ans[j] = min(ans[j], dist[i][j + 1]); }
  rep(i, N - 1) cout << ans[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
const int Max_A = 5010;
const int Max_N = 55;
const LL INF = 1e18;

struct node {
  int silver, num;
  LL now_time;
  node(int s, int v, LL t) : silver(s), num(v), now_time(t) {}
  bool operator>(const node &v) const {
    if (now_time == v.now_time)
      return silver > v.silver;
    return now_time > v.now_time;
  }
};

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

LL dist[Max_A][Max_N];
vector<vector<edge>> G;
vector<int> C;
vector<LL> D;
priority_queue<node, vector<node>, greater<node>> q;

void dijkstra() {
  while (!q.empty()) {
    node v = q.top();
    q.pop();
    int S = v.silver, n = v.num;
    LL T = v.now_time;
    if (dist[S][n] < T)
      continue;
    node v_plus(min(Max_A, S + C[n]), n, T + D[n]);
    q.push(v_plus);
    rep(i, G[n].size()) {
      int next = G[n][i].to, c = G[n][i].cost;
      LL t = G[n][i].time;
      if (S < c)
        continue;
      if (dist[S - c][next] <= T + t)
        continue;
      dist[S - c][next] = T + t;
      node nv(S - c, next, T + t);
      q.push(nv);
    }
  }
}

int main() {
  int N, M, S;
  cin >> N >> M >> S;
  G.resize(N);
  rep(i, M) {
    int u, v, a;
    LL b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    G[u].emplace_back(v, a, b);
    G[v].emplace_back(u, a, b);
  }
  C.resize(N), D.resize(N);
  rep(i, N) cin >> C[i] >> D[i];
  rep(i, Max_A) { rep(j, Max_N) dist[i][j] = INF; }
  S = min(S, Max_A);
  dist[S][0] = 0;
  node st(S, 0, 0);
  q.push(st);
  dijkstra();
  vector<LL> ans(N - 1, INF);
  rep(i, Max_A) { rep(j, N - 1) ans[j] = min(ans[j], dist[i][j + 1]); }
  rep(i, N - 1) cout << ans[i] << endl;

  return 0;
}