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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long LL;
const LL MX = 5052;

struct node {
  LL x, dis;
  bool operator<(const node &nd) const { return nd.dis < dis; }
} h, t;

LL ec = 0, hed[2000005] = {0}, nxt[8000005], to[8000005], w[8000005];
void add_edge(LL f, LL t, LL cst) {
  ++ec;
  to[ec] = t;
  w[ec] = cst;
  nxt[ec] = hed[f];
  hed[f] = ec;
}

// dijk
priority_queue<node> q;
LL dis[6000005];
LL vis[6000005];
void dijkstra(LL s) {
  memset(dis, 0x3f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  t.x = s;
  t.dis = 0;
  dis[s] = 0;
  q.push(t);
  while (!q.empty()) {
    h = q.top();
    q.pop();
    LL v, u = h.x;
    if (vis[u])
      continue;
    vis[u] = 1;
    for (LL i = hed[u]; i; i = nxt[i]) {
      LL v = to[i];
      if (dis[v] > dis[u] + w[i]) {
        dis[v] = dis[u] + w[i];
        t.x = v;
        t.dis = dis[v];
        q.push(t);
      }
    }
  }
}
// dijk

LL get_id(LL x, LL y) { return (x - 1) * (MX + 1) + y + 1; }
LL ans[2000005];

int main() {
  LL n, m, s, u, v, a, b;
  cin >> n >> m >> s;
  s = min(s, MX);
  for (LL i = 1; i <= m; i++) {
    cin >> u >> v >> a >> b;
    for (LL j = a; j <= MX; j++) {
      add_edge(get_id(u, j), get_id(v, j - a), b);
      add_edge(get_id(v, j), get_id(u, j - a), b);
    }
  }
  for (LL i = 1; i <= n; i++) {
    cin >> a >> b;
    for (LL j = 0; j <= MX; j++)
      add_edge(get_id(i, j), get_id(i, min(j + a, MX)), b);
  }
  dijkstra(get_id(1, s));
  for (LL i = 1; i <= n; i++) {
    ans[i] = dis[get_id(i, 0)];
    for (LL j = 1; j <= MX; j++)
      ans[i] = min(ans[i], dis[get_id(i, j)]);
  }
  for (LL i = 2; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long LL;
const LL MX = 2502;

struct node {
  LL x, dis;
  bool operator<(const node &nd) const { return nd.dis < dis; }
} h, t;

LL ec = 0, hed[6000005] = {0}, nxt[12000005], to[12000005], w[12000005];
void add_edge(LL f, LL t, LL cst) {
  ++ec;
  to[ec] = t;
  w[ec] = cst;
  nxt[ec] = hed[f];
  hed[f] = ec;
}

// dijk
priority_queue<node> q;
LL dis[6000005];
LL vis[6000005];
void dijkstra(LL s) {
  memset(dis, 0x3f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  t.x = s;
  t.dis = 0;
  dis[s] = 0;
  q.push(t);
  while (!q.empty()) {
    h = q.top();
    q.pop();
    LL v, u = h.x;
    if (vis[u])
      continue;
    vis[u] = 1;
    for (LL i = hed[u]; i; i = nxt[i]) {
      LL v = to[i];
      if (dis[v] > dis[u] + w[i]) {
        dis[v] = dis[u] + w[i];
        t.x = v;
        t.dis = dis[v];
        q.push(t);
      }
    }
  }
}
// dijk

LL get_id(LL x, LL y) { return (x - 1) * (MX + 1) + y + 1; }
LL ans[6000005];

int main() {
  LL n, m, s, u, v, a, b;
  cin >> n >> m >> s;
  for (LL i = 1; i <= m; i++) {
    cin >> u >> v >> a >> b;
    for (LL j = a; j <= MX; j++) {
      add_edge(get_id(u, j), get_id(v, j - a), b);
      add_edge(get_id(v, j), get_id(u, j - a), b);
    }
  }
  for (LL i = 1; i <= n; i++) {
    cin >> a >> b;
    for (LL j = 0; j <= MX; j++)
      add_edge(get_id(i, j), get_id(i, min(j + a, MX)), b);
  }
  dijkstra(get_id(1, s));
  for (LL i = 1; i <= n; i++) {
    ans[i] = dis[get_id(i, 0)];
    for (LL j = 1; j <= MX; j++)
      ans[i] = min(ans[i], dis[get_id(i, j)]);
  }
  for (LL i = 2; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}