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

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
using namespace std;
typedef long long ll;
const int N = 55;
int n, m, s, a[N][N], b[N][N];
ll dp[N * N * 2][N], c[N], d[N], ans[N];
bool vis[N * N * 2][N];
struct node {
  int s, id;
  ll v;
  node(int s = 0, ll id = 0, ll v = 0) : s(s), id(id), v(v) {}
  bool operator<(const node &o) const {
    if (s == o.s)
      return s > o.s;
    return v > o.v;
  }
};
queue<node> q;
int main() {
  memset(ans, inf, sizeof(ans));
  memset(a, -1, sizeof(a));
  memset(b, -1, sizeof(b));
  scanf("%d%d%d", &n, &m, &s);
  for (int i = 1; i <= m; i++) {
    int u, v, A, B;
    scanf("%d%d%d%d", &u, &v, &A, &B);
    a[u][v] = a[v][u] = A;
    b[u][v] = b[v][u] = B;
  }
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &c[i], &d[i]);
  memset(dp, inf, sizeof(dp));
  s = min(s, n * 50 * 2);
  dp[s][1] = 0;
  q.push(node(s, 1, dp[s][1]));
  vis[s][1] = true;
  while (!q.empty()) {
    int s = q.front().s, i = q.front().id;
    q.pop();
    ans[i] = min(ans[i], dp[s][i]);
    vis[s][i] = false;
    int up = min(s + (int)c[i], n * 50 * 2);
    if (dp[up][i] > dp[s][i] + d[i]) {
      dp[up][i] = dp[s][i] + d[i];
      if (!vis[up][i])
        q.push(node(up, i, dp[up][i])), vis[up][i] = true;
    }
    for (int j = 1; j <= n; j++) {
      if (a[i][j] == -1 || s < a[i][j])
        continue;
      int up = s - a[i][j];
      if (dp[up][j] > dp[s][i] + b[i][j]) {
        dp[up][j] = dp[s][i] + b[i][j];
        if (!vis[up][j])
          vis[up][j] = true, q.push(node(up, j, dp[up][j]));
      }
    }
  }
  for (int i = 2; i <= n; i++)
    printf("%lld\\n", ans[i]);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
using namespace std;
typedef long long ll;
const int N = 55;
int n, m, s, a[N][N], b[N][N];
ll dp[N * N * 2][N], c[N], d[N], ans[N];
bool vis[N * N * 2][N];
struct node {
  int s, id;
  ll v;
  node(int s = 0, ll id = 0, ll v = 0) : s(s), id(id), v(v) {}
  bool operator<(const node &o) const {
    if (s == o.s)
      return s > o.s;
    return v > o.v;
  }
};
priority_queue<node> q;
int main() {
  memset(ans, inf, sizeof(ans));
  memset(a, -1, sizeof(a));
  memset(b, -1, sizeof(b));
  scanf("%d%d%d", &n, &m, &s);
  for (int i = 1; i <= m; i++) {
    int u, v, A, B;
    scanf("%d%d%d%d", &u, &v, &A, &B);
    a[u][v] = a[v][u] = A;
    b[u][v] = b[v][u] = B;
  }
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &c[i], &d[i]);
  memset(dp, inf, sizeof(dp));
  dp[s][1] = 0;
  q.push(node(s, 1, dp[s][1]));
  while (!q.empty()) {
    int s = q.top().s, i = q.top().id;
    q.pop();
    ans[i] = min(ans[i], dp[s][i]);
    if (vis[s][i])
      continue;
    vis[s][i] = true;
    int up = min(s + (int)c[i], n * n * 2);
    if (dp[up][i] > dp[s][i] + d[i])
      dp[up][i] = dp[s][i] + d[i], q.push(node(up, i, dp[up][i]));
    for (int j = 1; j <= n; j++) {
      if (a[i][j] == -1 || s < a[i][j])
        continue;
      int up = s - a[i][j];
      if (dp[up][j] > dp[s][i] + b[i][j])
        dp[up][j] = dp[s][i] + b[i][j], q.push(node(up, j, dp[up][j]));
    }
  }
  for (int i = 2; i <= n; i++)
    printf("%lld\\n", ans[i]);
}