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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007ll
#define mod1 998244353ll
#define endl '\\n'
#define inf ((ll)1e18 + 1)
#include <chrono>
using namespace chrono;
mt19937_64 rng(high_resolution_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>
#define deb(x) cout << "-->  " << #x << " :  " << x << endl;
#define debr(x, y)                                                             \\
  {                                                                            \\
    cout << "-->  " << #x << " to " << #y << " :  ";                           \\
    auto itr = x;                                                              \\
    while (itr != y)                                                           \\
      cout << (*itr++) << ' ';                                                 \\
    cout << endl;                                                              \\
  }
#define deba(x)                                                                \\
  {                                                                            \\
    cout << "-->  " << #x << " :  ";                                           \\
    for (auto ele : (x))                                                       \\
      cout << ele << "  ";                                                     \\
    cout << endl;                                                              \\
  }

class cmp {
public:
  bool operator()(array<ll, 3> &a, array<ll, 3> &b) { return a[2] > b[2]; }
};

void solve(vector<array<ll, 3>> edges[], pair<ll, ll> change[],
           vector<ll> distance[], ll n, ll maxi, ll s) {
  priority_queue<array<ll, 3>, vector<array<ll, 3>>, cmp> pq; //{vert,coins,min}
  pq.push({0, s, 0});
  {
    ll time = 0;
    while (s + change[0].first < maxi) {
      s += change[0].first;
      time += change[0].second;
      distance[0][s] = time;
      pq.push({0, s, time});
    }
  }
  while (!pq.empty()) {
    auto pa = pq.top();
    pq.pop();
    ll u = pa[0];
    ll silver = pa[1];
    ll time = pa[2];
    for (auto ed : edges[u]) {
      ll v = ed[0];
      ll cost = ed[1];
      ll more = ed[2];
      ll temp_s = silver - cost;
      if (temp_s < 0)
        continue;
      ll temp_t = time + more;
      while (temp_s < maxi) {
        if (temp_t < distance[v][temp_s]) {
          distance[v][temp_s] = temp_t;
          pq.push({v, temp_s, temp_t});
        }
        temp_s += change[v].first;
        temp_t += change[v].second;
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, s;
  cin >> n >> m >> s;
  vector<array<ll, 3>> edges[n];
  ll maxi = 1;
  for (int x = 0; x < m; x++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    maxi += a;
    edges[u].push_back({v, a, b});
    edges[v].push_back({u, a, b});
  }
  pair<ll, ll> change[n];
  for (int x = 0; x < n; x++) {
    cin >> change[x].first >> change[x].second;
  }
  vector<ll> distance[n];
  for (int x = 0; x < n; x++) {
    distance[x].resize(maxi);
    for (int y = 0; y < maxi; y++) {
      distance[x][y] = inf;
    }
  }
  if (s > maxi) {
    s = maxi - 1;
  }
  distance[0][s] = 0;
  solve(edges, change, distance, n, maxi, s);
  for (int x = 1; x < n; x++) {
    ll mini = inf;
    for (ll y = 0; y < maxi; y++) {
      mini = min(mini, distance[x][y]);
    }
    cout << mini << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007ll
#define mod1 998244353ll
#define endl '\\n'
#define inf ((ll)1e18 + 1)
#include <chrono>
using namespace chrono;
mt19937_64 rng(high_resolution_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>
#define deb(x) cout << "-->  " << #x << " :  " << x << endl;
#define debr(x, y)                                                             \\
  {                                                                            \\
    cout << "-->  " << #x << " to " << #y << " :  ";                           \\
    auto itr = x;                                                              \\
    while (itr != y)                                                           \\
      cout << (*itr++) << ' ';                                                 \\
    cout << endl;                                                              \\
  }
#define deba(x)                                                                \\
  {                                                                            \\
    cout << "-->  " << #x << " :  ";                                           \\
    for (auto ele : (x))                                                       \\
      cout << ele << "  ";                                                     \\
    cout << endl;                                                              \\
  }

class cmp {
public:
  bool operator()(array<ll, 3> &a, array<ll, 3> &b) { return a[2] < b[2]; }
};

void solve(vector<array<ll, 3>> edges[], pair<ll, ll> change[],
           vector<ll> distance[], ll n, ll maxi, ll s) {
  priority_queue<array<ll, 3>, vector<array<ll, 3>>, cmp> pq; //{vert,coins,min}
  pq.push({0, s, 0});
  {
    ll time = 0;
    while (s + change[0].first < maxi) {
      s += change[0].first;
      time += change[0].second;
      distance[0][s] = time;
      pq.push({0, s, time});
    }
  }
  while (!pq.empty()) {
    auto pa = pq.top();
    pq.pop();
    ll u = pa[0];
    ll silver = pa[1];
    ll time = pa[2];
    for (auto ed : edges[u]) {
      ll v = ed[0];
      ll cost = ed[1];
      ll more = ed[2];
      ll temp_s = silver - cost;
      if (temp_s < 0)
        continue;
      ll temp_t = time + more;
      while (temp_s < maxi) {
        if (temp_t < distance[v][temp_s]) {
          distance[v][temp_s] = temp_t;
          pq.push({v, temp_s, temp_t});
        }
        temp_s += change[v].first;
        temp_t += change[v].second;
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, s;
  cin >> n >> m >> s;
  vector<array<ll, 3>> edges[n];
  ll maxi = 1;
  for (int x = 0; x < m; x++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    maxi += a;
    edges[u].push_back({v, a, b});
    edges[v].push_back({u, a, b});
  }
  pair<ll, ll> change[n];
  for (int x = 0; x < n; x++) {
    cin >> change[x].first >> change[x].second;
  }
  vector<ll> distance[n];
  for (int x = 0; x < n; x++) {
    distance[x].resize(maxi);
    for (int y = 0; y < maxi; y++) {
      distance[x][y] = inf;
    }
  }
  if (s > maxi) {
    s = maxi - 1;
  }
  distance[0][s] = 0;
  solve(edges, change, distance, n, maxi, s);
  for (int x = 1; x < n; x++) {
    ll mini = inf;
    for (ll y = 0; y < maxi; y++) {
      mini = min(mini, distance[x][y]);
    }
    cout << mini << endl;
  }
  return 0;
}