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


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

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

template <class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3fLL;

struct edge {
  ll to, fare, time;
  edge(int to, int fare, int time) : to(to), fare(fare), time(time){};
};

int main() {
  ll N, M, S;
  cin >> N >> M >> S;

  S = min(S, 2499ll);

  vector<vector<edge>> G(N);
  for (ll i = 0; i < M; ++i) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    G[u].emplace_back(v, a, b);
    G[v].emplace_back(u, a, b);
  }

  vector<pll> C(N);
  for (ll i = 0; i < N; ++i) {
    ll c, d;
    cin >> c >> d;
    C[i] = make_pair(c, d);
  }

  vector<vector<ll>> dp(N, vector<ll>(2503ll, INF));

  priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>>
      que;

  dp[0][S] = 0;
  que.emplace(make_pair(0, make_pair(0, S)));

  while (!que.empty()) {
    auto P = que.top();
    que.pop();

    ll t = P.first;
    ll v = P.second.first;
    ll s = P.second.second;

    if (dp[v][s] < t)
      continue;

    if (s + C[v].first < 2500ll) {
      ll nt = t + C[v].second;
      ll ns = s + C[v].first;
      if (chmin(dp[v][ns], nt)) {
        que.emplace(make_pair(nt, make_pair(v, ns)));
      }
    }

    for (auto &e : G[v]) {
      if (s < e.fare)
        continue;
      ll nt = t + e.time;
      ll nv = e.to;
      ll ns = s - e.fare;
      if (chmin(dp[nv][ns], nt)) {
        que.emplace(make_pair(nt, make_pair(nv, ns)));
      }
    }
  }

  for (ll i = 1; i < N; ++i) {
    ll ans = INF;
    for (auto num : dp[i]) {
      chmin(ans, num);
    }
    cout << ans << endl;
  }
}


Error: -11

Fixed submission:

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

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

template <class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3fLL;

struct edge {
  ll to, fare, time;
  edge(int to, int fare, int time) : to(to), fare(fare), time(time){};
};

int main() {
  ll N, M, S;
  cin >> N >> M >> S;

  S = min(S, 2499ll);

  vector<vector<edge>> G(N);
  for (ll i = 0; i < M; ++i) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    G[u].emplace_back(v, a, b);
    G[v].emplace_back(u, a, b);
  }

  vector<pll> C(N);
  for (ll i = 0; i < N; ++i) {
    ll c, d;
    cin >> c >> d;
    C[i] = make_pair(c, d);
  }

  vector<vector<ll>> dp(N, vector<ll>(2503ll, INF));
  priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>>
      que;

  dp[0][S] = 0;
  que.emplace(0, make_pair(0, S));
  while (!que.empty()) {
    auto P = que.top();
    que.pop();

    ll t = P.first;
    ll v = P.second.first;
    ll s = P.second.second;

    if (dp[v][s] < t)
      continue;

    if (s + C[v].first < 2500ll) {
      ll nt = t + C[v].second;
      ll ns = s + C[v].first;
      if (chmin(dp[v][ns], nt)) {
        que.emplace(make_pair(nt, make_pair(v, ns)));
      }
    }

    for (auto &e : G[v]) {
      ll nt = t + e.time;
      ll nv = e.to;
      ll ns = s - e.fare;
      if (chmin(dp[nv][ns], nt)) {
        que.emplace(make_pair(nt, make_pair(nv, ns)));
      }
    }
  }

  for (ll i = 1; i < N; ++i) {
    ll ans = INF;
    for (auto num : dp[i]) {
      chmin(ans, num);
    }
    cout << ans << endl;
  }
}