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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;

typedef tree<pair<long long, int>, null_type, less<pair<long long, int>>,
             rb_tree_tag, tree_order_statistics_node_update>
    ordered_set;

#define f first
#define se second
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pi pair<int, int>
#define all(num) num.begin(), num.end()
#define lp(i, nodess) for (int i = 0; i < nodess; i++)
#define mem(u, vis) memset(u, vis, sizeof(u))
#define Y imag()
#define angle(u) (atan2(u.Y, u.X))
#define vec(u, v) ((v) - (u))
// #define length(u) (hypot(u.Y , u.X))
#define normalize(u) (u) / length(u)
#define dotP(u, v) ((conj(u) * v).X)
#define crossP(u, v) ((conj(u) * v).Y)
#define same(p1, p2) (dotP(vec(p1, p2), vec(p1, p2)) < EPS)
#define lengthSqr(u) dotP(u, u)
#define rotate0(p, ang) ((p)*exp(point(0, ang)))
#define rotateA(p, ang, about) (rotate0(vec(about, p), ang) + about)
#define reflect0(vis, m) conj(vis / m) * m
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
const double PI = acos(-1.0);
const double EPS = (1e-4);
const long long int INF = 0x3f3f3f3f;
long long mod = 1e9 + 7;
typedef long long int ll;
typedef unsigned long long ull;
typedef complex<double> point;
long long fastpow(ll nodess, ll num, ll m) {
  if (!num)
    return 1;
  if (num % 2)
    return nodess * fastpow(nodess, num - 1, m) % m;
  ll p = fastpow(nodess, num / 2, m);
  p %= m;
  p *= p;
  p %= m;
  return p;
}

ll my_mod(ll num, ll mod) {
  ll reto = num % mod;
  while (reto < 0)
    reto += mod;
  return reto;
}

ll gcd(ll u, ll v) { return !u ? v : gcd(v % u, u); }
/*long long dist(pair<ll, ll> u, pair<ll, ll> v) {
  return
      abs(u.f - v.f)  + abs(u.se - v.se);
}*/
long long getDist(pair<ll, ll> u, pair<ll, ll> v) {
  return ceil(sqrt((u.f - v.f) * (u.f - v.f) + (u.se - v.se) * (u.se - v.se)));
}

// vector<int> ap;
/*
int np[MX+5];

void s(){
    mem(np , 1);
    np[1] = np[0] = 0;
    for(ll i = 2 ; i*i<= MX ;i++){
        if(np[i]){
            for(ll j= i*i ; j <=MX; j+=i) np[j] = 0;
        }
    }
    int idx = 1;
    for(ll i = 2 ; i<= MX ; i++){
        if(np[i]){
            np[i] = idx;
            idx++;
            ap.pb(i);
        }
    }
    //cout<<idx;
}*/

#define double long double
#define int ll

/*ll fact[MX], ifact[MX], iN[MX], dp[MX];
ll ncr(ll n, ll r){
        if(n < r) return 0;
    ll ret = fact[n];
    ret *= ifact[r];
    ret %= mod;
    ret *= ifact[n-r];
    return ret%mod;
}*/
/*
ll cat(ll n){
        if(~dp[n]) return dp[n];
        ll ret = fact[2*n];
        ret *= ifact[n];
        ret %= mod;
        ret *= ifact[n];
        ret %= mod;
        ret *= fastpow(n+1, mod-2, mod);
        return  dp[n] =  ret %mod;
}
const int P1=31;
const int MOD1=1e9+7;
struct Hashs
{
        vector<int> hashs;
        vector<int> pows;
        int P;
        int MOD;

        Hashs() {}

        Hashs(string &s, int P, int MOD) : P(P), MOD(MOD)
        {
                int n = s.size();
                pows.resize(n+1, 0);
                hashs.resize(n+1, 0);
                pows[0] = 1;
                for(int i=n-1;i>=0;i--)
                {
                        hashs[i]=(1LL * hashs[i+1] * P + s[i] - 'a' + 1) % MOD;
                        pows[n-i]=(1LL * pows[n-i-1] * P) % MOD;
                }
                pows[n] = (1LL * pows[n-1] * P)%MOD;
        }
        int get_hash(int l, int r)
        {
                if(l > r) {
                        cout<<l<<"**"<<r<<"\\n";
                        swap(l,r);
                }
                int ans=hashs[l] + MOD - (1LL*hashs[r+1]*pows[r-l+1])%MOD;
                ans%=MOD;
                return ans;
        }
};
*/
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
bool isp(string s) {
  int n = s.size();
  for (int i = 0; i < n / 2; i++) {
    if (s[i] != s[n - 1 - i])
      return 0;
  }
  return 1;
}

const int MX = 55;
class edge {
public:
  int from, to, a, b;
  edge(int from, int to, int a, int b) : from(from), to(to), a(a), b(b) {}
};
vector<edge> adj[MX];
vector<pair<int, int>> arr;
int32_t main() {
  IO;
  clock_t z = clock();
  int n, m, s;
  cin >> n >> m >> s;
  arr.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    adj[u].pb(edge(u, v, a, b));
    adj[v].pb(edge(v, u, a, b));
  }
  for (int i = 1; i <= n; i++)
    cin >> arr[i].f >> arr[i].se;

  vector<vector<int>> dd(51, vector<int>(2502, 2e18));
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  s = min(s, 2500 * 1ll);
  dd[1][s] = 0;
  pq.push({0, {s, 1}});
  while (pq.size() and (clock() - z) <= CLOCKS_PER_SEC * 1.8) {
    auto curr = pq.top();
    pq.pop();
    int cst = curr.f;
    int u = curr.se.se;
    int coins = curr.se.f;
    coins = min(coins, 2500 * 1ll);
    for (int wc = 0, cnt = 0;; wc++) {
      if (wc * arr[u].f + coins > 2500) {
        cnt++;
        if (cnt == 2)
          break;
      }
      int ncst = cst + wc * arr[u].se;
      int ncoins = wc * arr[u].f + coins;
      for (auto it : adj[u]) {
        if (ncoins >= it.a) {
          int nc = ncoins - it.a;
          nc = min(nc, 2500 * 1ll);
          if (ncst + it.b < dd[it.to][nc]) {
            dd[it.to][nc] = ncst + it.b;
            pq.push({ncst + it.b, {nc, it.to}});
          }
        }
      }
    }
  }

  for (int i = 2; i <= n; i++)
    cout << *min_element(all(dd[i])) << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;

typedef tree<pair<long long, int>, null_type, less<pair<long long, int>>,
             rb_tree_tag, tree_order_statistics_node_update>
    ordered_set;

#define f first
#define se second
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pi pair<int, int>
#define all(num) num.begin(), num.end()
#define lp(i, nodess) for (int i = 0; i < nodess; i++)
#define mem(u, vis) memset(u, vis, sizeof(u))
#define Y imag()
#define angle(u) (atan2(u.Y, u.X))
#define vec(u, v) ((v) - (u))
// #define length(u) (hypot(u.Y , u.X))
#define normalize(u) (u) / length(u)
#define dotP(u, v) ((conj(u) * v).X)
#define crossP(u, v) ((conj(u) * v).Y)
#define same(p1, p2) (dotP(vec(p1, p2), vec(p1, p2)) < EPS)
#define lengthSqr(u) dotP(u, u)
#define rotate0(p, ang) ((p)*exp(point(0, ang)))
#define rotateA(p, ang, about) (rotate0(vec(about, p), ang) + about)
#define reflect0(vis, m) conj(vis / m) * m
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
const double PI = acos(-1.0);
const double EPS = (1e-4);
const long long int INF = 0x3f3f3f3f;
long long mod = 1e9 + 7;
typedef long long int ll;
typedef unsigned long long ull;
typedef complex<double> point;
long long fastpow(ll nodess, ll num, ll m) {
  if (!num)
    return 1;
  if (num % 2)
    return nodess * fastpow(nodess, num - 1, m) % m;
  ll p = fastpow(nodess, num / 2, m);
  p %= m;
  p *= p;
  p %= m;
  return p;
}

ll my_mod(ll num, ll mod) {
  ll reto = num % mod;
  while (reto < 0)
    reto += mod;
  return reto;
}

ll gcd(ll u, ll v) { return !u ? v : gcd(v % u, u); }
/*long long dist(pair<ll, ll> u, pair<ll, ll> v) {
  return
      abs(u.f - v.f)  + abs(u.se - v.se);
}*/
long long getDist(pair<ll, ll> u, pair<ll, ll> v) {
  return ceil(sqrt((u.f - v.f) * (u.f - v.f) + (u.se - v.se) * (u.se - v.se)));
}

// vector<int> ap;
/*
int np[MX+5];

void s(){
    mem(np , 1);
    np[1] = np[0] = 0;
    for(ll i = 2 ; i*i<= MX ;i++){
        if(np[i]){
            for(ll j= i*i ; j <=MX; j+=i) np[j] = 0;
        }
    }
    int idx = 1;
    for(ll i = 2 ; i<= MX ; i++){
        if(np[i]){
            np[i] = idx;
            idx++;
            ap.pb(i);
        }
    }
    //cout<<idx;
}*/

#define double long double
#define int ll

/*ll fact[MX], ifact[MX], iN[MX], dp[MX];
ll ncr(ll n, ll r){
        if(n < r) return 0;
    ll ret = fact[n];
    ret *= ifact[r];
    ret %= mod;
    ret *= ifact[n-r];
    return ret%mod;
}*/
/*
ll cat(ll n){
        if(~dp[n]) return dp[n];
        ll ret = fact[2*n];
        ret *= ifact[n];
        ret %= mod;
        ret *= ifact[n];
        ret %= mod;
        ret *= fastpow(n+1, mod-2, mod);
        return  dp[n] =  ret %mod;
}
const int P1=31;
const int MOD1=1e9+7;
struct Hashs
{
        vector<int> hashs;
        vector<int> pows;
        int P;
        int MOD;

        Hashs() {}

        Hashs(string &s, int P, int MOD) : P(P), MOD(MOD)
        {
                int n = s.size();
                pows.resize(n+1, 0);
                hashs.resize(n+1, 0);
                pows[0] = 1;
                for(int i=n-1;i>=0;i--)
                {
                        hashs[i]=(1LL * hashs[i+1] * P + s[i] - 'a' + 1) % MOD;
                        pows[n-i]=(1LL * pows[n-i-1] * P) % MOD;
                }
                pows[n] = (1LL * pows[n-1] * P)%MOD;
        }
        int get_hash(int l, int r)
        {
                if(l > r) {
                        cout<<l<<"**"<<r<<"\\n";
                        swap(l,r);
                }
                int ans=hashs[l] + MOD - (1LL*hashs[r+1]*pows[r-l+1])%MOD;
                ans%=MOD;
                return ans;
        }
};
*/
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
bool isp(string s) {
  int n = s.size();
  for (int i = 0; i < n / 2; i++) {
    if (s[i] != s[n - 1 - i])
      return 0;
  }
  return 1;
}

const int MX = 55;
class edge {
public:
  int from, to, a, b;
  edge(int from, int to, int a, int b) : from(from), to(to), a(a), b(b) {}
};
vector<edge> adj[MX];
vector<pair<int, int>> arr;
int32_t main() {
  IO;
  int n, m, s;
  cin >> n >> m >> s;
  arr.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    adj[u].pb(edge(u, v, a, b));
    adj[v].pb(edge(v, u, a, b));
  }
  for (int i = 1; i <= n; i++)
    cin >> arr[i].f >> arr[i].se;
  vector<vector<int>> dd(51, vector<int>(2502, 2e18));
  for (int tst = 2; tst <= n; tst++) {
    int bst = 2e18;
    for (int j = 1; j <= n; j++)
      for (int k = 0; k <= 2501; k++)
        dd[j][k] = 2e18;
    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                   greater<pair<int, pair<int, int>>>>
        pq;
    s = min(s, 2500 * 1ll);
    dd[1][s] = 0;
    for (int i = 1; i <= s; i++)
      dd[1][i] = 0;
    pq.push({0, {s, 1}});

    while (pq.size()) {
      auto curr = pq.top();
      pq.pop();
      int cst = curr.f;
      int u = curr.se.se;
      int coins = curr.se.f;
      if (cst > bst)
        continue;
      coins = min(coins, 2500 * 1ll);
      // cout<<u<<" "<<cst<<" "<<coins<<"\\n";
      for (int wc = 0, cnt = 0;; wc++) {
        if (wc * arr[u].f + coins > 2500) {
          cnt++;
          if (cnt == 2)
            break;
        }
        int ncst = cst + wc * arr[u].se;
        int ncoins = wc * arr[u].f + coins;
        for (auto it : adj[u]) {
          if (ncoins >= it.a) {
            int nc = ncoins - it.a;
            nc = min(nc, 2500 * 1ll);
            if (ncst + it.b < dd[it.to][nc]) {
              dd[it.to][nc] = ncst + it.b;
              if (it.to == tst) {
                bst = min(bst, ncst + it.b);
              } else if (ncst + it.b < bst)
                pq.push({ncst + it.b, {nc, it.to}});
            }
          }
        }
      }
    }
    cout << bst << "\\n";
  }
  // for(int i = 2; i<= n; i++) cout<<*min_element(all(dd[i]))<<"\\n";
  return 0;
}