Problem p02703 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define endl '\n'
const int inf = 1000000007;
const ll INF = 1e18;
// int mod = 998244353;
int mod = 1000000007;
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll pow(ll a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
ll modpow(ll a, ll b, ll _mod) {
  return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.F << " " << p.S;
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  rep(i, sz(vec)) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> inline istream &operator>>(istream &is, vector<T> &v) {
  rep(j, sz(v)) is >> v[j];
  return is;
}
template <class T, class T2> inline void add(T &a, T2 b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);

  int T;
  // cin >> T;
  T = 1;

  while (T--) {
    solve();
  }
}

ll dp[55][55 * 55];
vector<ll> C, D;
ll n, m, s;
using t = tuple<ll, ll, int>; // 時間小さく, 銀貨は大きく
vector<vector<t>> G;
using t2 = tuple<int, ll, ll>;
vector<vector<int>> d;
clock_t st, ed;

void dijkstra() {
  priority_queue<t, vector<t>, greater<t>> q;
  q.push(t(0, -s, 0));

  while (!q.empty()) {
    ed = clock();
    if ((ed - st) * 1.0 / CLOCKS_PER_SEC > 2.0) {
      rep3(i, 1, n) {
        ll ans = INF;
        rep(j, 55 * 55) chmin(ans, dp[i][j]);
        cout << ans << endl;
      }
      exit(0);
    }

    int v;
    ll ti, co;
    tie(ti, co, v) = q.top();
    // cout << "v " << ti << " " << co << " " << v << endl;
    co = -co;
    q.pop();

    if (dp[v][co] < ti)
      continue;
    for (t2 nt : G[v]) {
      int nv;
      ll nti, nco;
      tie(nv, nco, nti) = nt;
      // cout << "nv " << nv << " " << nco << " " << nti << endl;
      // 交換
      int flg = 0;
      rep(i, inf) {
        ll co2 = co - nco;
        ll x = 0;
        if (co2 < 0)
          x = (-co2 + C[v] - 1) / C[v];
        co2 += x * C[v];
        co2 += i * C[v];
        if (co2 >= 55 * 55) {
          co2 = 55 * 55 - 1;
          flg = 1;
        } // debuged
        x += i;
        if (chmin(dp[nv][co2], dp[v][co] + nti + x * D[v])) {
          q.push(t(dp[nv][co2], -co2, nv));
        }
        if (flg)
          break;
      }
    }
  }
}

void solve() {
  st = clock();

  cin >> n >> m >> s;
  G.resize(n);

  rep(i, m) {
    int a, b, A, B;
    cin >> a >> b >> A >> B;
    a--;
    b--;
    G[a].eb(b, A, B);
    G[b].eb(a, A, B);
  }
  C.resize(n);
  D.resize(n);
  rep(i, n) cin >> C[i] >> D[i];
  rep(i, 55) rep(j, 55 * 55) dp[i][j] = INF;
  chmin(s, 55 * 55 - 1); // debuged
  dp[0][s] = 0;
  dijkstra();
  rep3(i, 1, n) {
    ll ans = INF;
    rep(j, 55 * 55) chmin(ans, dp[i][j]);
    cout << ans << endl;
  }
  // rep(i, n) { rep(j, 10) cout << (dp[i][j] == INF ? -1LL : dp[i][j]) << " ";
  // cout << endl; }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define endl '\n'
const int inf = 1000000007;
const ll INF = 1e18;
// int mod = 998244353;
int mod = 1000000007;
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll pow(ll a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
ll modpow(ll a, ll b, ll _mod) {
  return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.F << " " << p.S;
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  rep(i, sz(vec)) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> inline istream &operator>>(istream &is, vector<T> &v) {
  rep(j, sz(v)) is >> v[j];
  return is;
}
template <class T, class T2> inline void add(T &a, T2 b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);

  int T;
  // cin >> T;
  T = 1;

  while (T--) {
    solve();
  }
}

ll dp[55][55 * 55];
vector<ll> C, D;
ll n, m, s;
using t = tuple<ll, ll, int>; // 時間小さく, 銀貨は大きく
vector<vector<t>> G;
using t2 = tuple<int, ll, ll>;
vector<vector<int>> d;
clock_t st, ed;

void dijkstra() {
  priority_queue<t, vector<t>, greater<t>> q;
  q.push(t(0, -s, 0));

  while (!q.empty()) {
    ed = clock();
    if ((ed - st) * 1.0 / CLOCKS_PER_SEC > 1.9) {
      rep3(i, 1, n) {
        ll ans = INF;
        rep(j, 55 * 55) chmin(ans, dp[i][j]);
        cout << ans << endl;
      }
      exit(0);
    }

    int v;
    ll ti, co;
    tie(ti, co, v) = q.top();
    // cout << "v " << ti << " " << co << " " << v << endl;
    co = -co;
    q.pop();

    if (dp[v][co] < ti)
      continue;
    for (t2 nt : G[v]) {
      int nv;
      ll nti, nco;
      tie(nv, nco, nti) = nt;
      // cout << "nv " << nv << " " << nco << " " << nti << endl;
      // 交換
      int flg = 0;
      rep(i, inf) {
        ll co2 = co - nco;
        ll x = 0;
        if (co2 < 0)
          x = (-co2 + C[v] - 1) / C[v];
        co2 += x * C[v];
        co2 += i * C[v];
        if (co2 >= 55 * 55) {
          co2 = 55 * 55 - 1;
          flg = 1;
        } // debuged
        x += i;
        if (chmin(dp[nv][co2], dp[v][co] + nti + x * D[v])) {
          q.push(t(dp[nv][co2], -co2, nv));
        }
        if (flg)
          break;
      }
    }
  }
}

void solve() {
  st = clock();

  cin >> n >> m >> s;
  G.resize(n);

  rep(i, m) {
    int a, b, A, B;
    cin >> a >> b >> A >> B;
    a--;
    b--;
    G[a].eb(b, A, B);
    G[b].eb(a, A, B);
  }
  C.resize(n);
  D.resize(n);
  rep(i, n) cin >> C[i] >> D[i];
  rep(i, 55) rep(j, 55 * 55) dp[i][j] = INF;
  chmin(s, 55 * 55 - 1); // debuged
  dp[0][s] = 0;
  dijkstra();
  rep3(i, 1, n) {
    ll ans = INF;
    rep(j, 55 * 55) chmin(ans, dp[i][j]);
    cout << ans << endl;
  }
  // rep(i, n) { rep(j, 10) cout << (dp[i][j] == INF ? -1LL : dp[i][j]) << " ";
  // cout << endl; }
}