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

#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Edge {
  Edge(int arg_to, ll arg_coin, ll arg_cost) {
    to = arg_to;
    coin = arg_coin;
    cost = arg_cost;
  }
  int to;
  ll coin, cost;
};

struct Info {
  Info(int arg_node_id, ll arg_coin, ll arg_sum_dist) {
    node_id = arg_node_id;
    coin = arg_coin;
    sum_dist = arg_sum_dist;
  }
  bool operator<(const struct Info &arg) const {

    return sum_dist > arg.sum_dist; // 総距離の曹純(PQ)
  }

  int node_id;
  ll coin, sum_dist;
};

int N, M;
ll S;
ll min_dist[55][5005];
ll C[55], D[55];
vector<Edge> G[55];

int main() {

  scanf("%d %d %lld", &N, &M, &S);

  int from, to;
  ll coin, cost;
  ll total = 0;

  for (int i = 0; i < M; i++) {

    scanf("%d %d %lld %lld", &from, &to, &coin, &cost);
    from--;
    to--;
    total += coin;

    G[from].push_back(Edge(to, coin, cost));
    G[to].push_back(Edge(from, coin, cost));
  }

  for (int i = 0; i < N; i++) {

    scanf("%lld %lld", &C[i], &D[i]);
  }

  for (int i = 0; i < N; i++) {
    for (int k = 0; k <= total; k++) {

      min_dist[i][k] = HUGE_NUM;
    }
  }

  S = min(S, total);

  priority_queue<Info> Q;
  Q.push(Info(0, S, 0));

  while (!Q.empty()) {

    if (Q.top().sum_dist > min_dist[Q.top().node_id][Q.top().coin]) {

      Q.pop();

    } else {

      for (ll add = 0;; add++) { // 金→銀の枚数

        ll next_coin = Q.top().coin + C[Q.top().node_id] * add;
        if (next_coin > total) {

          next_coin = total;
        }
        ll next_dist = Q.top().sum_dist + D[Q.top().node_id] * add;

        for (int i = 0; i < G[Q.top().node_id].size(); i++) {

          if (G[Q.top().node_id][i].coin > next_coin)
            continue;
          int next_node = G[Q.top().node_id][i].to;

          ll work_coin = next_coin - G[Q.top().node_id][i].coin;
          ll work_dist = next_dist + G[Q.top().node_id][i].cost;

          if (min_dist[next_node][work_coin] > work_dist) {

            min_dist[next_node][work_coin] = work_dist;
            Q.push(Info(next_node, work_coin, work_dist));
          }
        }

        if (Q.top().coin + C[Q.top().node_id] * add >= total) {

          break;
        }
      }

      Q.pop();
    }
  }

  for (int i = 1; i < N; i++) {
    ll ans = HUGE_NUM;
    for (ll k = 0; k <= total; k++) {

      ans = min(ans, min_dist[i][k]);
    }

    printf("%lld\\n", ans);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Edge {
  Edge(int arg_to, ll arg_coin, ll arg_cost) {
    to = arg_to;
    coin = arg_coin;
    cost = arg_cost;
  }
  int to;
  ll coin, cost;
};

struct Info {
  Info(int arg_node_id, ll arg_coin, ll arg_sum_dist) {
    node_id = arg_node_id;
    coin = arg_coin;
    sum_dist = arg_sum_dist;
  }
  bool operator<(const struct Info &arg) const {

    return sum_dist > arg.sum_dist; // 総距離の曹純(PQ)
  }

  int node_id;
  ll coin, sum_dist;
};

int N, M, S;
ll min_dist[55][2505];
ll C[55], D[55];
vector<Edge> G[55];

int main() {

  scanf("%d %d %d", &N, &M, &S);

  int from, to;
  ll coin, cost;
  ll total = 0;

  for (int i = 0; i < M; i++) {

    scanf("%d %d %lld %lld", &from, &to, &coin, &cost);
    from--;
    to--;
    total += coin;

    G[from].push_back(Edge(to, coin, cost));
    G[to].push_back(Edge(from, coin, cost));
  }

  for (int i = 0; i < N; i++) {

    scanf("%lld %lld", &C[i], &D[i]);
  }

  for (int i = 0; i < N; i++) {
    for (int k = 0; k <= total; k++) {

      min_dist[i][k] = HUGE_NUM;
    }
  }

  priority_queue<Info> Q;
  Q.push(Info(0, S, 0));

  while (!Q.empty()) {

    if (Q.top().sum_dist > min_dist[Q.top().node_id][Q.top().coin]) {

      Q.pop();

    } else {

      for (ll add = 0;; add++) { // 金→銀の枚数

        ll next_coin = Q.top().coin + C[Q.top().node_id] * add;
        if (next_coin > total) {

          next_coin = total;
        }
        ll next_dist = Q.top().sum_dist + D[Q.top().node_id] * add;

        for (int i = 0; i < G[Q.top().node_id].size(); i++) {

          if (G[Q.top().node_id][i].coin > next_coin)
            continue;
          int next_node = G[Q.top().node_id][i].to;

          ll work_coin = next_coin - G[Q.top().node_id][i].coin;
          ll work_dist = next_dist + G[Q.top().node_id][i].cost;

          if (min_dist[next_node][work_coin] > work_dist) {

            min_dist[next_node][work_coin] = work_dist;
            Q.push(Info(next_node, work_coin, work_dist));
          }
        }

        if (Q.top().coin + C[Q.top().node_id] * add >= total) {

          break;
        }
      }

      Q.pop();
    }
  }

  for (int i = 1; i < N; i++) {
    ll ans = HUGE_NUM;
    for (ll k = 0; k <= total; k++) {

      ans = min(ans, min_dist[i][k]);
    }

    printf("%lld\\n", ans);
  }

  return 0;
}