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

#include <bits/stdc++.h>
#define db(x) cout << (x) << '\\n';
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<double, ll> pdi;
typedef pair<string, ll> psi;
typedef pair<ll, string> pls;
typedef pair<ll, pii> pip;
typedef pair<pii, pii> ppp;
#define MAXN 55
#define oo ((ll)(1e18))

vector<pip> g[MAXN];
ll d[MAXN][MAXN * MAXN], ci[MAXN], di[MAXN], res[MAXN];
bool mk[MAXN][MAXN * MAXN];

void dijkstra(ll start, ll s) {
  for (int i = 0; i < MAXN; i++) {
    res[i] = oo;
    for (int j = 0; j < MAXN * MAXN; j++)
      d[i][j] = oo;
  }
  s = min(s, MAXN * MAXN - 1ll);
  d[start][s] = 0;
  priority_queue<pip, vector<pip>, greater<pip>> pq;
  pq.push(pip(0, pii(start, s)));
  while (!pq.empty()) {
    ll w0 = pq.top().first;
    ll u = pq.top().second.first;
    ll silver = pq.top().second.second;
    pq.pop();

    if (mk[u][silver])
      continue;
    mk[u][silver] = 1;
    res[u] = min(res[u], w0);
    d[u][silver] = w0;
    ll ss = silver;
    while (silver != MAXN * MAXN - 1) {
      silver = min(silver + ci[u], MAXN * MAXN - 1ll);
      w0 += di[u];
      if (w0 < d[u][silver]) {
        d[u][silver] = w0;
        pq.push(pip(w0, pii(u, silver)));
      } else
        break;
    }

    silver = ss;
    for (auto y : g[u]) {
      ll v = y.first;
      ll a = y.second.first;
      ll b = y.second.second;
      if (silver >= a && !mk[v][silver - a] &&
          d[v][silver - a] > d[u][silver] + b) {
        d[v][silver - a] = d[u][silver] + b;
        pq.push(pip(d[u][silver] + b, pii(v, silver - a)));
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, m, s, u, v, a, b;
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> a >> b;
    u--, v--;
    g[u].push_back(pip(v, pii(a, b)));
    g[v].push_back(pip(u, pii(a, b)));
  }

  for (int i = 0; i < n; i++)
    cin >> ci[i] >> di[i];

  dijkstra(0, s);

  for (int i = 1; i < n; i++)
    db(res[i])

        return 0;
}
/**
3 2 1
1 2 1 2
1 3 2 4
1 11
1 2
2 5

6 5 1
1 2 1 1
1 3 2 1
2 4 5 1
3 5 11 1
1 6 50 1
1 10000
1 3000
1 700
1 100
1 1
100 1

4 6 10000000
1 2 50 1
1 3 50 5
1 4 50 7
2 3 50 2
2 4 50 4
3 4 50 3
10 2
4 4
5 5
7 7
**/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define db(x) cout << (x) << '\\n';
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<double, ll> pdi;
typedef pair<string, ll> psi;
typedef pair<ll, string> pls;
typedef pair<ll, pii> pip;
typedef pair<pii, pii> ppp;
#define MAXN 55
#define oo ((ll)(1e18))

vector<pip> g[MAXN];
ll d[MAXN][MAXN * MAXN], ci[MAXN], di[MAXN], res[MAXN];
bool mk[MAXN][MAXN * MAXN];

void dijkstra(ll start, ll s) {
  for (int i = 0; i < MAXN; i++) {
    res[i] = oo;
    for (int j = 0; j < MAXN * MAXN; j++)
      d[i][j] = oo;
  }
  s = min(s, MAXN * MAXN - 1ll);
  d[start][s] = 0;
  priority_queue<pip, vector<pip>, greater<pip>> pq;
  pq.push(pip(0, pii(start, s)));
  while (!pq.empty()) {
    ll w0 = pq.top().first;
    ll u = pq.top().second.first;
    ll silver = pq.top().second.second;
    pq.pop();

    if (mk[u][silver])
      continue;
    mk[u][silver] = 1;
    res[u] = min(res[u], w0);
    d[u][silver] = w0;
    ll ss = silver;
    while (silver != MAXN * MAXN - 1) {
      silver = min(silver + ci[u], MAXN * MAXN - 1ll);
      w0 += di[u];
      if (w0 < d[u][silver]) {
        d[u][silver] = w0;
        //                if(silver<10)
        //                {
        //                    cout << u << ' ' << w0 << '\\n';
        //                }
        pq.push(pip(w0, pii(u, silver)));
      } else
        break;
    }

    silver = ss;
    for (auto y : g[u]) {
      ll v = y.first;
      ll a = y.second.first;
      ll b = y.second.second;
      if (silver >= a && !mk[v][silver - a]) {
        //                if(silver-a<10)
        //                    cout << d[u][silver]+b << ' ' << v << ' ' <<
        //                    silver-a << '\\n';
        pq.push(pip(d[u][silver] + b, pii(v, silver - a)));
      }
    }
  }
}

int main() {
  //    ios_base::sync_with_stdio(0);
  //    cin.tie(0);
  //    cout.tie(0);

  ll n, m, s, u, v, a, b;
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> a >> b;
    u--, v--;
    g[u].push_back(pip(v, pii(a, b)));
    g[v].push_back(pip(u, pii(a, b)));
  }

  for (int i = 0; i < n; i++)
    cin >> ci[i] >> di[i];

  dijkstra(0, s);

  for (int i = 1; i < n; i++)
    db(res[i])

        return 0;
}
/**
3 2 1
1 2 1 2
1 3 2 4
1 11
1 2
2 5

6 5 1
1 2 1 1
1 3 2 1
2 4 5 1
3 5 11 1
1 6 50 1
1 10000
1 3000
1 700
1 100
1 1
100 1

4 6 10000000
1 2 50 1
1 3 50 5
1 4 50 7
2 3 50 2
2 4 50 4
3 4 50 3
10 2
4 4
5 5
7 7
**/