Problem p02703 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> lll;

const ll MOD = 1000000007;
const ll INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);

#define FOR(i, a, b) for (long long i = (long long)(a); i < (long long)(b); i++)
#define RFOR(i, a, b)                                                          \\
  for (long long i = (long long)(a); i >= (long long)(b); i--)
#define ull unsigned long long
#define deb(x) cerr << #x << " = " << x << endl;
#define endl "\\n"
#define coud(a, d) cout << fixed << showpoint << setprecision(d) << a;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define fill(x, y) memset(x, y, sizeof(y))
#define all(x) (x).begin(), (x).end()
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define verify(x)                                                              \\
  if (x >= MOD)                                                                \\
    x -= MOD;

inline void debug_vi(vi a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void debug_vll(vll a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void print_case(int tn) { cout << "Case #" << tn << ": "; }

template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;

template <typename T> using maxpq = priority_queue<T>;
/*----------------------------------------------------------------------*/

const int nax = 51;
int n, m, s;
ll dp[nax]
     [5100]; // dp[i][j]: min time to get to vertex i having j coins in hand.
vector<iii> g[nax]; // nxt, {coins, minutes}
int esum = 0;
vii coincounter; // goldtosilver, minutes

void solve() {
  minpq<pair<ll, ii>> pq; // minutes, vertex and coins in hand.
  FOR(boughttimes, 0, esum + 1) {
    ll starttime = 1LL * boughttimes * coincounter[0].ss;
    ll startingcoins = min(esum * 1LL, boughttimes * coincounter[0].ff + s);

    dp[0][startingcoins] = starttime;
    pq.push({starttime, {0, startingcoins}});

    if (boughttimes * 1LL * coincounter[0].ff + s > esum)
      break;
  }

  while (!pq.empty()) {
    auto curr = pq.top();
    pq.pop();
    ll mins = curr.ff;
    int v, silvers;
    tie(v, silvers) = curr.ss;

    if (dp[v][silvers] < mins)
      continue;
    for (auto nxt : g[v]) {
      int nxtv = nxt.ff;
      int coins, timecost;
      tie(coins, timecost) = nxt.ss;

      FOR(boughttimes, 0, esum + 1) {
        ll buyingtime = 1LL * boughttimes * coincounter[v].second;
        ll totalsilver = silvers + 1LL * boughttimes * coincounter[v].first;
        if (totalsilver < coins)
          continue;
        ll nxtsilver = min(1LL * esum, totalsilver - coins);

        ll newdist = dp[v][silvers] + buyingtime + timecost;
        if (dp[nxtv][nxtsilver] > newdist) {
          dp[nxtv][nxtsilver] = newdist;
          pq.push({newdist, {nxtv, nxtsilver}});
        }

        if (totalsilver - coins > esum)
          break;
      }
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n >> m >> s;
  int u, v, a, b;
  FOR(i, 0, m) {
    cin >> u >> v >> a >> b;
    u--;
    v--;
    g[u].push_back({v, {a, b}});
    g[v].push_back({u, {a, b}});
    esum += a;
  }
  coincounter.resize(n);
  FOR(i, 0, n) { cin >> coincounter[i].ff >> coincounter[i].ss; }
  FOR(i, 0, n) {
    FOR(j, 0, esum + 1) { dp[i][j] = (1e18 + 1); }
  }
  solve();
  FOR(i, 1, n) {
    ll res = LONG_LONG_MAX;
    FOR(j, 0, esum + 1) { res = min(res, dp[i][j]); }
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#pragma GCC optimize(2)
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int N = 55;
const int M = 2605;
const int inf = 0x3f3f3f3f;
const int mod = 300;
const double eps = 1e-9;
const long double pi = acos(-1.0L);
#define ls (i << 1)
#define rs (i << 1 | 1)
#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define mem(a, b) memset(a, b, sizeof(a))
LL read() {
  LL x = 0, t = 1;
  char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      t = -1;
  while (isdigit(ch)) {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * t;
}
struct edge {
  int t, c, v;
  edge() {}
  edge(int V, int T, int C) {
    v = V;
    t = T;
    c = C;
  }
};
struct node {
  int id, co, disx;
  node() {}
  node(int T, int C, int D) {
    id = T;
    co = C;
    disx = D;
  }
  friend bool operator<(node x, node y) { return x.disx > y.disx; }
};
int vis[N][M], dis[N][M], n, m, s;
priority_queue<node, vector<node>, less<node>> q;
vector<edge> e[N];
void dijk() {
  mem(dis, 0x3f);
  dis[1][s] = 0;
  q.push(node(1, s, 0));
  while (!q.empty()) {
    node u = q.top();
    q.pop();
    if (vis[u.id][u.co])
      continue;
    vis[u.id][u.co] = 1;
    for (auto x : e[u.id]) {
      // puts("...");
      if (x.c + u.co >= 0 && x.c + u.co <= 2600 &&
          dis[x.v][x.c + u.co] > dis[u.id][u.co] + x.t) {
        // printf("-- %d %d\\n",x.v,x.c+u.co);
        dis[x.v][x.c + u.co] = dis[u.id][u.co] + x.t;
        q.push(node(x.v, x.c + u.co, dis[x.v][x.c + u.co]));
      }
    }
  }
}
int main() {
  n = read(), m = read(), s = read();
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read(), c = read(), t = read();
    e[u].pb(edge(v, t, -c));
    e[v].pb(edge(u, t, -c));
  }
  for (int i = 1; i <= n; i++) {
    int c = read(), t = read();
    e[i].pb(edge(i, t, c));
  }
  dijk();
  for (int i = 2; i <= n; i++) {
    int mi = inf;
    for (int j = 0; j <= 2500; j++)
      mi = min(dis[i][j], mi);
    printf("%d\\n", mi);
  }
  return 0;
}