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

#include <bits/stdc++.h>
#define pi acos(-1)
#define ll long long
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define debug(a) cout << a << '\\n'
#define maxn 59 /// Still trying to be normal
#define MOD 1000000007
#define F first
#define S second
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define per(i, b, a) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
using namespace std;
const ll INF = 1e14 + 15;
vector<pair<ll, pii>> adj[maxn]; /// endpoint, money cost, time
pii ex[maxn];                    // money gain, time
ll dis[maxn][2559];              /// endpoint , money
ll n, m, s;
void addedge(ll a, ll b, ll c, ll d) {
  adj[a].push_back({b, {c, d}});
  adj[b].push_back({a, {c, d}});
}
void INFINITAR() {
  for (ll i = 0; i <= n; i++) {
    rep(j, 0, 2559) { dis[i][j] = INF; }
  }
}
void dijkstra(ll x) {
  INFINITAR();
  dis[x][s] = 0;
  priority_queue<piii, vector<piii>, greater<piii>>
      pq; /// time, endpoint, money have
  pq.push({0, {x, s}});
  while (!pq.empty()) {
    // cout<<pq.size()<<endl;
    auto inicio = pq.top();
    // cout<<f.F<<" "<<f.S.F<<" "<<f.S.S<<'\\n';
    pq.pop();
    if (dis[inicio.S.F][inicio.S.S] < inicio.F)
      continue;
    // cout<<"oi\\n";
    ll moneyprox;
    if (inicio.S.S < 2500) {
      moneyprox = min(2509LL, inicio.S.S + ex[inicio.S.F].F);
      if (inicio.F + ex[inicio.S.F].S < dis[inicio.S.F][moneyprox]) {
        dis[inicio.S.F][moneyprox] = inicio.F + ex[inicio.S.F].S;
        // cout<<e.F<<" "<<moneyprox<<" "<<dis[e.F][moneyprox]<<endl;
        pq.push({dis[inicio.S.F][moneyprox], {inicio.S.F, moneyprox}});
      }
    }
    for (auto final : adj[inicio.S.F]) {
      moneyprox = inicio.S.S - final.S.F;
      if (moneyprox < 0)
        continue;
      if (inicio.F + final.S.S < dis[final.F][moneyprox]) {
        dis[final.F][moneyprox] = inicio.F + final.S.S;
        // cout<<e.F<<" "<<moneyprox<<" "<<dis[e.F][moneyprox]<<endl;
        pq.push({dis[final.F][moneyprox], {final.F, moneyprox}});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> s;
  s = min(s, 2509LL);
  rep(i, 0, m) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    addedge(a, b, c, d);
  }
  rep(i, 1, n + 1) {
    ll a, b;
    cin >> a >> b;
    ex[i] = make_pair(a, b);
  }
  dijkstra(1);
  rep(i, 2, n + 1) {
    ll ans = INF;
    rep(j, 0, 2559) { ans = min(ans, dis[i][j]); }
    cout << ans << '\\n';
  }
}


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


Fixed submission:

#include <bits/stdc++.h>
#define pi acos(-1)
#define ll long long
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define debug(a) cout << a << '\\n'
#define maxn 59 /// Still trying to be normal
#define MOD 1000000007
#define F first
#define S second
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define per(i, b, a) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
using namespace std;
const ll INF = 1e9 + 15;
vector<pair<ll, pii>> adj[maxn]; /// endpoint, money cost, time
pii ex[maxn];                    // money gain, time
ll dis[maxn][2509];              /// endpoint , money
ll n, m, s;
void addedge(ll a, ll b, ll c, ll d) {
  adj[a].push_back({b, {c, d}});
  adj[b].push_back({a, {c, d}});
}
void INFINITAR() {
  for (ll i = 0; i <= n; i++) {
    rep(j, 0, 2509) { dis[i][j] = INF; }
  }
}
void dijkstra(ll x) {
  INFINITAR();
  dis[x][s] = 0;
  priority_queue<piii, vector<piii>, greater<piii>>
      pq; /// time, endpoint, money have
  pq.push({0, {x, s}});
  while (!pq.empty()) {
    auto f = pq.top();
    // cout<<f.F<<" "<<f.S.F<<" "<<f.S.S<<'\\n';
    pq.pop();
    if (dis[f.S.F][f.S.S] < f.F)
      continue;
    // cout<<"oi\\n";
    for (auto e : adj[f.S.F]) {
      rep(i, 0, 2509) {
        ll moneyprox = min(2508LL, f.S.S + i * ex[f.S.F].F - e.S.F);
        // cout<<moneyprox<<endl;
        if (moneyprox < 0)
          continue;
        if (f.F + e.S.S + i * ex[f.S.F].S < dis[e.first][moneyprox]) {
          dis[e.F][moneyprox] = f.F + e.S.S + i * ex[f.S.F].S;
          pq.push({dis[e.F][moneyprox], {e.F, moneyprox}});
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> s;
  rep(i, 0, m) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    addedge(a, b, c, d);
  }
  rep(i, 1, n + 1) {
    ll a, b;
    cin >> a >> b;
    ex[i] = make_pair(a, b);
  }
  dijkstra(1);
  rep(i, 2, n + 1) {
    ll ans = INF;
    rep(j, 0, 2509) { ans = min(ans, dis[i][j]); }
    cout << ans << '\\n';
  }
}