Problem p02703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
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;
}

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  for (auto it : P) {
    s << "<" << it << "> ";
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  for (auto it : P) {
    s << "<" << it.first << "->" << it.second << "> ";
  }
  return s << endl;
}

using pll = pair<long long, long long>; // (money, time)
using Edge = pair<int, pll>;
using Graph = vector<vector<Edge>>;
const long long INF = 1LL << 60;
const int MAX = 2500;

int N, M;
long long S;
Graph G;
vector<long long> C, D; // money, time

void solve() {
  if (S >= MAX)
    S = MAX;
  vector<vector<long long>> dp(N, vector<long long>(MAX + 1, INF));
  priority_queue<pair<long long, pll>, vector<pair<long long, pll>>,
                 greater<pair<long long, pll>>>
      que;
  dp[0][S] = 0;
  que.push(make_pair(0, pll(0, S)));
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    // COUT(p);
    long long time = p.first;
    int v = p.second.first;
    long long s = p.second.second;
    if (time > dp[v][s])
      continue;

    {
      long long ns = s + C[v];
      long long ntime = time + D[v];
      if (chmin(dp[v][ns], ntime)) {
        que.push(make_pair(ntime, pll(v, ns)));
      }
    }
    for (auto e : G[v]) {
      if (s < e.second.first)
        continue;
      int nv = e.first;
      long long ns = s - e.second.first;
      long long ntime = time + e.second.second;
      if (chmin(dp[nv][ns], ntime)) {
        que.push(make_pair(ntime, pll(nv, ns)));
      }
    }
  }
  for (int v = 1; v < N; ++v) {
    long long res = INF;
    for (int s = 0; s <= MAX; ++s)
      chmin(res, dp[v][s]);
    cout << res << endl;
  }
}

int main() {
  while (cin >> N >> M >> S) {
    G.assign(N, vector<Edge>());
    for (int i = 0; i < M; ++i) {
      long long u, v, a, b;
      cin >> u >> v >> a >> b;
      --u, --v;
      G[u].push_back(Edge(v, pll(a, b)));
      G[v].push_back(Edge(u, pll(a, b)));
    }
    C.resize(N);
    D.resize(N);
    for (int i = 0; i < N; ++i)
      cin >> C[i] >> D[i];
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
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;
}

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  for (auto it : P) {
    s << "<" << it << "> ";
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  for (auto it : P) {
    s << "<" << it.first << "->" << it.second << "> ";
  }
  return s << endl;
}

using pll = pair<long long, long long>; // (money, time)
using Edge = pair<int, pll>;
using Graph = vector<vector<Edge>>;
const long long INF = 1LL << 60;
const int MAX = 2500;

int N, M;
long long S;
Graph G;
vector<long long> C, D; // money, time

void solve() {
  if (S >= MAX)
    S = MAX;
  vector<vector<long long>> dp(N, vector<long long>(MAX + 1, INF));
  priority_queue<pair<long long, pll>, vector<pair<long long, pll>>,
                 greater<pair<long long, pll>>>
      que;
  dp[0][S] = 0;
  que.push(make_pair(0, pll(0, S)));
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    // COUT(p);
    long long time = p.first;
    int v = p.second.first;
    long long s = p.second.second;
    if (time > dp[v][s])
      continue;

    if (s + C[v] <= MAX) {
      long long ns = s + C[v];
      long long ntime = time + D[v];
      if (chmin(dp[v][ns], ntime)) {
        que.push(make_pair(ntime, pll(v, ns)));
      }
    }
    for (auto e : G[v]) {
      if (s < e.second.first)
        continue;
      int nv = e.first;
      long long ns = s - e.second.first;
      long long ntime = time + e.second.second;
      if (chmin(dp[nv][ns], ntime)) {
        que.push(make_pair(ntime, pll(nv, ns)));
      }
    }
  }
  for (int v = 1; v < N; ++v) {
    long long res = INF;
    for (int s = 0; s <= MAX; ++s)
      chmin(res, dp[v][s]);
    cout << res << endl;
  }
}

int main() {
  while (cin >> N >> M >> S) {
    G.assign(N, vector<Edge>());
    for (int i = 0; i < M; ++i) {
      long long u, v, a, b;
      cin >> u >> v >> a >> b;
      --u, --v;
      G[u].push_back(Edge(v, pll(a, b)));
      G[v].push_back(Edge(u, pll(a, b)));
    }
    C.resize(N);
    D.resize(N);
    for (int i = 0; i < N; ++i)
      cin >> C[i] >> D[i];
    solve();
  }
}