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

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

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> ill;
typedef tuple<int, int, int> tt;
const int maxN = 50;
const int INF = 1e18;

vector<tt> Adj[maxN];
int d[55][5005];
int n, m, s;

void dijkstra(int s) {

  priority_queue<tt, vector<tt>, greater<tt>> pq;
  for (int i = 1; i < 51; i++) {
    fill(d[i], d[i] + 5005, INF);
  }
  pq.push({0, 1, s});
  int u, cur, du;
  d[1][s] = 0;
  while (!pq.empty()) {
    tt x = pq.top();
    pq.pop();

    tie(du, u, cur) = x;
    if (du != d[u][cur])
      continue;

    for (auto i : Adj[u]) {
      int v, a, b, t;
      tie(v, a, b) = i;

      t = cur - a;
      if (t < 0)
        continue;
      if (t > 5000)
        continue;
      if (du + b < d[v][t]) {
        d[v][t] = du + b;
        pq.push({d[v][t], v, t});
      }
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> s;
  if (s >= 5000)
    s = 5000;
  for (int i = 1; i <= m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    Adj[u].push_back({v, a, b});
    Adj[v].push_back({u, a, b});
  }

  for (int i = 1; i <= n; i++) {
    int c, d;
    cin >> c >> d;
    Adj[i].push_back({i, -c, d});
  }
  dijkstra(s);
  int ans = INF;
  for (int i = 2; i <= n; i++) {
    ans = INF;
    for (int j = 0; j <= 5000; j++)
      ans = min(ans, d[i][j]);
    cout << ans << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
using llong = long long;
using ullong = unsigned long long;
using ulong = unsigned long;
#include <bits/stdc++.h>
#define PROBLEMNAME ""
#define DEBUG1(key, x) cerr << #key ": " << x << endl;
#define DEBUG(x) cerr << #x ": " << x << endl;
#define DEBUG_CONTAINER(x)                                                     \\
  {                                                                            \\
    cerr << #x ":" << endl;                                                    \\
    for (const auto &xx : x)                                                   \\
      cerr << xx << " ";                                                       \\
    cerr << endl;                                                              \\
  }
#define REPORT cerr << "Ok here" << endl;
#ifndef AduMaster
#define endl '\\n'
#undef DEBUG
#undef DEBUG1
#undef DEBUG_CONTAINER
#undef REPORT
#define DEBUG(x)
#define DEBUG1(key, x)
#define REPORT
#define DEBUG_CONTAINER(x)
#endif
using namespace std;
// ifstream fin(PROBLEMNAME".INP");
// ofstream fout(PROBLEMNAME".OUT");
#ifdef AduMaster
// #define cin fin
// #define fout cout
#endif

struct Edge {
  int V, A, B;
};

struct Counter {
  int C, D;
};

const int MaxSilver = 5000 + 1;

vector<vector<llong>> Dijkstra(int S, const vector<vector<Edge>> &Adj,
                               const vector<Counter> &Counters) {
  S = min(S, MaxSilver);

  const int N = Adj.size() - 1;

  vector<vector<llong>> DP(N + 1, vector<llong>(MaxSilver, LLONG_MAX));

  struct Record {
    llong Time;
    int Position;
    llong Money;

    bool operator>(const Record &r) const {
      return (Time != r.Time)
                 ? Time > r.Time
                 : ((Position != r.Position) ? Position > r.Position
                                             : Money > r.Money);
    }
  };

  priority_queue<Record, vector<Record>, greater<Record>> Q;

  DP[1][S] = 0;
  Q.push({0, 1, S});

  while (Q.size()) {
    auto [time, position, money] = Q.top();
    Q.pop();

    if (time > DP[position][money])
      continue;

    DEBUG(time);
    DEBUG(position);
    DEBUG(money);

    REPORT;

    if (money + Counters[position].C < MaxSilver) {
      llong newMoney = money + Counters[position].C;
      llong newTime = time + Counters[position].D;

      if (DP[position][newMoney] > newTime) {
        DP[position][newMoney] = newTime;
        Q.push({newTime, position, newMoney});
      }
    }

    REPORT;

    for (const auto &x : Adj[position]) {
      if (money < x.A)
        continue;
      int newPos = x.V;
      int newMoney = money - x.A;
      int newTime = time + x.B;

      if (DP[newPos][newMoney] > newTime) {
        DP[newPos][newMoney] = newTime;
        Q.push({newTime, newPos, newMoney});
      }
    }

    REPORT;
  }

  return DP;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N, M, S;
  cin >> N >> M >> S;

  vector<vector<Edge>> Adj(N + 1);
  while (M--) {
    static int U, V, A, B;
    cin >> U >> V >> A >> B;
    Adj[U].push_back({V, A, B});
    Adj[V].push_back({U, A, B});
  }

  vector<Counter> Counters(N + 1);
  for (int i = 1; i <= N; ++i) {
    cin >> Counters[i].C >> Counters[i].D;
  }

  REPORT;

  vector<vector<llong>> Result = Dijkstra(S, Adj, Counters);

  REPORT;

  for (int v = 2; v <= N; ++v) {
    llong result = *min_element(Result[v].begin(), Result[v].end());
    cout << result << endl;
  }

  return 0;
}