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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int mod = (int)1e9 + 7;
const int MX = (int)1e5 + 10;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}
void mul(int &x, int y) { x = x * 1LL * y % mod; }

int main() {

  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);

  int n, m, s;
  cin >> n >> m >> s;

  vector<array<int, 3>> g[n];

  int mx = 0;
  for (int i = 0; i < m; ++i) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    --u, --v;
    g[u].push_back({v, a, b});
    g[v].push_back({u, a, b});
    mx += a;
  }

  vector<pair<int, int>> f(n);
  for (auto &p : f) {
    cin >> p.X >> p.Y; // c, d
  }

  s = min(s, mx);
  vector<vector<ll>> best(n, vector<ll>(mx + 1, 9e18));
  priority_queue<array<ll, 3>> q;
  q.push({0, 0, s});
  best[0][s] = 0;

  while (!q.empty()) {
    auto ar = q.top();
    q.pop();
    ll timer = -ar[0];
    int x = ar[1];
    int silver = ar[2];

    if (best[x][silver] != timer)
      continue;

    // cout << x << " " << silver << endl;

    int t = 1;
    if (silver + t * f[x].X <= mx) {
      if (best[x][silver + t * f[x].X] > best[x][silver] + t * f[x].Y) {
        best[x][silver + t * f[x].X] = best[x][silver] + t * f[x].Y;
        ll cost = best[x][silver] + t * f[x].Y;
        q.push({-cost, x, silver + t * f[x].X});
      }
      t++;
    }

    for (auto &p : g[x]) {
      int y = p[0];
      int a = p[1];
      int b = p[2];
      if (silver >= a && best[y][silver - a] > best[x][silver] + b) {
        best[y][silver - a] = best[x][silver] + b;
        ll cost = best[x][silver] + b;
        q.push({-cost, y, silver - a});
      }
    }
  }

  for (int i = 1; i < n; ++i) {
    cout << *min_element(all(best[i])) << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int mod = (int)1e9 + 7;
const int MX = (int)1e5 + 10;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}
void mul(int &x, int y) { x = x * 1LL * y % mod; }

int main() {

  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);

  int n, m, s;
  cin >> n >> m >> s;

  vector<array<int, 3>> g[n];

  int mx = 0;
  for (int i = 0; i < m; ++i) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    --u, --v;
    g[u].push_back({v, a, b});
    g[v].push_back({u, a, b});
    mx += a;
  }

  vector<pair<int, int>> f(n);
  for (auto &p : f) {
    cin >> p.X >> p.Y; // c, d
  }

  s = min(s, mx);
  vector<vector<ll>> best(n, vector<ll>(2555, 9e18));
  queue<array<int, 2>> q;
  q.push({0, s});
  best[0][s] = 0;

  while (!q.empty()) {
    auto ar = q.front();
    q.pop();
    int x = ar[0];
    int silver = ar[1];

    // cout << x << " " << silver << endl;

    int t = 1;
    while (silver + t * f[x].X <= mx) {
      if (best[x][silver + t * f[x].X] > best[x][silver] + t * f[x].Y) {
        best[x][silver + t * f[x].X] = best[x][silver] + t * f[x].Y;
        q.push({x, silver + t * f[x].X});
      }
      t++;
    }

    for (auto &p : g[x]) {
      int y = p[0];
      int a = p[1];
      int b = p[2];
      if (silver >= a && best[y][silver - a] > best[x][silver] + b) {
        best[y][silver - a] = best[x][silver] + b;
        q.push({y, silver - a});
      }
    }
  }

  for (int i = 1; i < n; ++i) {
    cout << *min_element(all(best[i])) << "\\n";
  }

  return 0;
}