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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const ll MAX = 2500LL;
typedef pair<ll, ll> P;

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, m, s;
  cin >> n >> m >> s;
  s = min(s, MAX - 1);

  vector<vector<P>> edge(n * MAX);
  for (int i = 0; i < m; i++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    for (int j = a; j < MAX; j++) {
      edge[u * MAX + j].push_back({v * MAX + j - a, b});
      edge[v * MAX + j].push_back({u * MAX + j - a, b});
    }
  }

  for (int i = 0; i < n; i++) {
    ll c, d;
    cin >> c >> d;
    for (int j = 0; j < MAX; j++) {
      edge[i * MAX + j].push_back({i * MAX + min(j + c, MAX - 1), d});
    }
  }

  vector<ll> ans(n * MAX, 1LL << 60);
  priority_queue<P, vector<P>, greater<P>> q;
  ans[s] = 0;
  q.push({0, s});

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    ll time = p.first;
    int cur = p.second;
    if (ans[cur] < time)
      continue;
    for (P nxt : edge[cur]) {
      int next_node = nxt.first;
      ll total_time = time + nxt.second;
      if (ans[next_node] > total_time) {
        ans[next_node] = total_time;
        q.push({total_time, next_node});
      }
    }
  }

  for (int i = 1; i < n; i++) {
    ll mn = 1LL << 60;
    for (int j = 0; j < MAX; j++)
      mn = min(mn, ans[i * MAX + j]);
    cout << mn << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;
typedef pair<int, ll> P;
typedef pair<ll, P> PP;

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, m, s;
  cin >> n >> m >> s;

  vector<vector<PP>> edge(n);
  for (int i = 0; i < m; i++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    edge[u].push_back(PP(b, P(v, -a)));
    edge[v].push_back(PP(b, P(u, -a)));
  }

  for (int i = 0; i < n; i++) {
    ll c, d;
    cin >> c >> d;
    edge[i].push_back(PP(d, P(i, c)));
  }

  vector<ll> ans(n, 1LL << 60);
  priority_queue<PP, vector<PP>, greater<PP>> q;
  ans[0] = 0;
  q.push(PP(0, P(0, min(2500LL, s))));

  while (true) {
    PP pp = q.top();
    q.pop();

    ll cur_t = pp.first;
    int cur = pp.second.first;
    ll cur_cost = pp.second.second;

    ll mx = *max_element(ans.begin(), ans.end());
    if (cur_t >= mx)
      break;

    for (auto nxt : edge[cur]) {
      ll t = nxt.first;
      int to = nxt.second.first;
      ll cost = nxt.second.second;

      ll remain_money = min(2500LL, cur_cost + cost);
      if (remain_money >= 0) {
        ans[to] = min(ans[to], cur_t + t);
        q.push(PP(cur_t + t, P(to, remain_money)));
      }
    }
  }

  for (int i = 1; i < n; i++)
    cout << ans[i] << '\\n';
  return 0;
}