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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 55;
const ll INF = 1e15;
ll all = 0;
struct Node {
  ll id, dis, level;
  bool operator<(const Node &a) const { return dis > a.dis; }
};
ll u[maxn * 4], v[maxn * 4], w[maxn * 4], cost[maxn * 4], nextt[maxn * 4],
    first[maxn], sign;
int book[maxn][2505], n, m;
ll dist[maxn][2505];
pair<ll, ll> G[maxn];
void addedge(ll a, ll b, ll c, ll d) {
  u[++sign] = a;
  v[sign] = b;
  w[sign] = c;
  cost[sign] = d;
  nextt[sign] = first[a];
  first[a] = sign;
}
priority_queue<Node> Q;
void Dijstra(int s, ll money) {
  while (Q.size())
    Q.pop();
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 2500; j++)
      book[i][j] = 0, dist[i][j] = INF;
  }
  dist[s][money] = 0;
  Q.push({s, 0, money});
  while (Q.size()) {
    Node temp = Q.top();
    Q.pop();
    ll dis = temp.dis, id = temp.id, level = temp.level;
    if (book[id][level])
      continue;
    book[id][level] = 1;
    for (int i = first[id]; i; i = nextt[i]) {
      if (level < cost[i])
        continue;
      if (dist[v[i]][level - cost[i]] > dis + w[i]) {
        dist[v[i]][level - cost[i]] = dis + w[i];
        Q.push({v[i], dist[v[i]][level - cost[i]], level - cost[i]});
      }
    }
    if (dist[id][min(2500ll, level + G[id].first)] > dis + G[id].second) {
      dist[id][min(2500ll, level + G[id].first)] = dis + G[id].second;
      Q.push({id, dist[id][min(2500ll, level + G[id].first)],
              min(2500ll, level + G[id].first)});
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  ll s;
  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    int x, y, a, b;
    cin >> x >> y >> a >> b;
    addedge(x, y, b, a);
    addedge(y, x, b, a);
    all += a;
  }
  for (int i = 1; i <= n; i++) {
    ll c, d;
    cin >> c >> d;
    G[i].first = c;
    G[i].second = d;
  }
  Dijstra(1, min(s, 2500ll));
  for (int i = 2; i <= n; i++) {
    ll ans = INF;
    for (int j = 0; j <= 2500; j++)
      ans = min(ans, dist[i][j]);
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 55;
const ll INF = 1e15;
ll all = 0;
struct Node {
  ll id, dis, level;
  bool operator<(const Node &a) const { return dis > a.dis; }
};
ll u[maxn * 2], v[maxn * 2], w[maxn * 2], cost[maxn * 2], nextt[maxn * 2],
    first[maxn], sign;
int book[maxn][2505], n, m;
ll dist[maxn][2505];
pair<ll, ll> G[maxn];
void addedge(ll a, ll b, ll c, ll d) {
  u[++sign] = a;
  v[sign] = b;
  w[sign] = c;
  cost[sign] = d;
  nextt[sign] = first[a];
  first[a] = sign;
}
priority_queue<Node> Q;
void Dijstra(int s, ll money) {
  while (Q.size())
    Q.pop();
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 2500; j++)
      book[i][j] = 0, dist[i][j] = INF;
  }
  dist[s][money] = 0;
  Q.push({s, 0, money});
  while (Q.size()) {
    Node temp = Q.top();
    Q.pop();
    ll dis = temp.dis, id = temp.id, level = temp.level;
    if (book[id][level])
      continue;
    book[id][level] = 1;
    for (int i = first[id]; i; i = nextt[i]) {
      if (level < cost[i])
        continue;
      if (dist[v[i]][level - cost[i]] > dis + w[i]) {
        dist[v[i]][level - cost[i]] = dis + w[i];
        Q.push({v[i], dist[v[i]][level - cost[i]], level - cost[i]});
      }
    }
    if (dist[id][min(2500ll, level + G[id].first)] > dis + G[id].second) {
      dist[id][min(2500ll, level + G[id].first)] = dis + G[id].second;
      Q.push({id, dist[id][min(2500ll, level + G[id].first)],
              min(2500ll, level + G[id].first)});
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  ll s;
  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    int x, y, a, b;
    cin >> x >> y >> a >> b;
    addedge(x, y, b, a);
    addedge(y, x, b, a);
    all += a;
  }
  for (int i = 1; i <= n; i++) {
    ll c, d;
    cin >> c >> d;
    G[i].first = c;
    G[i].second = d;
  }
  Dijstra(1, min(s, 2500ll));
  for (int i = 2; i <= n; i++) {
    ll ans = INF;
    for (int j = 0; j <= 2500; j++)
      ans = min(ans, dist[i][j]);
    cout << ans << endl;
  }
  return 0;
}