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

#ifdef __APPLE__
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define tc(t) for (int tc = 1; tc <= t; tc++)
typedef vector<int> VI;
typedef vector<long long int> VLLI;
typedef vector<pair<int, int>> VPI;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll modInverse(ll a) {
  ll p = mod - 2;
  return powmod(a, p);
}
// head

int main() {
  int n, m, s;
  cin >> n >> m >> s;
  int u, v, a, b, amax = 0;
  vector<vector<pair<int, PLL>>> graph(n + 1);
  vector<PLL> ex(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> a >> b;
    amax = std::max(amax, a);
    graph[u].push_back({v, {a, b}});
    graph[v].push_back({u, {a, b}});
  }
  for (int i = 1; i <= n; i++) {
    cin >> u >> v;
    ex[i] = {u, v};
  }

  map<pair<int, int>, ll> dist;
  set<pair<ll, pair<int, int>>> pq;
  set<pair<int, int>> visited;
  s = min(s, amax * n);

  dist[{1, s}] = 0;
  pq.insert({0, {1, s}});

  int count = 0, l = 0, edges = 0;

  while (!pq.empty()) {
    auto it = pq.begin();
    auto top = *it;
    pq.erase(it);
    visited.insert(top.second);

    int vertex = top.se.fi;
    ll coins = top.se.se;
    ll time = top.fi;

    if (coins + ex[vertex].fi < amax * n) {
      PLL state = {vertex, coins + ex[vertex].fi};
      if (visited.find(state) == visited.end() and
          ((dist.find(state) == dist.end()) or
           (dist[state] > time + ex[vertex].se))) {
        pq.erase({dist[state], state});
        dist[state] = time + ex[vertex].se;
        pq.insert({dist[state], state});
      }
    }

    for (auto x : graph[vertex]) {
      if (coins < x.se.fi)
        continue;
      PII state = {x.fi, coins - x.se.fi};
      if ((visited.find(state) == visited.end()) and
          ((dist.find(state) == dist.end()) or
           (dist[state] > time + x.se.se))) {
        pq.erase({dist[state], state});
        dist[state] = time + x.se.se;
        pq.insert({dist[state], state});
      }
    }
  }

  vector<ll> ans(n + 1, LLONG_MAX);
  for (auto x : dist) {
    ans[x.fi.fi] = min(ans[x.fi.fi], x.se);
  }
  for (int i = 2; i <= n; i++)
    cout << ans[i] << "\\n";
}


Error: TLE

Fixed submission:

#ifdef __APPLE__
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define tc(t) for (int tc = 1; tc <= t; tc++)
typedef vector<int> VI;
typedef vector<long long int> VLLI;
typedef vector<pair<int, int>> VPI;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll modInverse(ll a) {
  ll p = mod - 2;
  return powmod(a, p);
}
// head

int main() {
  int n, m, s;
  cin >> n >> m >> s;
  int u, v, a, b, amax = 0;
  vector<vector<pair<int, PII>>> graph(n + 1);
  vector<pair<int, int>> ex(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> a >> b;
    amax = std::max(amax, a);
    graph[u].push_back({v, {a, b}});
    graph[v].push_back({u, {a, b}});
  }
  for (int i = 1; i <= n; i++) {
    cin >> u >> v;
    ex[i] = {u, v};
  }

  map<pair<int, int>, int> dist;
  set<pair<int, pair<int, int>>> pq;
  set<pair<int, int>> visited;
  s = min(s, amax * n);

  int c = s, t = 0;
  while (c < amax * n) {
    dist[{1, c}] = t;
    pq.insert({t, {1, c}});
    c += ex[1].fi;
    t += ex[1].se;
  }

  dist[{1, s}] = 0;
  pq.insert({0, {1, s}});

  while (!pq.empty()) {
    auto it = pq.begin();
    auto top = *it;
    pq.erase(it);
    visited.insert(top.second);

    int vertex = top.se.fi;
    int coins = top.se.se;
    int time = top.fi;
    do {
      for (auto x : graph[vertex]) {
        PII state = {x.fi, coins - x.se.fi};
        if (coins >= x.se.fi and (visited.find(state) == visited.end()) and
            ((dist.find(state) == dist.end()) or
             (dist[state] > time + x.se.se))) {
          pq.erase({dist[state], state});
          dist[state] = time + x.se.se;
          pq.insert({dist[state], state});
        }
      }
      coins += ex[vertex].fi;
      time += ex[vertex].se;
    } while (coins < amax * n);
  }

  vector<int> ans(n + 1, INT_MAX);
  for (auto x : dist) {
    ans[x.fi.fi] = min(ans[x.fi.fi], x.se);
  }
  for (int i = 2; i <= n; i++)
    cout << ans[i] << "\\n";
}