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

#include <bits/stdc++.h>

using namespace std;

const long long INF = 1ll << 61;
const int MAX = 2500;

struct Node {
  long long time;
  int money;
  int node;

  bool operator>(const Node &t) const { return time > t.time; }
};

int main() {
  int N, M, S;
  cin >> N >> M >> S;

  vector<vector<Node>> to(N);
  for (int i = 0; i < M; i++) {
    int U, V, A, B;
    cin >> U >> V >> A >> B;
    --U;
    --V;

    to[U].push_back({B, A, V});
    to[V].push_back({B, A, U});
  }

  vector<int> C(N), D(N);
  for (int i = 0; i < N; i++)
    cin >> C[i] >> D[i];

  vector<vector<long long>> dp(N, vector<long long>(MAX + 1, INF));
  priority_queue<Node, vector<Node>, greater<Node>> pq;

  S = min(S, MAX);
  pq.push({0, S, 0});
  dp[0][S] = 0;
  while (!pq.empty()) {
    auto [time, money, node] = pq.top();
    pq.pop();

    if (dp[node][money] < time)
      continue;

    for (auto [t, m, next] : to[node]) {
      if (money >= m && time + t < dp[next][min(money - m, MAX)]) {
        dp[next][min(money - m, MAX)] = time + t;
        pq.push({time + t, min(money - m, MAX), next});
      }
    }

    if (money < MAX) {
      if (time + D[node] < dp[node][min(money + C[node], MAX)]) {
        dp[node][min(money + C[node], MAX)] = time + D[node];
        pq.push({time + D[node], min(money + C[node], MAX), node});
      }
    }
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < MAX; j++) {
      dp[i][j + 1] = min(dp[i][j + 1], dp[i][j]);
    }
  }

  for (int i = 1; i < N; i++) {
    cout << dp[i][MAX] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const long long INF = 1ll << 61;

struct Node {
  long long time;
  int money;
  int node;

  bool operator>(const Node &t) const { return time > t.time; }
};

int main() {
  int N, M, S;
  cin >> N >> M >> S;

  int MAX = 0;

  vector<vector<Node>> to(N);
  for (int i = 0; i < M; i++) {
    int U, V, A, B;
    cin >> U >> V >> A >> B;
    --U;
    --V;
    MAX = max(A, MAX);

    to[U].push_back({B, A, V});
    to[V].push_back({B, A, U});
  }

  MAX *= (N - 1);

  vector<int> C(N), D(N);
  for (int i = 0; i < N; i++)
    cin >> C[i] >> D[i];

  vector<vector<long long>> dp(N, vector<long long>(MAX + 1, INF));
  priority_queue<Node, vector<Node>, greater<Node>> pq;
  set<int> fin;

  S = min(S, MAX);
  pq.push({0, S, 0});
  dp[0][S] = 0;
  fin.insert(0);
  while (!pq.empty() && fin.size() < N) {
    auto [time, money, node] = pq.top();
    pq.pop();

    fin.insert(node);

    if (dp[node][money] < time)
      continue;

    for (auto [t, m, next] : to[node]) {
      for (int j = max((m - money) / C[node], 0);; j++) {
        if (money - m + j * C[node] < 0)
          continue;

        if (time + t + j * D[node] <
            dp[next][min(money - m + j * C[node], MAX)]) {
          dp[next][min(money - m + j * C[node], MAX)] = time + t + j * D[node];
          pq.push({dp[next][min(money - m + j * C[node], MAX)],
                   min(money - m + j * C[node], MAX), next});
        }

        if (money - m + j * C[node] >= MAX)
          break;
      }
    }
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < MAX; j++) {
      dp[i][j + 1] = min(dp[i][j + 1], dp[i][j]);
    }
  }

  for (int i = 1; i < N; i++) {
    cout << dp[i][MAX] << endl;
  }

  return 0;
}