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

#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_set>
#include <vector>

using namespace std;

#define INF (1e18)

typedef pair<long long, long long> P;
typedef pair<long long, P> P2;
typedef pair<long long, P2> P3;

int main() {
  long long N, M, X;
  cin >> N >> M >> X;

  vector<long long> H(N);
  vector<long long> nxt_min(N, INF);
  vector<P> to[N]; // dist, tree
  for (int i = 0; i < N; ++i) {
    cin >> H[i];
  }
  for (int i = 0; i < M; ++i) {
    long long a, b, t;
    cin >> a >> b >> t;
    --a;
    --b;
    if (H[a] >= t) {
      to[a].push_back(P(b, t));
      nxt_min[a] = min(nxt_min[a], t);
    }
    if (H[b] >= t) {
      to[b].push_back(P(a, t));
      nxt_min[b] = min(nxt_min[b], t);
    }
  }

  priority_queue<P2, vector<P2>, greater<P2>> q; // dist, now_tree, rest_height
  vector<long long> dist(N, INF);
  unordered_set<long long> closed;
  long long ans = -1;

  q.push(P2(0, P(0, X)));
  while (!q.empty()) {
    P2 pp = q.top();
    q.pop();
    long long now_dist = pp.first;
    long long now_tree = pp.second.first;
    long long now_rest = pp.second.second;
    if (now_tree == N - 1) {
      ans = now_dist + (H[N - 1] - now_rest);
      break;
    }
    if (closed.count(now_tree) != 0)
      continue;
    closed.insert(now_tree);

    for (int i = 0; i < (int)to[now_tree].size(); ++i) {
      long long nxt_tree = to[now_tree][i].first;
      long long nxt_dist;
      long long nxt_rest = max(0LL, now_rest - to[now_tree][i].second);
      if (closed.count(nxt_tree) != 0)
        continue;
      if (nxt_rest > H[nxt_tree]) {
        nxt_dist = now_dist + to[now_tree][i].second + (nxt_rest - H[nxt_tree]);
        nxt_rest = H[nxt_tree];
      } else if (nxt_rest > 0) {
        nxt_dist = now_dist + to[now_tree][i].second;
      } else {
        nxt_dist = now_dist + 2 * to[now_tree][i].second - now_rest;
      }
      dist[nxt_tree] = nxt_dist;
      q.push(P2(nxt_dist, P(nxt_tree, nxt_rest)));
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_set>
#include <vector>

using namespace std;

#define INF (1e18)

typedef pair<long long, long long> P;
typedef pair<long long, P> P2;
typedef pair<long long, P2> P3;

int main() {
  long long N, M, X;
  cin >> N >> M >> X;

  vector<long long> H(N);
  vector<P> to[N]; // dist, tree
  for (int i = 0; i < N; ++i) {
    cin >> H[i];
  }
  for (int i = 0; i < M; ++i) {
    long long a, b, t;
    cin >> a >> b >> t;
    --a;
    --b;
    if (H[a] >= t)
      to[a].push_back(P(b, t));
    if (H[b] >= t)
      to[b].push_back(P(a, t));
  }

  priority_queue<P2, vector<P2>, greater<P2>> q; // dist, now_tree, rest_height
  vector<long long> dist(N, INF);
  vector<bool> pre_closed(N, false);
  unordered_set<long long> closed;
  long long ans = -1;

  q.push(P2(0, P(0, X)));
  while (!q.empty()) {
    P2 pp = q.top();
    q.pop();
    long long now_dist = pp.first;
    long long now_tree = pp.second.first;
    long long now_rest = pp.second.second;
    if (now_tree == N - 1) {
      ans = now_dist + (H[N - 1] - now_rest);
      break;
    }
    if (closed.count(now_tree) != 0)
      continue;
    if (now_rest == 0 || pre_closed[now_tree]) {
      closed.insert(now_tree);
    }
    bool test_closed = true;
    for (int i = 0; i < (int)to[now_tree].size(); ++i) {
      long long nxt_tree = to[now_tree][i].first;
      long long nxt_dist;
      long long nxt_rest = max(0LL, now_rest - to[now_tree][i].second);
      if (closed.count(nxt_tree) != 0)
        continue;
      if (nxt_rest > H[nxt_tree]) {
        nxt_dist = now_dist + to[now_tree][i].second + (nxt_rest - H[nxt_tree]);
        nxt_rest = H[nxt_tree];
        test_closed = false;
      } else if (nxt_rest > 0) {
        nxt_dist = now_dist + to[now_tree][i].second;
        test_closed = false;
      } else {
        nxt_dist = now_dist + 2 * to[now_tree][i].second - now_rest;
      }
      if (nxt_rest == 0 && dist[nxt_tree] <= nxt_dist)
        continue;
      dist[nxt_tree] = nxt_dist;
      q.push(P2(nxt_dist, P(nxt_tree, nxt_rest)));
      if (test_closed)
        pre_closed[now_tree] = true;
    }
  }

  cout << ans << endl;

  return 0;
}