Problem p02703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back
#define ins insert
#define er erase

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, ii> iiii;

const int oo = 1e18 + 7, mod = 1e9 + 7;

int n, m, s, c[55], d[55], dp[55][5005];
vector<iii> vc[55];
priority_queue<iii, vector<iii>, greater<iii>> pq;

void dijk() {
  while (!pq.empty()) {
    // if(pq.size() <= 5000) cout << pq.size() << "\n";
    iii tmp = pq.top();
    pq.pop();
    if (tmp.fi > dp[tmp.se.fi][tmp.se.se])
      continue;
    int u = tmp.se.fi;
    // cout << u << " " << tmp.se.se << " " << dp[tmp.se.fi][tmp.se.se] << " "
    // << c[u] << " " << d[u] << "\n";
    if (tmp.se.se + c[u] <= 5000 &&
        dp[u][tmp.se.se + c[u]] > (dp[u][tmp.se.se] + d[u])) {
      dp[u][tmp.se.se + c[u]] = (dp[u][tmp.se.se] + d[u]);
      pq.push({dp[u][tmp.se.se + c[u]], {u, tmp.se.se + c[u]}});
    }
    for (int i = 0; i < vc[u].size(); i++) {
      int v = vc[u][i].fi;
      if (tmp.se.se < vc[u][i].se.fi)
        continue;
      // cout << u << " " << tmp.se.se << " " << dp[u][tmp.se.se] << "\n";
      if (dp[v][tmp.se.se - vc[u][i].se.fi] >
          (dp[u][tmp.se.se] + vc[u][i].se.se)) {
        // if(v == 3) cout << u << " " << v << " " << tmp.se.se << "\n";
        dp[v][tmp.se.se - vc[u][i].se.fi] = dp[u][tmp.se.se] + vc[u][i].se.se;
        // if(pq.size() < 10000) cout << v << " " << tmp.se.se - vc[u][i].se.fi
        // << "\n";
        pq.push({dp[v][tmp.se.se - vc[u][i].se.fi],
                 {v, tmp.se.se - vc[u][i].se.fi}});
      }
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    vc[u].pb({v, {a, b}});
    vc[v].pb({u, {a, b}});
  }
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> d[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 5000; j++)
      dp[i][j] = oo;
  }
  dp[1][s] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 5000; j++)
      pq.push({dp[i][j], {i, j}});
  }
  dijk();
  for (int i = 2; i <= n; i++) {
    int answ = oo;
    for (int j = 0; j <= 5000; j++)
      answ = min(answ, dp[i][j]);
    cout << answ << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back
#define ins insert
#define er erase

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, ii> iiii;

const int oo = 1e18 + 7, mod = 1e9 + 7;

int n, m, s, c[55], d[55], dp[55][5005];
vector<iii> vc[55];
priority_queue<iii, vector<iii>, greater<iii>> pq;

void dijk() {
  while (!pq.empty()) {
    // if(pq.size() <= 5000) cout << pq.size() << "\n";
    iii tmp = pq.top();
    pq.pop();
    if (tmp.fi > dp[tmp.se.fi][tmp.se.se])
      continue;
    int u = tmp.se.fi;
    // cout << u << " " << tmp.se.se << " " << dp[tmp.se.fi][tmp.se.se] << " "
    // << c[u] << " " << d[u] << "\n";
    if (tmp.se.se + c[u] <= 5000 &&
        dp[u][tmp.se.se + c[u]] > (dp[u][tmp.se.se] + d[u])) {
      dp[u][tmp.se.se + c[u]] = (dp[u][tmp.se.se] + d[u]);
      pq.push({dp[u][tmp.se.se + c[u]], {u, tmp.se.se + c[u]}});
    }
    for (int i = 0; i < vc[u].size(); i++) {
      int v = vc[u][i].fi;
      if (tmp.se.se < vc[u][i].se.fi)
        continue;
      // cout << u << " " << tmp.se.se << " " << dp[u][tmp.se.se] << "\n";
      if (dp[v][tmp.se.se - vc[u][i].se.fi] >
          (dp[u][tmp.se.se] + vc[u][i].se.se)) {
        // if(v == 3) cout << u << " " << v << " " << tmp.se.se << "\n";
        dp[v][tmp.se.se - vc[u][i].se.fi] = dp[u][tmp.se.se] + vc[u][i].se.se;
        // if(pq.size() < 10000) cout << v << " " << tmp.se.se - vc[u][i].se.fi
        // << "\n";
        pq.push({dp[v][tmp.se.se - vc[u][i].se.fi],
                 {v, tmp.se.se - vc[u][i].se.fi}});
      }
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> s;
  s = min(s, 5000LL);
  for (int i = 1; i <= m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    vc[u].pb({v, {a, b}});
    vc[v].pb({u, {a, b}});
  }
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> d[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 5000; j++)
      dp[i][j] = oo;
  }
  dp[1][s] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 5000; j++)
      pq.push({dp[i][j], {i, j}});
  }
  dijk();
  for (int i = 2; i <= n; i++) {
    int answ = oo;
    for (int j = 0; j <= 5000; j++)
      answ = min(answ, dp[i][j]);
    cout << answ << "\n";
  }
}