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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ll n, m, s;
  cin >> n >> m >> s;
  ll max_a = 0;
  vector<vector<tuple<ll, ll, ll>>> G(n); // (place, silver coin, minute)
  for (int i = 0; i < m; i++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    G[u].push_back(make_tuple(v, a, b));
    G[v].push_back(make_tuple(u, a, b));
    max_a = max(a, max_a);
  }

  vector<ll> C(n), D(n);
  for (int i = 0; i < n; i++) {
    cin >> C[i] >> D[i];
  }

  const ll INF = 1e18;

  ll cost[n][max_a * n]; // minute [place][silver]

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < max_a * n; j++) {
      cost[i][j] = INF;
    }
  }

  priority_queue<tuple<ll, int, ll>> Q; // minutes, place, silver
  Q.push(make_tuple(0, 0, min(s, max_a * n - 1)));
  cost[0][min(s, max_a * n - 1)] = 0;

  while (!Q.empty()) {
    ll minute, silver;
    int x;
    tie(minute, x, silver) = Q.top();
    Q.pop();
    minute = -minute;

    for (auto t : G[x]) {
      int y;
      ll pay, take_minute;
      tie(y, pay, take_minute) = t;
      if (silver >= pay && take_minute + minute < cost[y][silver - pay]) {
        Q.push(make_tuple(-(take_minute + minute), y, silver - pay));
        cost[y][silver - pay] = take_minute + minute;
      }
    }

    if (silver + C[x] < max_a * n && minute + D[x] < cost[x][silver + C[x]]) {
      Q.push(make_tuple(-(minute + D[x]), x, silver + C[x]));
      cost[x][silver + C[x]] = minute + D[x];
    }
  }

  for (int i = 1; i < n; i++) {
    ll ans = INF;
    for (int j = 0; j < max_a * n; j++) {
      ans = min(ans, cost[i][j]);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ll n, m, s;
  cin >> n >> m >> s;
  ll max_a = 0;
  vector<vector<tuple<ll, ll, ll>>> G(n); // (place, silver coin, minute)
  for (int i = 0; i < m; i++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    G[u].push_back(make_tuple(v, a, b));
    G[v].push_back(make_tuple(u, a, b));
    max_a = max(a, max_a);
  }

  vector<ll> C(n), D(n);
  for (int i = 0; i < n; i++) {
    cin >> C[i] >> D[i];
  }

  const ll INF = 1e18;

  ll cost[n][max_a * n]; // minute [place][silver]

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < max_a * n; j++) {
      cost[i][j] = INF;
    }
  }

  priority_queue<tuple<ll, int, ll>> Q; // minutes, place, silver
  Q.push(make_tuple(0, 0, min(s, max_a * n - 1)));

  ll check = 1;

  while (!Q.empty()) {
    ll minute, silver;
    int x;
    tie(minute, x, silver) = Q.top();
    Q.pop();
    minute = -minute;

    if (cost[x][silver] <= minute)
      continue;

    cost[x][silver] = minute;

    check = check | (1LL << x);
    if (check == (1LL) << n - 1)
      break;

    for (auto t : G[x]) {
      int y;
      ll pay, take_minute;
      tie(y, pay, take_minute) = t;
      if (silver >= pay && cost[y][silver - pay] == INF)
        Q.push(make_tuple(-(take_minute + minute), y, silver - pay));
    }

    ll exchange = 1;
    while (silver + exchange * C[x] < max_a * n) {
      if (cost[x][silver + exchange * C[x]] == INF)
        Q.push(make_tuple(-(minute + exchange * D[x]), x,
                          silver + exchange * C[x]));
      exchange++;
    }
  }

  for (int i = 1; i < n; i++) {
    ll ans = INF;
    for (int j = 0; j < max_a * n; j++) {
      ans = min(ans, cost[i][j]);
    }
    cout << ans << endl;
  }

  return 0;
}