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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n)-1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\\n"
#define popcnt(x) __builtin_popcount(x)

const double PI = acos(-1.0);

using ll = long long;
using P = pair<int, int>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++)
    cin >> v.at(i);
  return is;
}

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
  cout << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <class T> void print(const vector<T> &v, const string &delimiter) {
  rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i);
  cout << endl;
}

template <class T>
void print(const vector<vector<T>> &vv, const string &delimiter) {
  for (const auto &v : vv)
    print(v, delimiter);
}

template <class Weight> struct WeightedGraph {
  struct Edge {
    int to;
    Weight weight;
  };
  int n;
  vector<vector<Edge>> edges;
  Weight zero;
  Weight inf;
  WeightedGraph(int n, Weight zero, Weight inf)
      : n(n), edges(n), zero(zero), inf(inf) {}
  void add_edge(int u, int v, Weight w) { edges[u].push_back({v, w}); }
};

template <class Weight>
vector<Weight> dijkstra(int s, const WeightedGraph<Weight> &g) {
  using Node = pair<Weight, int>;
  priority_queue<Node, vector<Node>, greater<Node>> que;
  vector<Weight> d(g.n, g.inf);
  d[s] = g.zero;
  que.push(Node(g.zero, s));

  while (!que.empty()) {
    Node p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < (int)g.edges[v].size(); i++) {
      auto e = g.edges[v][i];
      if (d[e.to] > d[v] + e.weight) {
        d[e.to] = d[v] + e.weight;
        que.push(Node(d[e.to], e.to));
      }
    }
  }
  return d;
}

int main() {
  int n, m, s;
  cin >> n >> m >> s;

  ll max_silver = 5000;

  WeightedGraph<ll> graph(n * (max_silver + 1) + max_silver + 100000, 0, 1e18);

  auto id = [&](int v, int sil) {
    // assert(sil <= max_silver);
    return v * (max_silver + 1) + sil;
  };

  rep(i, m) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    rep(sil, max_silver + 1) {
      if (sil >= a) {
        graph.add_edge(id(u, sil), id(v, sil - a), b);
        graph.add_edge(id(v, sil), id(u, sil - a), b);
      }
    }
  }

  rep(v, n) {
    ll c, d;
    cin >> c >> d;
    rep(sil, max_silver + 1) {
      ll new_sil = sil + c;
      graph.add_edge(id(v, sil), id(v, min(max_silver, new_sil)), d);
      // if (max_silver < new_sil) break;
    }
  }

  auto d = dijkstra(id(0, min<int>(max_silver, s)), graph);
  repr(to, 1, n) {
    ll min_time = 1e18;
    rep(sil, max_silver + 1) chmin(min_time, d[id(to, sil)]);
    cout << min_time << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n)-1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\\n"
#define popcnt(x) __builtin_popcount(x)

const double PI = acos(-1.0);

using ll = long long;
using P = pair<int, int>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++)
    cin >> v.at(i);
  return is;
}

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
  cout << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <class T> void print(const vector<T> &v, const string &delimiter) {
  rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i);
  cout << endl;
}

template <class T>
void print(const vector<vector<T>> &vv, const string &delimiter) {
  for (const auto &v : vv)
    print(v, delimiter);
}

template <class Weight> struct WeightedGraph {
  struct Edge {
    int to;
    Weight weight;
  };
  int n;
  vector<vector<Edge>> edges;
  Weight zero;
  Weight inf;
  WeightedGraph(int n, Weight zero, Weight inf)
      : n(n), edges(n), zero(zero), inf(inf) {}
  void add_edge(int u, int v, Weight w) { edges[u].push_back({v, w}); }
};

template <class Weight>
vector<Weight> dijkstra(int s, const WeightedGraph<Weight> &g) {
  using Node = pair<Weight, int>;
  priority_queue<Node, vector<Node>, greater<Node>> que;
  vector<Weight> d(g.n, g.inf);
  d[s] = g.zero;
  que.push(Node(g.zero, s));

  while (!que.empty()) {
    Node p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < (int)g.edges[v].size(); i++) {
      auto e = g.edges[v][i];
      if (d[e.to] > d[v] + e.weight) {
        d[e.to] = d[v] + e.weight;
        que.push(Node(d[e.to], e.to));
      }
    }
  }
  return d;
}

int main() {
  int n, m, s;
  cin >> n >> m >> s;

  ll max_silver = 5000;

  WeightedGraph<ll> graph(n * (max_silver + 1) + max_silver + 100000, 0, 1e18);

  auto id = [&](int v, int sil) {
    // assert(sil <= max_silver);
    return v * (max_silver + 1) + sil;
  };

  rep(i, m) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    rep(sil, max_silver + 1) {
      if (sil >= a) {
        graph.add_edge(id(u, sil), id(v, sil - a), b);
        graph.add_edge(id(v, sil), id(u, sil - a), b);
      }
    }
  }

  rep(v, n) {
    ll c, d;
    cin >> c >> d;
    rep(sil, max_silver + 1) {
      ll new_sil = sil + c;
      graph.add_edge(id(v, sil), id(v, min(max_silver, new_sil)), d);
      // if (max_silver < new_sil) break;
    }
  }

  auto d = dijkstra(id(0, s), graph);
  repr(to, 1, n) {
    ll min_time = 1e18;
    rep(sil, max_silver + 1) chmin(min_time, d[id(to, sil)]);
    cout << min_time << endl;
  }
}