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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

#define int long long

using namespace std;

const int N = 100, K = 3000, INF = 1e18;

struct node {
  int city;
  int cost;
  int time;
};

vector<node> g[N];
int dp[N][K], c[N], d[N];

void dijkstra(int &s) {
  dp[1][s] = 0;
  priority_queue<pair<int, pair<int, int>>> q;
  q.push({0, {s, 1}});
  while (!q.empty()) {
    int utime = -q.top().first;
    int ucoin = q.top().second.first;
    int ucity = q.top().second.second;
    q.pop();
    if (dp[ucity][ucoin] < utime)
      continue;
    for (auto v : g[ucity]) {
      if (ucoin >= v.cost && dp[v.city][ucoin - v.cost] > utime + v.time) {
        dp[v.city][ucoin - v.cost] = utime + v.time;
        q.push({-utime - v.time, {ucoin - v.cost, v.city}});
      }
    }
    if (ucoin + c[ucity] < K &&
        dp[ucity][ucoin + c[ucity]] > utime + d[ucity]) {
      dp[ucity][ucoin + c[ucity]] = utime + d[ucity];
      q.push({-utime - d[ucity], {ucoin + c[ucity], ucity}});
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m, s;
  cin >> n >> m >> s;
  s = min(s, K - 1);
  while (m--) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    g[u].push_back({v, a, b});
    g[v].push_back({u, a, b});
  }
  for (int i = 1; i <= n; ++i) {
    cin >> c[i] >> d[i];
    for (int j = 0; j < K; ++j)
      dp[i][j] = INF;
  }
  dijkstra(s);
  for (int i = 2; i <= n; ++i) {
    cout << *min_element(dp[i], dp[i] + K) << '\\n';
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

#define int long long

using namespace std;

const int N = 100, K = 3000, INF = 1e9;

struct node {
  int city;
  int cost;
  int time;
};

struct state {
  int city;
  int coins;
};

vector<state> x;
vector<node> g[N];
int dp[N][K], c[N], d[N];

void dijkstra(int &s) {
  dp[1][s] = 0;
  priority_queue<pair<int, int>> q;
  q.push({0, 0});
  x.push_back({1, s});
  while (!q.empty()) {
    int ctime = -q.top().first;
    state cur = x[q.top().second];
    q.pop();
    if (dp[cur.city][cur.coins] < ctime)
      continue;
    for (auto v : g[cur.city]) {
      for (int i = 0;; ++i) {
        int vcoin = min(K - 1LL, cur.coins + i * c[cur.city]) - v.cost;
        if (vcoin < 0)
          continue;
        int vtime = i * d[cur.city] + v.time + ctime;
        if (dp[v.city][vcoin] > vtime) {
          dp[v.city][vcoin] = vtime;
          q.push({-vtime, x.size()});
          x.push_back({v.city, vcoin});
        }
        if (vcoin + v.cost == K - 1)
          break;
      }
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m, s;
  cin >> n >> m >> s;
  while (m--) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    g[u].push_back({v, a, b});
    g[v].push_back({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 < K; ++j)
      dp[i][j] = INF;
  }
  dijkstra(s);
  for (int i = 2; i <= n; ++i) {
    int ans = INF;
    for (int j = 0; j < K; ++j)
      ans = min(ans, dp[i][j]);
    cout << ans << '\\n';
  }
  return 0;
}