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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 55;
int n, m, tot;
ll dis[maxn][2600], c[maxn], t[maxn];
ll s;
int head[maxn];
struct node {
  int to, next;
  ll w, cost;
} e[maxn * maxn];
void add(int u, int v, ll w, ll c) {
  e[tot].to = v;
  e[tot].w = w;
  e[tot].cost = c;
  e[tot].next = head[u];
  head[u] = tot++;
}
void dijkstra(ll s, ll d[][2600]) {
  queue<pair<int, ll>> q;
  int vis[n + 5][2600];
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 2500; j++)
      dis[i][j] = 0x3f3f3f3f3f3f3f3f, vis[i][j] = 0;
  }
  d[1][s] = 0;
  q.push({1, s});
  while (!q.empty()) {
    int cur = q.front().first;
    ll w = q.front().second;
    q.pop();
    // if(vis[cur][w]) continue;
    // vis[cur][w] = 1;
    for (int i = head[cur]; ~i; i = e[i].next) {
      int y = e[i].to;
      ll a = e[i].w, b = e[i].cost;
      if (w >= b && d[y][w - b] > d[cur][w] + a) {
        d[y][w - b] = d[cur][w] + a;
        q.push({y, w - b});
      }
    }
    if (d[cur][min(w + c[cur], (ll)2500)] > d[cur][w] + t[cur]) {
      d[cur][min(w + c[cur], (ll)2500)] = d[cur][w] + t[cur];
      q.push({cur, min(w + c[cur], (ll)2500)});
    }
  }
}
int main() {
  scanf("%d%d%lld", &n, &m, &s);
  s = min(s, (ll)2500);
  for (int i = 0; i <= n; ++i)
    head[i] = -1;
  /* code */
  for (int i = 0; i < m; ++i) {
    int u, v;
    ll a, b;
    scanf("%d%d%lld%lld", &u, &v, &a, &b);
    add(u, v, b, a);
    add(v, u, b, a);
    /* code */
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &c[i], &t[i]);
  }
  dijkstra(s, dis);
  for (int i = 2; i <= n; i++) {
    ll ans = 0x3f3f3f3f3f3f3f3f;
    for (int j = 0; j <= 2500; j++)
      ans = min(ans, dis[i][j]);
    printf("%lld\\n", ans);
  }
  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 {
  llong 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);

    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});
      }
    }

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

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

  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;
  }

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

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

  return 0;
}