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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

const ll mod = 1000000007;
const double eps = 1e-8;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

const int MAX_COIN = 2501;
const int MAX_V = 50 * 2501;
const ll INF = 1e17;
typedef pair<ll, int> P;

struct Edge {
  int to;
  ll cost;
};
vector<Edge> G[MAX_V];

int main() {
  int N, M, S;
  cin >> N >> M >> S;
  S = min(S, MAX_COIN - 1);
  for (int i = 0; i < M; ++i) {
    int U, V, A;
    ll B;
    cin >> U >> V >> A >> B;
    U--;
    V--;
    for (int j = A; j < MAX_COIN; ++j) {
      G[U * MAX_COIN + j].push_back((Edge){V * MAX_COIN + j - A, B});
      G[V * MAX_COIN + j].push_back((Edge){U * MAX_COIN + j - A, B});
    }
  }
  for (int i = 0; i < N; ++i) {
    ll C, D;
    cin >> C >> D;
    for (int j = 0; j < MAX_COIN; ++j) {
      int CC = min(j + (int)C, MAX_COIN - 1);
      G[i * MAX_COIN + j].push_back((Edge){i * MAX_COIN + CC, D});
    }
  }

  // dijkstra
  vector<ll> d(MAX_V, INF);
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0LL, S));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (d[p.second] <= p.first)
      continue;
    d[p.second] = p.first;
    for (Edge &e : G[p.second]) {
      if (d[e.to] > p.first + e.cost) {
        q.push(P(p.first + e.cost, e.to));
      }
    }
  }

  for (int t = 1; t < N; ++t) {
    ll res = INF;
    for (int j = 0; j < MAX_COIN; ++j) {
      res = min(res, d[t * MAX_COIN + j]);
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

const ll mod = 1000000007;
const double eps = 1e-8;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

const int MAX_COIN = 62501;
const int MAX_V = 50 * 62501;
const ll INF = 1e17;
typedef pair<ll, int> P;

struct Edge {
  int to;
  ll cost;
};
vector<Edge> G[MAX_V];

int main() {
  int N, M, S;
  cin >> N >> M >> S;
  S = min(S, MAX_COIN - 1);
  for (int i = 0; i < M; ++i) {
    int U, V, A;
    ll B;
    cin >> U >> V >> A >> B;
    U--;
    V--;
    for (int j = A; j < MAX_COIN; ++j) {
      G[U * MAX_COIN + j].push_back((Edge){V * MAX_COIN + j - A, B});
      G[V * MAX_COIN + j].push_back((Edge){U * MAX_COIN + j - A, B});
    }
  }
  for (int i = 0; i < N; ++i) {
    ll C, D;
    cin >> C >> D;
    for (int j = 0; j < MAX_COIN; ++j) {
      int CC = min(j + (int)C, MAX_COIN - 1);
      G[i * MAX_COIN + j].push_back((Edge){i * MAX_COIN + CC, D});
    }
  }

  // dijkstra
  vector<ll> d(MAX_V, INF);
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0LL, S));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (d[p.second] <= p.first)
      continue;
    d[p.second] = p.first;
    for (Edge &e : G[p.second]) {
      if (d[e.to] > p.first + e.cost) {
        q.push(P(p.first + e.cost, e.to));
      }
    }
  }

  for (int t = 1; t < N; ++t) {
    ll res = INF;
    for (int j = 0; j < MAX_COIN; ++j) {
      res = min(res, d[t * MAX_COIN + j]);
    }
    cout << res << endl;
  }
  return 0;
}