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

#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 100000000000000
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;
// using P = pair<int, int>;

/*
const int MAX = 1000000;
const int MOD = 1000000007;


long long fac[MAX], finv[MAX], inv[MAX];

//テーブルの前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        //そのまま計算すると負の値になるのでMODを足す
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

//二分累乗法
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
*/

/*
//UnionFind木
struct UnionFind{
   vector<long long int> par; //par[i] : iの親の番号
   vector<long long int> rnk; //root[i] : iの木のサイズ

   //コンストラクタ
   UnionFind(long long int n): par(n), rnk(n){
       //木の初期化
       for(long long int i = 0; i < n; i++){
           par[i] = i;
           rnk[i] = 1;
       }
   }

   //データxが属する木の根を再帰で取得
   long long int root(long long int x){
       if(par[x] == x){
           return x;
       }else{
           return par[x] = root(par[x]);
       }
   }

   //xとyが同じ木に属しているかを判定
   bool same(long long int x, long long int y){
       return root(x) == root(y);
   }

   //xとyの木を併合
   void unite(long long int x, long long int y){
       long long int rx = root(x); //xの根
       long long int ry = root(y); //yの根
       if(rx == ry) return; //根が同じならそのまま

       //併合するときにサイズを足し合わせる
       if(rnk[rx] < rnk[ry]){
           par[rx] = ry;
           rnk[ry] += rnk[rx];
       }else{
           par[ry] = rx;
           rnk[rx] += rnk[ry];
       }
   }

   //属している木のサイズを取得
   long long int size(long long int x){
       return rnk[root(x)];
   }
};
*/

/*
//重み付きUnionFind
template<class Abel> struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<Abel> diff_weight;

    UnionFind(int n = 1, Abel SUM_UNITY = 0) {
        init(n, SUM_UNITY);
    }

    void init(int n = 1, Abel SUM_UNITY = 0) {
        par.resize(n); rank.resize(n); diff_weight.resize(n);
        for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] =
SUM_UNITY;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            diff_weight[x] += diff_weight[par[x]];
            return par[x] = r;
        }
    }

    Abel weight(int x) {
        root(x);
        return diff_weight[x];
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y, Abel w) {
        w += weight(x); w -= weight(y);
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y), w = -w;
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        diff_weight[y] = w;
        return true;
    }

    Abel diff(int x, int y) {
        return weight(y) - weight(x);
    }
};
*/

/*
//ダイクストラ
typedef struct edge{
   int to;
   int cost;
} edge;

vector<edge> graph[2010];
long long int d[2010];

void dijkstra(int s){
   priority_queue<P, vector<P>, greater<P> > que;
   for(int i = 0; i < 2010; i++){
       d[i] = INF;
   }
   d[s] = 0;
   que.push(P(s, 0));

   while (!que.empty()) {
       P p = que.top();
       que.pop();
       int v = p.first;
       if (d[v] < p.second) continue;

       for (int i=0; i<graph[v].size(); ++i) {
           edge e = graph[v][i];
           if (d[e.to] > d[v] + e.cost) {
               d[e.to] = d[v] + e.cost;
               que.push(P(e.to, d[e.to]));
           }
       }
   }
}
*/

typedef pair<long long int, long long int> P;

typedef struct edge {
  int to;
  P cost;
} edge;

vector<edge> graph[60];
vector<P> exch;
long long int d[5050][60]; // 枚数, 場所
long long int big = 0;

void dijkstra(int s, long long tic) {
  typedef pair<long long int, P> T; // pair<頂点までの距離, 枚数, 頂点>
  priority_queue<T, vector<T>, greater<T>> que;
  for (int i = 0; i < 5050; i++) {
    for (int j = 0; j < 60; j++) {
      d[i][j] = INF;
    }
  }

  // スタート地点への距離は0
  if (tic >= big) {
    d[big][s] = 0;
    que.push(T(0, P(big, s)));
  } else {
    d[tic][s] = 0;
    que.push(T(0, P(tic, s)));
  }

  // ダイクストラ法
  while (!que.empty()) {
    T t = que.top();
    que.pop();
    long long int dist = t.first;
    int state = t.second.first;
    int v = t.second.second;
    // cout << v << " " << state << endl;

    if (dist > d[state][v])
      continue;

    long long int c = exch[v].first;
    long long int dd = exch[v].second;
    for (int i = 1; state + (i - 1) * c <= big; i++) {
      long long int nexts = min(big, state + i * c);
      if (d[nexts][v] > dist + i * dd) {
        d[nexts][v] = dist + i * dd;
        que.push(T(d[nexts][v], P(nexts, v)));
      }
    }

    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      long long int a = e.cost.first;
      long long int b = e.cost.second;
      if (state >= a) {
        if (d[state - a][e.to] > dist + b) {
          d[state - a][e.to] = dist + b;
          que.push(T(d[state - a][e.to], P(state - a, e.to)));
        }
      }
    }
  }
}

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

  for (int i = 0; i < m; i++) {
    long long int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;

    edge e;
    e.to = u;
    e.cost = make_pair(a, b);
    graph[v].push_back(e);
    e.to = v;
    e.cost = make_pair(a, b);
    graph[u].push_back(e);

    big += a;
  }

  for (int i = 0; i < n; i++) {
    long long int c, d;
    cin >> c >> d;
    exch.push_back(make_pair(c, d));
  }
  dijkstra(0, s);

  for (int i = 1; i < n; i++) {
    long long ans = INF;
    for (int j = 0; j <= big; j++) {
      ans = min(ans, d[j][i]);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 100000000000000
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;
// using P = pair<int, int>;

/*
const int MAX = 1000000;
const int MOD = 1000000007;


long long fac[MAX], finv[MAX], inv[MAX];

//テーブルの前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        //そのまま計算すると負の値になるのでMODを足す
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

//二分累乗法
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
*/

/*
//UnionFind木
struct UnionFind{
   vector<long long int> par; //par[i] : iの親の番号
   vector<long long int> rnk; //root[i] : iの木のサイズ

   //コンストラクタ
   UnionFind(long long int n): par(n), rnk(n){
       //木の初期化
       for(long long int i = 0; i < n; i++){
           par[i] = i;
           rnk[i] = 1;
       }
   }

   //データxが属する木の根を再帰で取得
   long long int root(long long int x){
       if(par[x] == x){
           return x;
       }else{
           return par[x] = root(par[x]);
       }
   }

   //xとyが同じ木に属しているかを判定
   bool same(long long int x, long long int y){
       return root(x) == root(y);
   }

   //xとyの木を併合
   void unite(long long int x, long long int y){
       long long int rx = root(x); //xの根
       long long int ry = root(y); //yの根
       if(rx == ry) return; //根が同じならそのまま

       //併合するときにサイズを足し合わせる
       if(rnk[rx] < rnk[ry]){
           par[rx] = ry;
           rnk[ry] += rnk[rx];
       }else{
           par[ry] = rx;
           rnk[rx] += rnk[ry];
       }
   }

   //属している木のサイズを取得
   long long int size(long long int x){
       return rnk[root(x)];
   }
};
*/

/*
//重み付きUnionFind
template<class Abel> struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<Abel> diff_weight;

    UnionFind(int n = 1, Abel SUM_UNITY = 0) {
        init(n, SUM_UNITY);
    }

    void init(int n = 1, Abel SUM_UNITY = 0) {
        par.resize(n); rank.resize(n); diff_weight.resize(n);
        for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] =
SUM_UNITY;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            diff_weight[x] += diff_weight[par[x]];
            return par[x] = r;
        }
    }

    Abel weight(int x) {
        root(x);
        return diff_weight[x];
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y, Abel w) {
        w += weight(x); w -= weight(y);
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y), w = -w;
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        diff_weight[y] = w;
        return true;
    }

    Abel diff(int x, int y) {
        return weight(y) - weight(x);
    }
};
*/

/*
//ダイクストラ
typedef struct edge{
   int to;
   int cost;
} edge;

vector<edge> graph[2010];
long long int d[2010];

void dijkstra(int s){
   priority_queue<P, vector<P>, greater<P> > que;
   for(int i = 0; i < 2010; i++){
       d[i] = INF;
   }
   d[s] = 0;
   que.push(P(s, 0));

   while (!que.empty()) {
       P p = que.top();
       que.pop();
       int v = p.first;
       if (d[v] < p.second) continue;

       for (int i=0; i<graph[v].size(); ++i) {
           edge e = graph[v][i];
           if (d[e.to] > d[v] + e.cost) {
               d[e.to] = d[v] + e.cost;
               que.push(P(e.to, d[e.to]));
           }
       }
   }
}
*/

typedef pair<long long int, long long int> P;

typedef struct edge {
  int to;
  P cost;
} edge;

vector<edge> graph[60];
vector<P> exch;
long long int d[2550][60]; // 枚数, 場所
long long int big = 0;

void dijkstra(int s, long long tic) {
  typedef pair<long long int, P> T; // pair<頂点までの距離, 枚数, 頂点>
  priority_queue<T, vector<T>, greater<T>> que;
  for (int i = 0; i < 2550; i++) {
    for (int j = 0; j < 60; j++) {
      d[i][j] = INF;
    }
  }

  // スタート地点への距離は0
  if (tic >= big) {
    d[big][s] = 0;
    que.push(T(0, P(big, s)));
  } else {
    d[tic][s] = 0;
    que.push(T(0, P(tic, s)));
  }

  // ダイクストラ法
  while (!que.empty()) {
    T t = que.top();
    que.pop();
    long long int dist = t.first;
    int state = t.second.first;
    int v = t.second.second;
    // cout << v << " " << state << endl;

    if (dist > d[state][v])
      continue;

    long long int c = exch[v].first;
    long long int dd = exch[v].second;
    for (int i = 1; state + (i - 1) * c <= big; i++) {
      long long int nexts = min(big, state + i * c);
      if (d[nexts][v] > dist + i * dd) {
        d[nexts][v] = dist + i * dd;
        que.push(T(d[nexts][v], P(nexts, v)));
      }
    }

    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      long long int a = e.cost.first;
      long long int b = e.cost.second;
      if (state >= a) {
        if (d[state - a][e.to] > dist + b) {
          d[state - a][e.to] = dist + b;
          que.push(T(d[state - a][e.to], P(state - a, e.to)));
        }
      }
    }
  }
}

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

  for (int i = 0; i < m; i++) {
    long long int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;

    edge e;
    e.to = u;
    e.cost = make_pair(a, b);
    graph[v].push_back(e);
    e.to = v;
    e.cost = make_pair(a, b);
    graph[u].push_back(e);

    big += a;
  }

  for (int i = 0; i < n; i++) {
    long long int c, d;
    cin >> c >> d;
    exch.push_back(make_pair(c, d));
  }
  dijkstra(0, s);

  for (int i = 1; i < n; i++) {
    long long ans = INF;
    for (int j = 0; j <= big; j++) {
      ans = min(ans, d[j][i]);
    }
    cout << ans << endl;
  }
}