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

/*
    Author:zeke

    pass System Test!
    GET AC!!
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
#define Scin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
ll INF = 1e18;
// cout << "Case #" << index << " :IMPOSSIBLE";

typedef ll Weight;
struct Edge { // src:辺の始点,dst:辺の終点,weight:辺の重さ
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
                              : // 辺は重さが重いものを"小さい"と定義する
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
// 引数
// g:隣接リスト,s:始点,dist:各頂点までの距離が入る,prev:最短路木の親頂点が入る
// 戻値 なし
void shortestPath(const Graph &g, int s, vector<Weight> &dist,
                  vector<int> &prev) {
  int n = g.size();
  dist.assign(n, INF);
  dist[s] = 0;
  prev.assign(n, -1);
  priority_queue<Edge> Q;
  Q.push(Edge(-2, s, 0));
  while (!Q.empty()) {
    Edge e = Q.top();
    Q.pop();
    if (prev[e.dst] != -1)
      continue;
    prev[e.dst] = e.src;
    for (auto f = g[e.dst].begin(); f != g[e.dst].end(); f++) {
      if (dist[f->dst] > e.weight + f->weight) {
        dist[f->dst] = e.weight + f->weight;
        Q.push(Edge(f->src, f->dst, e.weight + f->weight));
      }
    }
  }
}

// 引数 prev:最短路木の親頂点集合,t:終点
// 戻値 path:sからtへの最短経路
vector<int> buildPath(const vector<int> &prev, int t) {
  vector<int> path;
  for (int u = t; u >= 0; u = prev[u])
    path.push_back(u);
  reverse(path.begin(), path.end());
  return path;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll n, m, s;
  cin >> n >> m >> s;
  if (s >= 2500)
    s = 2500;
  Graph g(n * 2501);
  rep(i, m) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    rep3(j, a, 2501) {
      ll start = u * 2501 + j;
      ll dist = v * 2501 + j - a;
      g[start].push_back(Edge(start, dist, b));
      start = v * 2501 + j;
      dist = u * 2501 + j - a;
      g[start].push_back(Edge(start, dist, b));
    }
  }
  rep(i, n) {
    ll c, d;
    cin >> c >> d;
    rep(j, 2501) {
      ll start = i * 2501 + j;
      ll dist = min(i * 2501 + j + c, i * 2501 + 2500LL);
      g[start].push_back(Edge(start, dist, d));
    }
  }
  vector<Weight> dist;
  vector<int> prev;
  shortestPath(g, s, dist, prev);
  rep3(j, 1, n) {
    ll res = 1e18;
    rep(i, 2501) {
      if (chmin(res, (ll)dist[2501 * j + i])) {
        /* ll now=2501*j+i;
         while(now!=s){
             cout<<now<<" ";
             now=prev[now];

         }
         cout<<now<<endl;
         cout<<dist[2501*j+i]<<endl;*/
      }
    }
    cout << res << endl;
  }
}


Error: 0

Fixed submission:

/*
    Author:zeke

    pass System Test!
    GET AC!!
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
#define Scin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
ll INF = 1e11;
// cout << "Case #" << index << " :IMPOSSIBLE";

typedef int Weight;
struct Edge { // src:辺の始点,dst:辺の終点,weight:辺の重さ
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
                              : // 辺は重さが重いものを"小さい"と定義する
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
// 引数
// g:隣接リスト,s:始点,dist:各頂点までの距離が入る,prev:最短路木の親頂点が入る
// 戻値 なし
void shortestPath(const Graph &g, int s, vector<Weight> &dist,
                  vector<int> &prev) {
  int n = g.size();
  dist.assign(n, INF);
  dist[s] = 0;
  prev.assign(n, -1);
  priority_queue<Edge> Q;
  Q.push(Edge(-2, s, 0));
  while (!Q.empty()) {
    Edge e = Q.top();
    Q.pop();
    if (prev[e.dst] != -1)
      continue;
    prev[e.dst] = e.src;
    for (auto f = g[e.dst].begin(); f != g[e.dst].end(); f++) {
      if (dist[f->dst] > e.weight + f->weight) {
        dist[f->dst] = e.weight + f->weight;
        Q.push(Edge(f->src, f->dst, e.weight + f->weight));
      }
    }
  }
}

// 引数 prev:最短路木の親頂点集合,t:終点
// 戻値 path:sからtへの最短経路
vector<int> buildPath(const vector<int> &prev, int t) {
  vector<int> path;
  for (int u = t; u >= 0; u = prev[u])
    path.push_back(u);
  reverse(path.begin(), path.end());
  return path;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll n, m, s;
  cin >> n >> m >> s;
  Graph g(n + 10);
  VV cost1(n + 10, V(n + 10));
  rep(i, m) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    g[u].push_back(Edge(u, v, b));
    g[v].push_back(Edge(v, u, b));
    cost1[u][v] = a;
    cost1[v][u] = a;
  }
  VP cost2(n + 10);
  rep(i, n) { cin >> cost2[i].F >> cost2[i].S; }
  vector<vector<pair<ll, ll>>> reg(n, vector<pair<ll, ll>>(n));
  rep(i, n) {
    vector<Weight> dist;
    vector<int> prev;
    shortestPath(g, i, dist, prev);
    rep(j, n) {
      if (i == j) {
        continue;
      }
      ll now = j;
      ll tempcost = 0;
      while (i != now) {
        ll nowtemp = prev[now];
        tempcost += cost1[now][nowtemp];
        now = nowtemp;
      }
      // cout<<i<<" "<<j<<" "<<tempcost<<endl;
      reg[i][j] = {tempcost, dist[j]};
      // cout<<tempcost<<" "<<dist[j]<<endl;
    }
  }
  VP res(n);
  rep3(i, 1, n) res[i].F = 1e18;
  res[0].S = s;
  rep(index, n) {
    rep(i, n) {
      rep(j, n) {
        if (i == j)
          continue;
        ll tempcost = reg[i][j].F;
        ll tempdist = reg[i][j].S;
        ll num = (tempcost - res[i].S + cost2[i].F - 1) / cost2[i].F;
        if (num < 0)
          num = 0;
        ll amari = res[i].S + cost2[i].F * num - tempcost;
        ll resdist = res[i].F + cost2[i].S * num + tempdist;
        // cout<<i<<" "<<j<<" "<<num<<" "<<resdist<<" "<<amari<<endl;
        if (chmin(res[j].F, resdist)) {
          res[j].S = amari;
        }
      }
    }
  }
  rep3(i, 1, n) { cout << res[i].F << endl; }
}