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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

const ll INF = 1e15;
ll n, m, s, u, v, a, b, dist[55][2505];
vector<pii> adj[55];
priority_queue<pii, vector<pii>, greater<pii>> pq;

int main() {
  fastio;
  cin >> n >> m >> s;
  s = min(s, 50 * (n - 1));
  FOR(i, 1, m) {
    cin >> u >> v >> a >> b;
    adj[u].pb(pii(b, pi(-a, v)));
    adj[v].pb(pii(b, pi(-a, u)));
  }
  FOR(i, 1, n) {
    cin >> a >> b;
    adj[i].pb(pii(b, pi(a, i)));
  }
  memset(dist, -1, sizeof dist);
  pq.push(pii(0, pi(s, 1)));
  dist[1][s] = 0;
  while (!pq.empty()) {
    pii cur = pq.top();
    pq.pop();
    for (auto it : adj[cur.s.s]) {
      ll to = it.s.s, nc = cur.s.f + it.s.f, nt = cur.f + it.f;
      nc = min(nc, 50 * (n - 1));
      if (nc < 0)
        continue;
      if (dist[to][nc] == -1 or dist[to][nc] > nt) {
        dist[to][nc] = nt;
        pq.push(pii(nt, pi(nc, to)));
      }
    }
  }
  FOR(i, 2, n) {
    ll ans = INF;
    FOR(j, 0, 50 * (n - 1)) if (dist[i][j] != -1) ans = min(ans, dist[i][j]);
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

const ll INF = 1e17;
ll n, m, s, u, v, a, b, dist[55][2505];
vector<pii> adj[55];
priority_queue<pii, vector<pii>, greater<pii>> pq;

int main() {
  fastio;
  cin >> n >> m >> s;
  s = min(s, 50 * (n - 1));
  FOR(i, 1, m) {
    cin >> u >> v >> a >> b;
    adj[u].pb(pii(v, pi(-a, b)));
    adj[v].pb(pii(u, pi(-a, b)));
  }
  FOR(i, 1, n) {
    cin >> a >> b;
    adj[i].pb(pii(i, pi(a, b)));
  }
  memset(dist, -1, sizeof dist);
  pq.push(pii(1, pi(s, 0)));
  dist[1][s] = 0;
  while (!pq.empty()) {
    pii cur = pq.top();
    pq.pop();
    for (auto it : adj[cur.f]) {
      ll to = it.f, nc = cur.s.f + it.s.f, nt = cur.s.s + it.s.s;
      nc = min(nc, 50 * (n - 1));
      if (nc < 0)
        continue;
      if (dist[to][nc] == -1 or dist[to][nc] > nt) {
        dist[to][nc] = nt;
        pq.push(pii(to, pi(nc, nt)));
      }
    }
  }
  FOR(i, 2, n) {
    ll ans = INF;
    FOR(j, 0, 50 * (n - 1)) if (dist[i][j] != -1) ans = min(ans, dist[i][j]);
    cout << ans << "\\n";
  }
}