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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const LL inf = 0x3f3f3f3f3f3f3f3f;
const int N = 55, M = 110;
struct Edge {
  int to, next;
  LL a, b;
} e[M * 2];
int head[N], idx;
LL c[N], d[N];
LL dis[N][2510];
int n, m, s;

void add(int u, int v, LL a, LL b) {
  e[++idx].to = v;
  e[idx].next = head[u];
  e[idx].a = a;
  e[idx].b = b;
  head[u] = idx;
}

void spfa() {
  queue<PII> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 2500; j++) {
      dis[i][j] = inf;
    }
  }
  dis[1][s] = 0;
  q.push({1, s});
  while (!q.empty()) {
    int u = q.front().first, w = q.front().second;
    q.pop();
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].to;
      LL a = e[i].a, b = e[i].b;
      if (w >= a && dis[v][w - a] > dis[u][w] + b) {
        dis[v][w - a] = dis[u][w] + b;
        q.push({v, w - a});
      }
    }
    if (dis[u][min(w + c[u], (LL)2500)] > dis[u][w] + d[u]) {
      dis[u][min(w + c[u], (LL)2500)] = dis[u][w] + d[u];
      q.push({u, min(w + c[u], (LL)2500)});
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &s);
  s = min(s, 2500);
  while (m--) {
    int u, v;
    LL a, b;
    scanf("%d%d%lld%lld", &u, &v, &a, &b);
    add(u, v, a, b);
    add(v, u, a, b);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld %lld", &c[i], &d[i]);
  }
  spfa();
  for (int i = 2; i <= n; i++) {
    LL res = inf;
    for (int j = 0; j <= 2500; j++)
      res = min(res, dis[i][j]);
    printf("%lld\\n", res);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define mem(f, x) memset(f, x, sizeof(f))
#define pii pair<int, int>
#define fi first
#define se second
#define mk(x, y) make_pair(x, y)
#define pk push_back
using namespace std;
const int M = 215;
const int N = 55;
const ll INF = 0x3f3f3f3f3f3f;
const int MAXN = 2505;
int m, n, s;
int head[N], cnt;
ll dis[N][MAXN];
ll c[N], d[N];

struct eg {
  int to, pre;
  ll a, b;
  eg() : to(0), pre(0), a(0), b(0) {}
  eg(int tt, int pp, int aa, int bb) { to = tt, pre = pp, a = aa, b = bb; }
} e[M];

struct node {
  int x, c, d;
  node() : x(0), c(0), d(0) {}
  node(int xx, int cc, int dd) : x(xx), c(cc), d(dd) {}
  friend bool operator<(node p1, node p2) {
    if (p1.d == p2.d)
      return p1.c < p2.c;
    return p1.d > p2.d;
  }
};

void add(int x, int y, int a, int b) {
  e[++cnt] = eg(y, head[x], a, b);
  head[x] = cnt;
}

void Bfs() {
  dis[1][s] = 0;
  queue<node> pq;
  node cur, nxt;
  cur = node(1, s, 0);
  pq.push(cur);
  while (!pq.empty()) {
    cur = pq.front();
    pq.pop();
    int x = cur.x;
    // cout << "cur_x: " << x << endl;
    for (int i = head[x]; i; i = e[i].pre) {
      int to = e[i].to;
      if (cur.c < e[i].a)
        continue;
      nxt = node(to, cur.c - e[i].a, dis[x][cur.c] + e[i].b);
      if (dis[to][cur.c - e[i].a] > nxt.d) {
        dis[to][cur.c - e[i].a] = nxt.d;
        pq.push(nxt);
      }
    }

    ll mi = 2500;
    mi = min(mi, cur.c + c[x]);
    if (dis[x][mi] > dis[x][cur.c] + d[x]) {
      dis[x][mi] = dis[x][cur.c] + d[x];
      pq.push(node(x, mi, dis[x][mi]));
    }
  }
}

void init() {
  for (int i = 0; i <= n; i++) {
    head[i] = 0;
    for (int j = 0; j < MAXN; j++)
      dis[i][j] = INF;
  }

  cnt = 0;
}

int main() {
  while (scanf("%d %d %d", &n, &m, &s) != EOF) {
    init();
    int x, y;
    ll a, b;
    for (int i = 0; i < m; i++) {
      scanf("%d %d %lld %lld", &x, &y, &a, &b);
      add(x, y, a, b);
      add(y, x, a, b);
    }
    for (int i = 1; i <= n; i++)
      scanf("%lld %lld", c + i, d + i);
    Bfs();
    for (int i = 2; i <= n; i++) {
      ll ans = INF;
      for (int j = 0; j < MAXN; j++)
        ans = min(ans, dis[i][j]);
      printf("%lld\\n", ans);
    }
  }
  return 0;
}