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

#include <bits/stdc++.h>
using namespace std;
using ll = long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using Map = map<ll, vl>;
using T = tuple<ll, ll, ll>;
using vvl = vector<vector<ll>>;
const ll INF = 1L << 60;
const ll MOD = 998244353;

struct H {
  ll to;
  ll cost;
  ll time;
};

int main() {
  ll N;
  cin >> N;
  ll M;
  cin >> M;
  ll S;
  cin >> S;
  vector<ll> U(M, 0);
  vector<ll> V(M, 0);
  vector<ll> A(M, 0);
  vector<ll> B(M, 0);
  vector<ll> C(N, 0);
  vector<ll> D(N, 0);
  for (ll i = 0; i < M; i++) {
    cin >> U[i] >> V[i] >> A[i] >> B[i];
    U[i]--;
    V[i]--;
    ;
  }
  for (ll i = 0; i < N; i++) {
    cin >> C[i] >> D[i];
  }
  vector<vector<H>> hen(N, vector<H>(0));
  ;
  for (ll i = 0; i < M; i++) {
    for (ll j = 0; j <= 2500; j++) {
      hen[U[i]].emplace_back(H{V[i], A[i], B[i]});
      hen[V[i]].emplace_back(H{U[i], A[i], B[i]});
      ;
    };
  }

  // dp[i][j]は都市iにちょうどj円ある状態で存在する最小時間
  vector<vector<ll>> dp(N, vector<ll>(2501, INF));
  dp[0][min(2500L, S)] = 0;
  priority_queue<T, vector<T>, greater<T>> pq;
  // pqに格納するのは、T(最小時間、都市、金)
  pq.push(T(0, 0, min(2500L, S)));
  while (!pq.empty()) {
    T x = pq.top();
    pq.pop();
    ll y = get<0>(x);
    ll z = get<1>(x);
    ll w = get<2>(x);
    if (dp[z][w] < y)
      continue;
    for (H p : hen[z]) {
      ll cos = p.cost;
      ll toto = p.to;
      ll w2 = w - cos;
      ll tim = y + p.time;
      if (cos <= w && dp[toto][w2] > tim) {
        dp[toto][w2] = tim;
        pq.push(T(tim, toto, w2));
      };
    }
    ll w2 = w + C[z];
    ll y2 = y + D[z];
    ll Min = min(2500L, w2);
    if (dp[z][Min] > y2) {
      dp[z][Min] = y2;
      pq.push(T(y2, z, Min));
    }
  }
  for (ll i = 1; i < N; i++) {
    ll ans = INF;
    for (ll j = 0; j <= 2500; j++) {
      ans = min(ans, dp[i][j]);
    }
    cout << ans << endl;
    ;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using Map = map<ll, vl>;
using T = tuple<ll, ll, ll>;
using vvl = vector<vector<ll>>;
const ll INF = 1LL << 60;
const ll MOD = 998244353;

struct H {
  ll to;
  ll cost;
  ll time;
};

int main() {
  ll N;
  cin >> N;
  ll M;
  cin >> M;
  ll S;
  cin >> S;
  vector<ll> U(M, 0);
  vector<ll> V(M, 0);
  vector<ll> A(M, 0);
  vector<ll> B(M, 0);
  vector<ll> C(N, 0);
  vector<ll> D(N, 0);
  for (ll i = 0; i < M; i++) {
    cin >> U[i] >> V[i] >> A[i] >> B[i];
    U[i]--;
    V[i]--;
    ;
  }
  for (ll i = 0; i < N; i++) {
    cin >> C[i] >> D[i];
  }
  vector<vector<H>> hen(N, vector<H>(0));
  ;
  for (ll i = 0; i < M; i++) {
    for (ll j = 0; j <= 2500; j++) {
      hen[U[i]].emplace_back(H{V[i], A[i], B[i]});
      hen[V[i]].emplace_back(H{U[i], A[i], B[i]});
      ;
    };
  }

  // dp[i][j]は都市iにちょうどj円ある状態で存在する最小時間
  vector<vector<ll>> dp(N, vector<ll>(2501, INF));
  dp[0][min(2500LL, S)] = 0;
  priority_queue<T, vector<T>, greater<T>> pq;
  // pqに格納するのは、T(最小時間、都市、金)
  pq.push(T(0, 0, min(2500LL, S)));
  while (!pq.empty()) {
    T x = pq.top();
    pq.pop();
    ll y = get<0>(x);
    ll z = get<1>(x);
    ll w = get<2>(x);
    if (dp[z][w] < y)
      continue;
    for (auto p : hen[z]) {
      if (p.cost <= w && dp[p.to][w - p.cost] > y + p.time) {
        dp[p.to][w - p.cost] = y + p.time;
        pq.push(T(y + p.time, p.to, w - p.cost));
      };
    }
    if (dp[z][min(2500LL, w + C[z])] > y + D[z]) {
      dp[z][min(2500LL, w + C[z])] = y + D[z];
      pq.push(T(y + D[z], z, min(2500LL, w + C[z])));
    }
  }
  for (ll i = 1; i < N; i++) {
    ll ans = INF;
    for (ll j = 0; j <= 2500; j++) {
      ans = min(ans, dp[i][j]);
    }
    cout << ans << endl;
    ;
  }

  return 0;
}