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

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<long long, long long> P;
typedef pair<P, long long> PP;
int main() {
  long long n, m;
  long long s;
  long long min_cost[100][5000];
  vector<PP> node[100];
  cin >> n >> m >> s;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= 5000; j++) {
      min_cost[i][j] = LLONG_MAX;
    }
  }

  long long maxim = 0;
  for (int i = 0; i < m; i++) {
    long long a, b;
    long long fee, minute;
    cin >> a >> b;
    cin >> fee >> minute;
    maxim = max(maxim, fee);
    node[a].push_back(PP(P(minute, fee), b));
    node[b].push_back(PP(P(minute, fee), a));
  }
  long long change[100];
  long long times[100];
  for (int i = 1; i <= n; i++) {
    cin >> change[i] >> times[i];
  }
  priority_queue<PP, vector<PP>, greater<PP>> pq;
  s = min(maxim * (n - 1), s);
  min_cost[1][s] = 0;
  pq.push(PP(P(0, s), 1));
  while (!pq.empty()) {
    PP now = pq.top();
    pq.pop();
    long long cost = now.first.first;
    long long coin = now.first.second;
    long long pos = now.second;
    //        cout<<endl<<endl<<"pos ="<<pos<<endl;
    //        cout<<"cost="<<cost<<endl;
    //        cout<<"coin="<<coin<<endl;
    for (int i = 0; i < (int)node[pos].size(); i++) {
      long long next = node[pos][i].second;
      long long need = node[pos][i].first.second;
      long long ncost = node[pos][i].first.first + cost;
      long long ncoin;
      if (coin >= need) {
        ncoin = coin - need;
      } else {
        ncoin = change[pos] - (need - coin) % change[pos];
        if (ncoin == change[pos]) {
          ncoin = 0;
        }
        ncost += (need - coin + change[pos] - 1) / change[pos] * times[pos];
      }
      //            cout<<"  next ="<<next<<endl;
      while (ncoin <= maxim * (n - 1)) {
        //		cout<<"  ncost="<<ncost<<endl;
        //		cout<<"  ncoin="<<ncoin<<endl<<endl;
        if (ncost < min_cost[next][ncoin]) {
          min_cost[next][ncoin] = ncost;
          pq.push(PP(P(ncost, ncoin), next));
        }
        ncost += times[pos];
        ncoin += change[pos];
      }
      ncoin = max(maxim * (n - 1), ncoin);
      //            cout<<"  ncost="<<ncost<<endl;
      //            cout<<"  ncoin="<<ncoin<<endl<<endl;
      if (ncost < min_cost[next][ncoin]) {
        min_cost[next][ncoin] = ncost;
        pq.push(PP(P(ncost, ncoin), next));
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    long long minimum = LLONG_MAX;
    for (int j = 0; j <= maxim * (n - 1); j++) {
      minimum = min(minimum, min_cost[i][j]);
    }
    cout << minimum << endl;
  }
  return (0);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<long long, long long> P;
typedef pair<P, long long> PP;
int main() {
  long long n, m;
  long long s;
  long long min_cost[51][2501];
  vector<PP> node[51];
  cin >> n >> m >> s;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= 2501; j++) {
      min_cost[i][j] = LLONG_MAX;
    }
  }

  long long maxim = 0;
  for (int i = 0; i < m; i++) {
    long long a, b;
    long long fee, minute;
    cin >> a >> b;
    cin >> fee >> minute;
    maxim = max(maxim, fee);
    node[a].push_back(PP(P(minute, fee), b));
    node[b].push_back(PP(P(minute, fee), a));
  }
  long long change[55];
  long long times[55];
  for (int i = 1; i <= n; i++) {
    cin >> change[i] >> times[i];
  }
  priority_queue<PP, vector<PP>, greater<PP>> pq;
  s = min(maxim * (n - 1), s);
  min_cost[1][s] = 0;
  pq.push(PP(P(0, s), 1));
  while (!pq.empty()) {
    PP now = pq.top();
    pq.pop();
    long long cost = now.first.first;
    long long coin = now.first.second;
    long long pos = now.second;
    //        cout<<endl<<endl<<"pos ="<<pos<<endl;
    //        cout<<"cost="<<cost<<endl;
    //        cout<<"coin="<<coin<<endl;
    for (int i = 0; i < node[pos].size(); i++) {
      long long next = node[pos][i].second;
      long long need = node[pos][i].first.second;
      long long ncost = node[pos][i].first.first + cost;
      long long ncoin;
      if (coin >= need) {
        ncoin = coin - need;
      } else {
        ncoin = change[pos] - (need - coin) % change[pos];
        if (ncoin == change[pos]) {
          ncoin = 0;
        }
        ncost += (need - coin + change[pos] - 1) / change[pos] * times[pos];
      }
      //            cout<<"  next ="<<next<<endl;
      while (ncoin <= maxim * (n - 1)) {
        //		cout<<"  ncost="<<ncost<<endl;
        //		cout<<"  ncoin="<<ncoin<<endl<<endl;
        if (ncost < min_cost[next][ncoin]) {
          min_cost[next][ncoin] = ncost;
          pq.push(PP(P(ncost, ncoin), next));
        }
        ncost += times[pos];
        ncoin += change[pos];
      }
      ncoin = max(maxim * (n - 1), ncoin);
      //            cout<<"  ncost="<<ncost<<endl;
      //            cout<<"  ncoin="<<ncoin<<endl<<endl;
      if (ncost < min_cost[next][ncoin]) {
        min_cost[next][ncoin] = ncost;
        pq.push(PP(P(ncost, ncoin), next));
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    long long minimum = LLONG_MAX;
    for (int j = 0; j <= maxim * (n - 1); j++) {
      minimum = min(minimum, min_cost[i][j]);
    }
    cout << minimum << endl;
  }
  return (0);
}