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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
struct edge {
  int to, silver, cost;
};
#define PP pair<int, P>
int n, m, s, c[50], d[50], dist[50][2451];
vector<edge> v[50];
void dijkstra() {
  rep(i, 50) {
    rep(j, 2451) { dist[i][j] = INF; }
  }
  priority_queue<PP, vector<PP>, greater<PP>> q;
  q.push({0, {0, s}});
  dist[0][s] = 0;
  while (!q.empty()) {
    PP p = q.top();
    q.pop();
    if (dist[p.se.fi][p.se.se] != p.fi)
      continue;
    for (int i = 0; p.se.se + c[p.se.fi] * i <= 2450; i++) {
      for (edge e : v[p.se.fi]) {
        if (p.se.se + c[p.se.fi] * i - e.silver < 0)
          continue;
        if (dist[e.to][p.se.se + c[p.se.fi] * i - e.silver] >
            p.fi + d[p.se.fi] * i + e.cost) {
          dist[e.to][p.se.se + c[p.se.fi] * i - e.silver] =
              p.fi + d[p.se.fi] * i + e.cost;
          q.push({dist[e.to][p.se.se + c[p.se.fi] * i - e.silver],
                  {e.to, p.se.se + c[p.se.fi] * i - e.silver}});
        }
      }
    }
  }
}
signed main() {
  cin >> n >> m >> s;
  chmin(s, 2450ll);
  rep(i, m) {
    int u, w, a, b;
    cin >> u >> w >> a >> b;
    u--, w--;
    v[u].pb({w, a, b});
    v[w].pb({u, a, b});
  }
  rep(i, n) cin >> c[i] >> d[i];
  dijkstra();
  rep(i, n) {
    if (i) {
      int ans = INF;
      rep(j, 2451) chmin(ans, dist[i][j]);
      cout << ans << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int kaijo[2000010];
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
void init_fact() {
  kaijo[0] = 1;
  for (int i = 1; i <= 2000000; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
int comb(int a, int b) {
  if (!kaijo[0])
    init_fact();
  return kaijo[a] * modpow(kaijo[a - b], mod - 2) % mod *
         modpow(kaijo[b], mod - 2) % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}
bool kosa(double ax, double ay, double bx, double by, double cx, double cy,
          double dx, double dy) {
  double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
  return tc * td < 0 && ta * tb < 0;
}
struct edge {
  int to, silver, cost;
};
#define PP pair<int, P>
int n, m, s, c[50], d[50], dist[50][2451];
vector<edge> v[50];
void dijkstra() {
  rep(i, 50) {
    rep(j, 2451) { dist[i][j] = INF; }
  }
  priority_queue<PP, vector<PP>, greater<PP>> q;
  q.push({0, {0, s}});
  dist[0][s] = 0;
  while (!q.empty()) {
    PP p = q.top();
    q.pop();
    if (dist[p.se.fi][p.se.se] != p.fi)
      continue;
    for (int i = 0; p.se.se + c[p.se.fi] * i <= 2450; i++) {
      for (edge e : v[p.se.fi]) {
        if (p.se.se + c[p.se.fi] * i - e.silver < 0)
          continue;
        if (dist[e.to][p.se.se + c[p.se.fi] * i - e.silver] >
            p.fi + d[p.se.fi] * i + e.cost) {
          dist[e.to][p.se.se + c[p.se.fi] * i - e.silver] =
              p.fi + d[p.se.fi] * i + e.cost;
          q.push({dist[e.to][p.se.se + c[p.se.fi] * i - e.silver],
                  {e.to, p.se.se + c[p.se.fi] * i - e.silver}});
        }
      }
    }
  }
}
signed main() {
  cin >> n >> m >> s;
  chmin(s, 2450ll);
  rep(i, m) {
    int u, w, a, b;
    cin >> u >> w >> a >> b;
    u--, w--;
    v[u].pb({w, a, b});
    v[w].pb({u, a, b});
  }
  rep(i, n) cin >> c[i] >> d[i];
  dijkstra();
  rep(i, n) {
    if (i) {
      int ans = INF;
      rep(j, 2451) chmin(ans, dist[i][j]);
      cout << ans << endl;
    }
  }
  return 0;
}