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

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;

struct Station {
  int num;
  int silver;
};

int main() {
  int N, M, S;
  cin >> N >> M >> S;
  vector<ll> U, V, A, B, C, D;
  vector<vector<int>> graph(N, vector<int>());
  ll maxa = 0;
  for (int i = 0; i < M; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    maxa = max((ll)a, maxa);
    U.push_back(u);
    V.push_back(v);
    A.push_back(a);
    B.push_back(b);
    graph[u].push_back(i);
    graph[v].push_back(i);
  }
  for (int i = 0; i < N; i++) {
    int c, d;
    cin >> c >> d;
    C.push_back(c);
    D.push_back(d);
  }
  ll G = maxa * (N - 1);
  G = 2500;
  if (S > G)
    S = G;
  // for(int i = 0;i < N;i++){
  //     for(int s = 0;s <= 5000;s++)
  //         stats.push_back(Station{i,s});
  // }
  auto comp = [](auto &A, auto &B) { return get<0>(A) > get<0>(B); };
  // cout<<comp(pair<int,Station>{222,Station{0,0}},pair<int,Station>{1,Station{0,0}})<<endl;
  // return 0;
  priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll>>, decltype(comp)>
      q{comp};
  q.push(tuple<ll, ll, ll>(0, 0, S));
  // q.push(pair<ll,Station>{10,Station{10,S}});
  // q.push(pair<ll,Station>{40,Station{0,S}});
  // cout<<q.top().first<<endl;
  // return 0;

  vector<vector<ll>> d(N, vector<ll>(G + 1, LLONG_MAX));
  vector<ll> d2(N, LLONG_MAX);
  d[0][S] = 0;
  while (q.size() > 0) {
    tuple<ll, ll, ll> p = q.top();
    q.pop();
    ll t = get<0>(p);
    ll num = get<1>(p);
    ll silver = get<2>(p);
    if (d[num][silver] < t)
      continue;
    // cout<<st.num<<" "<<st.silver<<endl;
    // cout<<p.first<<endl;
    d2[num] = min(d2[num], t);

    if (silver < G) {
      ll nextsi = min(G, silver + C[num]);
      ll time = t + D[num];
      if (d[num][nextsi] > time) {
        d[num][nextsi] = time;
        q.push(tuple<ll, ll, ll>{time, num, nextsi});
      }
    }

    for (int i = 0; i < graph[num].size(); i++) {
      int rail = graph[num][i];
      int to = U[rail] == num ? V[rail] : U[rail];

      ll time = t + B[rail];
      ll nextsi = silver - A[rail];
      if (nextsi < 0)
        continue;
      if (d[to][nextsi] > time) {
        d[to][nextsi] = time;
        q.push(tuple<ll, ll, ll>{time, to, nextsi});
      }
    }
  }
  for (int i = 1; i < N; i++) {
    cout << d2[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;

struct Station {
  int num;
  int silver;
};

int main() {
  int N, M, S;
  cin >> N >> M >> S;
  vector<ll> U, V, A, B, C, D;
  vector<vector<int>> graph(N, vector<int>());
  ll maxa = 0;
  for (int i = 0; i < M; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    maxa = max((ll)a, maxa);
    U.push_back(u);
    V.push_back(v);
    A.push_back(a);
    B.push_back(b);
    graph[u].push_back(i);
    graph[v].push_back(i);
  }
  for (int i = 0; i < N; i++) {
    int c, d;
    cin >> c >> d;
    C.push_back(c);
    D.push_back(d);
  }
  ll G = maxa * (N - 1);
  // G = 2500;
  if (S > G)
    S = G;
  // for(int i = 0;i < N;i++){
  //     for(int s = 0;s <= 5000;s++)
  //         stats.push_back(Station{i,s});
  // }
  auto comp = [](auto &A, auto &B) { return get<0>(A) > get<0>(B); };
  // cout<<comp(pair<int,Station>{222,Station{0,0}},pair<int,Station>{1,Station{0,0}})<<endl;
  // return 0;
  priority_queue<tuple<ll, ll, ll, ll>, vector<tuple<ll, ll, ll, ll>>,
                 decltype(comp)>
      q{comp};
  q.push(tuple<ll, ll, ll, ll>(0, 0, S, 0));
  // q.push(pair<ll,Station>{10,Station{10,S}});
  // q.push(pair<ll,Station>{40,Station{0,S}});
  // cout<<q.top().first<<endl;
  // return 0;

  vector<vector<ll>> d(N, vector<ll>(G + 1, LLONG_MAX));
  vector<ll> d2(N, LLONG_MAX);
  d[0][S] = 0;
  while (q.size() > 0) {
    tuple<ll, ll, ll, ll> p = q.top();
    q.pop();
    ll t = get<0>(p);
    ll num = get<1>(p);
    ll silver = get<2>(p);
    ll from = get<3>(p);
    if (d[num][silver] < t)
      continue;
    // cout<<st.num<<" "<<st.silver<<endl;
    // cout<<p.first<<endl;
    d2[num] = min(d2[num], t);
    for (int i = 0; i < graph[num].size(); i++) {
      int rail = graph[num][i];
      int to = U[rail] == num ? V[rail] : U[rail];
      // if(to == from)continue;
      for (ll s = silver; true; s += C[num]) {
        if (s > G)
          s = G;
        d[num][s] = min(d[num][s], t + (s - silver) / C[num] * D[num]);
        ll time = t + (s - silver) / C[num] * D[num] + B[rail];
        // cout<<time<<endl;
        int nextsi = s - A[rail];
        if (nextsi < 0)
          continue;
        if (d[to][nextsi] > time) {
          d[to][nextsi] = time;
          q.push(tuple<ll, ll, ll, ll>{time, to, nextsi, num});
        }
        if (s == G)
          break;
      }
    }
  }
  for (int i = 1; i < N; i++) {
    cout << d2[i] << endl;
  }
}