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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

struct Edge {
  int to;
  int timeCost;
  int silverCost;
  Edge() {}

  Edge(int pt, int ptc, int psc) : to(pt), timeCost(ptc), silverCost(psc) {}
};

struct Node {
  int now;
  int silver;
  long long timeCost;
  Node() {}

  Node(int n, int s, long long t) : now(n), silver(s), timeCost(t) {}

  bool operator<(const Node &n) const {
    return timeCost > n.timeCost; // reverse pq order
  }
};

using VLL = vector<long long>;
using VVLL = vector<VLL>;

using VI = vector<int>;
using VVI = vector<VI>;

using VE = vector<Edge>;
using VVE = vector<VE>;

const long long INF = 1000000LL * 1000000000LL;

int main() {
  int N;
  int M;
  int S;

  cin >> N >> M >> S;

  VVE adja(N);

  VI goldSilver(N);
  VI goldTime(N);

  for (int i = 0; i < M; ++i) {
    int from;
    int to;
    int T;
    int C;
    cin >> from >> to >> C >> T;
    --from;
    --to;
    adja[from].emplace_back(Edge(to, T, C));
    adja[to].emplace_back(Edge(from, T, C));
  }

  for (int i = 0; i < N; ++i) {
    cin >> goldSilver[i] >> goldTime[i];
  }

  const int UPPER = 60;
  VVLL best(N, VLL(N * UPPER + 1, INF));

  priority_queue<Node> pq;
  pq.push(Node(0, min(S, UPPER * N), 0));
  best[0][min(S, UPPER * N)] = 0;

  while (!pq.empty()) {
    Node now = pq.top();
    pq.pop();
    int nowv = now.now;

    if (now.timeCost > best[nowv][now.silver]) {
      continue;
    }

    // cout << "DEBUG " << nowv << " " << now.silver << " " << now.timeCost <<
    // endl;

    for (int i = 0; i < adja[nowv].size(); ++i) {
      int nextv = adja[nowv][i].to;

      // cout << "INN " << nowv << " " << nextv << endl;

      for (int j = 0; true; ++j) {
        int newS = now.silver + j * (long long)goldSilver[nowv];
        long long timeCost =
            best[nowv][now.silver] + j * (long long)goldTime[nowv];

        if (newS <= UPPER * N || j == 0) {
          int nnewS = newS - adja[nowv][i].silverCost;
          if (nnewS < 0) {
            continue;
          }
          long long nnewT = timeCost + adja[nowv][i].timeCost;

          // cout << "TRY " << nextv << " " << nnewS << " " << nnewT << endl;

          if (nnewT < best[nextv][nnewS]) {
            best[nextv][nnewS] = nnewT;
            pq.push(Node(nextv, nnewS, nnewT));
          }
        } else {
          break;
        }
      }
    }
  }

  for (int i = 1; i < N; ++i) {
    long long minVal = INF;
    for (int j = 0; j <= UPPER * N; ++j) {
      minVal = min(minVal, best[i][j]);
    }

    cout << minVal << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

struct Edge {
  int to;
  int timeCost;
  int silverCost;
  Edge() {}

  Edge(int pt, int ptc, int psc) : to(pt), timeCost(ptc), silverCost(psc) {}
};

using VLL = vector<long long>;
using VVLL = vector<VLL>;

using VI = vector<int>;
using VVI = vector<VI>;

using VE = vector<Edge>;
using VVE = vector<VE>;

const long long INF = 1000000000LL * 1000000000LL;

int main() {
  int N;
  int M;
  int S;

  cin >> N >> M >> S;

  VVE adja(N);

  VI goldSilver(N);
  VI goldTime(N);

  for (int i = 0; i < M; ++i) {
    int from;
    int to;
    int T;
    int C;
    cin >> from >> to >> C >> T;
    --from;
    --to;
    adja[from].emplace_back(Edge(to, T, C));
    adja[to].emplace_back(Edge(from, T, C));
  }

  for (int i = 0; i < N; ++i) {
    cin >> goldSilver[i] >> goldTime[i];
  }

  VVLL best(N, VLL(N * 60 + 1, INF));

  vector<pair<int, int>> que;
  int front = 0;

  que.push_back(make_pair(0, min(S, 60 * N)));
  best[0][min(S, 60 * N)] = 0;

  while (front < que.size()) {
    pair<int, int> now = que[front++];
    int nowv = now.first;

    // cout << "DEBUG " << front << " " << now.first << " " << now.second <<
    // endl;

    for (int i = 0; i < adja[nowv].size(); ++i) {
      int nextv = adja[nowv][i].to;

      // cout << "INN " << nowv << " " << nextv << endl;

      for (int j = 0; true; ++j) {
        int newS = now.second + j * goldSilver[nowv];
        long long timeCost = best[now.first][now.second] + j * goldTime[nowv];

        if (newS <= 60 * N || j == 0) {
          int nnewS = newS - adja[nowv][i].silverCost;
          long long nnewT = timeCost + adja[nowv][i].timeCost;

          // cout << "TRY " << nextv << " " << nnewS << " " << nnewT << endl;

          if (nnewT < best[nextv][nnewS]) {
            best[nextv][nnewS] = min(best[nextv][nnewS], nnewT);
            que.push_back(make_pair(nextv, nnewS));
          }
        } else {
          break;
        }
      }
    }
  }

  for (int i = 1; i < N; ++i) {
    long long minVal = INF;
    for (int j = 0; j <= 60 * N; ++j) {
      minVal = min(minVal, best[i][j]);
    }

    cout << minVal << endl;
  }

  return 0;
}