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

#include <bits/stdc++.h>
using namespace std;
const int N = 55;
typedef long long LL;
struct edge {
  int to, next, len, cost;
} e[N * 4];
int n, m, tot, c[N], d[N], s, mx, bk[N], first[N], mxi;
LL dp[N][N * N * 2];
void add(int u, int v, int a, int b) {
  e[++tot].next = first[u], first[u] = tot;
  e[tot].len = b, e[tot].to = v;
  e[tot].cost = a;
}

void spfa() {
  queue<int> q;
  q.push(1);
  bk[1] = 1;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = 0; i <= mx; i++)
      for (int j = 1; j * c[now] <= i; j++) {
        dp[now][i] =
            min(dp[now][i], dp[now][i - j * c[now]] + 1ll * j * d[now]);
        // cout<<now<<" "<<dp[now][i]<<endl;
      }
    for (int i = first[now]; i; i = e[i].next) {
      int u = e[i].to;
      for (int j = 0; j <= mx; j++) {
        if (e[i].cost <= j) {
          if (dp[u][j - e[i].cost] > dp[now][j] + e[i].len) {
            dp[u][j - e[i].cost] = dp[now][j] + e[i].len;
            if (!bk[u])
              bk[u] = 1, q.push(u);
          }
        }
      }
    }
    bk[now] = 0;
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &s);
  for (int i = 1, u, v, a, b; i <= m; i++) {
    scanf("%d%d%d%d", &u, &v, &a, &b);
    add(u, v, a, b);
    add(v, u, a, b);
    if (a > mxi)
      mxi = a;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &c[i], &d[i]);
  }
  if (s > 1000000)
    mx = mxi * n * 2;
  else {
    mx = n * n * 2 + 1;
  }
  if (s > mx)
    s = mx;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= mx; j++)
      dp[i][j] = 1e17;
  for (int i = 0; i <= s; i++)
    dp[1][i] = 0;
  spfa();
  for (int i = 2; i <= n; i++) {
    LL tmp = 1e17;
    for (int j = 0; j <= mx; j++)
      if (dp[i][j] < tmp)
        tmp = dp[i][j];
    printf("%lld\\n", tmp);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 55;
typedef long long LL;
struct edge {
  int to, next, len, cost;
} e[N * 4];
int n, m, tot, c[N], d[N], s, mx, bk[N], first[N];
LL dp[N][N * N * 2];
void add(int u, int v, int a, int b) {
  e[++tot].next = first[u], first[u] = tot;
  e[tot].len = b, e[tot].to = v;
  e[tot].cost = a;
}

int spfa() {
  queue<int> q;
  q.push(1);
  bk[1] = 1;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = 0; i <= mx; i++)
      for (int j = 1; j * c[now] <= i; j++) {
        dp[now][i] =
            min(dp[now][i], dp[now][i - j * c[now]] + 1ll * j * d[now]);
        // cout<<now<<" "<<dp[now][i]<<endl;
      }
    for (int i = first[now]; i; i = e[i].next) {
      int u = e[i].to;
      for (int j = 0; j <= mx; j++) {
        if (e[i].cost <= j) {
          if (dp[u][j - e[i].cost] > dp[now][j] + e[i].len) {
            dp[u][j - e[i].cost] = dp[now][j] + e[i].len;
            if (!bk[u])
              bk[u] = 1, q.push(u);
          }
        }
      }
    }
    bk[now] = 0;
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &s);
  for (int i = 1, u, v, a, b; i <= m; i++) {
    scanf("%d%d%d%d", &u, &v, &a, &b);
    add(u, v, a, b);
    add(v, u, a, b);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &c[i], &d[i]);
  memset(dp, 0x3f3f3f3f, sizeof(dp));
  mx = n * n * 2 + 1;
  if (s > mx)
    s = mx;
  for (int i = 0; i <= s; i++)
    dp[1][i] = 0;
  spfa();
  for (int i = 2; i <= n; i++) {
    LL tmp = 1e17;
    for (int j = 0; j <= mx; j++) // cout<<dp[i][j]<<" ";
      if (dp[i][j] < tmp)
        tmp = dp[i][j];
    printf("%lld\\n", tmp);
    // cout<<endl;
  }
}