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

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}

int main() {
  run();
  int s, w;
  cin >> s >> w;
  cout << (w >= s ? "unsafe" : "safe");
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}
struct edge {
  int from, to;
  ll a, b;
};

struct state {
  int cur;
  ll silver, time;
  int buyIdx;
  state(int cur, ll silver, ll time, int buyIdx)
      : cur(cur), silver(silver), time(time), buyIdx(buyIdx) {}
  bool operator<(const state &o) const { return time > o.time; }
};

const int N = 51;
vector<edge> adj[N];
pair<ll, ll> cost[N];
ll dis[N][N * N + 1][N];
int s;
void dijkstra() {
  memset(dis, INF, sizeof(dis));
  priority_queue<state> q;
  q.push(state(1, s, 0, 1));
  dis[1][s][1] = 0;
  while (sz(q)) {
    int cur = q.top().cur;
    ll silver = q.top().silver;
    ll time = q.top().time;
    int buyIdx = q.top().buyIdx;
    q.pop();
    if (dis[cur][silver][buyIdx] < time)
      continue;
    for (auto ch : adj[cur]) {
      ll nxtTime = (time + ch.b);
      ll nxtSilver = (silver - ch.a);
      if (nxtSilver < 0) {
        ll cntNum = (-nxtSilver + cost[buyIdx].first - 1) / cost[buyIdx].first;
        nxtTime += cntNum * cost[buyIdx].second;
        nxtSilver += cntNum * cost[buyIdx].first;
        nxtSilver = min(nxtSilver, 1LL * N * N);
      }
      if (dis[ch.to][nxtSilver][buyIdx] > nxtTime) {
        dis[ch.to][nxtSilver][buyIdx] = nxtTime;
        q.push(state(ch.to, nxtSilver, nxtTime, buyIdx));
      }
      if (dis[ch.to][nxtSilver][ch.to] > nxtTime) {
        dis[ch.to][nxtSilver][ch.to] = nxtTime;
        q.push(state(ch.to, nxtSilver, nxtTime, ch.to));
      }
    }
  }
}
int main() {
  run();
  int n, m;
  cin >> n >> m >> s;
  s = min(s, N * N);
  while (m--) {
    edge e;
    cin >> e.from >> e.to >> e.a >> e.b;
    adj[e.from].push_back(e);
    swap(e.from, e.to);
    adj[e.from].push_back(e);
  }
  for (int i = 1; i <= n; i++)
    cin >> cost[i].first >> cost[i].second;
  dijkstra();
  for (int i = 2; i <= n; i++) {
    ll mn = INF;
    for (int si = 0; si <= N * N; si++)
      for (int j = 1; j <= n; j++)
        mn = min(mn, dis[i][si][j]);
    cout << mn << "\\n";
  }
}