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

// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #include "boost/multiprecision/cpp_int.hpp"
// typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fi first
#define se second
#define endl "\\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

//////////////////////////

struct edge {
  ll to, a, b;
};

struct NOW {
  l_l z;
  ll time;
  bool operator<(const NOW &opp) const { return time < opp.time; }
  bool operator>(const NOW &opp) const {
    return time > opp.time;
  } // 小さい順に取り出す時に必要
};

int main() {
  fastio ll n, m, s;
  cin >> n >> m >> s;
  vector<edge> v[n + 1];
  ll M = 3;

  rep(i, 1, m) {
    edge e;
    ll u;
    cin >> u >> e.to >> e.a >> e.b;
    v[u].pb(e);
    swap(u, e.to);
    v[u].pb(e);

    M += e.a;
  }
  Min(M, 50 * 53);
  ll c[n + 1], e[n + 1];
  rep(i, 1, n) cin >> c[i] >> e[i];

  s = min(s, M - 1);
  ll d[n + 1][M];
  rep(i, 1, n) rep(j, 0, M - 1) d[i][j] = inf;
  d[1][s] = 0;

  priority_queue<NOW, vector<NOW>, greater<NOW>> q;
  q.push({{1, s}, 0}); // 頂点、硬貨の枚数
  while (!q.empty()) {
    NOW front = q.top();
    q.pop();
    l_l fro = front.z;
    ll t = fro.fi;
    ll mai = fro.se;
    for (ll j = mai + c[t]; j <= M - 1; j += c[t]) {
      if (chmin(d[t][j], d[t][j - c[t]] + e[t])) {
        q.push({{t, j}, d[t][j]});
      }
    }

    for (auto x : v[t]) {
      if (mai - x.a < 0)
        continue;
      if (chmin(d[x.to][mai - x.a], d[t][mai] + x.b)) {
        q.push({{x.to, mai - x.a}, d[x.to][mai - x.a]});
      }
    }
  }
  ll ans[n + 1];
  rep(i, 1, n) ans[i] = inf;
  rep(i, 2, n) {
    rep(j, 0, M - 1) { Min(ans[i], d[i][j]); }
  }
  rep(i, 2, n) cout << ans[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #include "boost/multiprecision/cpp_int.hpp"
// typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fi first
#define se second
#define endl "\\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

//////////////////////////

struct edge {
  ll to, a, b;
};

struct NOW {
  l_l z;
  ll time;
  bool operator<(const NOW &opp) const { return time < opp.time; }
  bool operator>(const NOW &opp) const {
    return time > opp.time;
  } // 小さい順に取り出す時に必要
};

int main() {
  fastio ll n, m, s;
  cin >> n >> m >> s;
  vector<edge> v[n + 1];
  rep(i, 1, m) {
    edge e;
    ll u;
    cin >> u >> e.to >> e.a >> e.b;
    v[u].pb(e);
    swap(u, e.to);
    v[u].pb(e);
  }
  ll M = 50 * 55;
  s = min(s, M - 1);
  ll d[n + 1][M];
  rep(i, 1, n) rep(j, 0, M - 1) d[i][j] = inf;
  d[1][s] = 0;
  ll c[n + 1], e[n + 1];
  rep(i, 1, n) cin >> c[i] >> e[i];
  priority_queue<NOW, vector<NOW>, greater<NOW>> q;
  q.push({{1, s}, 0}); // 頂点、硬貨の枚数
  while (!q.empty()) {
    NOW front = q.top();
    q.pop();
    l_l fro = front.z;
    ll t = fro.fi;
    ll mai = fro.se;
    for (ll j = mai + c[t]; j <= M - 1; j += c[t]) {
      if (chmin(d[t][j], d[t][j - c[t]] + e[t])) {
        q.push({{t, j}, d[t][j]});
      }
    }

    for (auto x : v[t]) {
      if (mai - x.a < 0)
        continue;
      if (chmin(d[x.to][mai - x.a], d[t][mai] + x.b)) {
        q.push({{x.to, mai - x.a}, d[x.to][mai - x.a]});
      }
    }
  }
  ll ans[n + 1];
  rep(i, 1, n) ans[i] = inf;
  rep(i, 2, n) {
    rep(j, 0, M - 1) { Min(ans[i], d[i][j]); }
  }
  rep(i, 2, n) cout << ans[i] << endl;

  return 0;
}