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

#include <bits/stdc++.h>
#define re register
#define int long long
using namespace std;
inline int read() {
  re int t = 0;
  re char v = getchar();
  while (v < '0')
    v = getchar();
  while (v >= '0')
    t = (t << 3) + (t << 1) + v - 48, v = getchar();
  return t;
}
struct edge {
  int to, next, w;
} e[20000002];
int n, m, head[300002], ans, cnt, sum, s;
inline void add(re int x, re int y, re int w) {
  e[++cnt].to = y;
  e[cnt].next = head[x];
  head[x] = cnt;
  e[cnt].w = w;
}
struct node {
  int pos, dis;
  bool operator<(const node x) const { return dis > x.dis; }
} h[300002];
priority_queue<node> q;
bool v[300002];
inline int pos(re int x, re int y) { return x * (sum + 1) + y; }
inline void dis(re int s) {
  for (re int i = 1; i <= pos(n, sum); ++i)
    h[i].dis = 1e18, h[i].pos = i;
  h[s].dis = 0;
  q.push(h[s]);
  while (!q.empty()) {
    while ((h[q.top().pos].dis < q.top().dis)) {
      q.pop();
      if (q.empty())
        break;
    }
    if (q.empty())
      break;
    node tmp = q.top();
    q.pop();
    for (re int i = head[tmp.pos]; i; i = e[i].next) {
      if ((tmp.dis + e[i].w) < h[e[i].to].dis) {
        h[e[i].to].dis = tmp.dis + e[i].w;
        q.push(h[e[i].to]);
      }
    }
  }
}
signed main() {
  n = read();
  m = read();
  s = read();
  sum = 5000;
  for (re int i = 1, x, y, a, b; i <= m; ++i) {
    x = read();
    y = read();
    a = read();
    b = read();
    for (re int j = a; j <= sum; ++j)
      add(pos(x, j), pos(y, j - a), b), add(pos(y, j), pos(x, j - a), b);
  }
  for (re int x, y, i = 1; i <= n; ++i) {
    x = read();
    y = read();
    for (re int j = 0; j <= sum; ++j) {
      add(pos(i, j), pos(i, min(j + x, sum)), y);
      if (j)
        add(pos(i, j), pos(i, j - 1), 0);
    }
  }
  dis(pos(1, min(s, sum)));
  for (re int i = 2; i <= n; ++i)
    printf("%lld\\n", h[pos(i, 0)].dis);
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define re register
#define int long long
using namespace std;
inline int read() {
  re int t = 0;
  re char v = getchar();
  while (v < '0')
    v = getchar();
  while (v >= '0')
    t = (t << 3) + (t << 1) + v - 48, v = getchar();
  return t;
}
struct edge {
  int to, next, w;
} e[20000002];
int n, m, head[250002], ans, cnt, sum, s;
inline void add(re int x, re int y, re int w) {
  e[++cnt].to = y;
  e[cnt].next = head[x];
  head[x] = cnt;
  e[cnt].w = w;
}
struct node {
  int pos, dis;
  bool operator<(const node x) const { return dis > x.dis; }
} h[250002];
priority_queue<node> q;
bool v[250005];
inline int pos(re int x, re int y) { return x * (sum + 1) + y; }
inline void dis(re int s) {
  for (re int i = 1; i <= pos(n, sum); ++i)
    h[i].dis = 1e9, h[i].pos = i;
  h[s].dis = 0;
  q.push(h[s]);
  while (!q.empty()) {
    while ((h[q.top().pos].dis < q.top().dis)) {
      q.pop();
      if (q.empty())
        break;
    }
    if (q.empty())
      break;
    node tmp = q.top();
    q.pop();
    for (re int i = head[tmp.pos]; i; i = e[i].next) {
      if ((tmp.dis + e[i].w) < h[e[i].to].dis) {
        h[e[i].to].dis = tmp.dis + e[i].w;
        q.push(h[e[i].to]);
      }
    }
  }
}
signed main() {
  n = read();
  m = read();
  s = read();
  sum = 5000;
  for (re int i = 1, x, y, a, b; i <= m; ++i) {
    x = read();
    y = read();
    a = read();
    b = read();
    for (re int j = a; j <= sum; ++j)
      add(pos(x, j), pos(y, j - a), b), add(pos(y, j), pos(x, j - a), b);
  }
  for (re int x, y, i = 1; i <= n; ++i) {
    x = read();
    y = read();
    for (re int j = 0; j <= sum; ++j) {
      add(pos(i, j), pos(i, min(j + x, sum)), y);
      if (j)
        add(pos(i, j), pos(i, j - 1), 0);
    }
  }
  dis(pos(1, min(s, sum)));
  for (re int i = 2; i <= n; ++i)
    printf("%lld\\n", h[pos(i, 0)].dis);
}