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

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

#define debug(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define FOR(it, b, e) for (typeof(b) it = (b); it != (e); ++it)
#define MSET(c, v) memset(c, v, sizeof(c))

const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const int NULO = -1;
const double EPS = 1e-10;
const ll mod = 998244353;

inline int cmp(double x, double y = 0) {
  if (fabs(x - y) < EPS)
    return 0;
  return x > y ? 1 : -1;
}

ll exp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}

const int maxn = 100 + 10;
const int maxm = 5000;

int d[maxn], c[maxn];

struct node {
  int v;
  ll a, b;
};

vector<node> graph[maxn];

struct state {
  int no;
  ll t, qts;
  state() {}
  state(int no, ll t, ll qts) : no(no), t(t), qts(qts) {}

  bool operator<(const state &s) const { return t < s.t; }
};

ll dist[maxn][maxm + 10];
bool vis[maxn][maxm];

int main() {
  MSET(dist, INF);
  int n, m, s;
  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    graph[u].push_back({v, a, b});
    graph[v].push_back({u, a, b});
  }

  for (int i = 1; i <= n; i++) {
    cin >> c[i] >> d[i];
  }
  s = min(s, maxm);
  multiset<state> q;
  q.insert(state(1, 0, s));
  dist[1][s] = 0;
  while (q.size()) {
    auto at = *q.begin();
    q.erase(q.begin());
    if (at.t != dist[at.no][at.qts])
      continue;
    if (vis[at.no][at.qts])
      continue;
    vis[at.no][at.qts] = true;
    int u = at.no;
    ll t = at.t;
    ll qts = at.qts;
    // debug(u);
    // debug(t);
    // debug(qts);
    // cout << "\\n\\n\\n";
    for (const auto &e : graph[u]) {
      ll a = e.a;
      ll b = e.b;
      int v = e.v;
      if (qts >= a && dist[v][qts - a] > t + b) {
        dist[v][qts - a] = t + b;
        q.insert(state(v, t + b, qts - a));
      }
    }
    if (t + d[u] < dist[u][min(ll(maxm), qts + c[u])]) {
      dist[u][min(ll(maxm), qts + c[u])] = t + d[u];
      q.insert(state(u, t + d[u], min(ll(maxm), qts + c[u])));
    }
  }

  for (int i = 2; i <= n; i++) {
    cout << *min_element(dist[i], dist[i] + maxm + 2) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define debug(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define FOR(it, b, e) for (typeof(b) it = (b); it != (e); ++it)
#define MSET(c, v) memset(c, v, sizeof(c))

const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const int NULO = -1;
const double EPS = 1e-10;
const ll mod = 998244353;

inline int cmp(double x, double y = 0) {
  if (fabs(x - y) < EPS)
    return 0;
  return x > y ? 1 : -1;
}

ll exp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}

const int maxn = 55;
const int maxm = maxn * maxn;

int d[maxn], c[maxn];

struct node {
  int v;
  int a, b;
};

vector<node> graph[maxn];

struct state {
  int no;
  int t, qts;
  state() {}
  state(int no, ll t, ll qts) : no(no), t(t), qts(qts) {}

  bool operator<(const state &s) const { return t > s.t; }
};

ll dist[maxn][maxm + 10];
bool vis[maxn][maxm];

int main() {
  MSET(dist, INF);
  int n, m, s;
  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    graph[u].push_back({v, a, b});
    graph[v].push_back({u, a, b});
  }

  for (int i = 1; i <= n; i++) {
    cin >> c[i] >> d[i];
  }

  priority_queue<state> q;
  q.push(state(1, 0, s));
  dist[1][s] = 0;
  while (q.size()) {
    auto at = q.top();
    q.pop();
    if (at.t != dist[at.no][at.qts])
      continue;
    auto u = at.no;
    auto t = at.t;
    auto qts = at.qts;
    // debug(u);
    // debug(t);
    // debug(qts);
    // cout << "\\n\\n\\n";
    for (const auto &e : graph[u]) {
      ll a = e.a;
      ll b = e.b;
      int v = e.v;
      if (qts >= a && dist[v][qts - a] > t + b) {
        dist[v][qts - a] = t + b;
        q.push(state(v, t + b, qts - a));
      }
    }
    if (t + d[u] < dist[u][min(maxm, qts + c[u])]) {
      dist[u][min(maxm, qts + c[u])] = t + d[u];
      q.push(state(u, t + d[u], min(maxm, qts + c[u])));
    }
  }

  for (int i = 2; i <= n; i++) {
    cout << *min_element(dist[i], dist[i] + maxm + 1) << endl;
  }

  return 0;
}