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

#include <bitset>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, long long int> pi;
typedef pair<long long int, pi> plpi;
#define F first
#define S second
#define PB push_back
const int N = 1e5 + 10;
const long long int INF = 1LL << 60;
int main() {
  int n, m, l, r;
  long long int ans = INF, dis[N], d, h[N], x;
  bitset<N> went;
  queue<plpi> q;
  priority_queue<plpi, vector<plpi>, greater<plpi>> pq;
  plpi nxt;
  vector<pi> graph[N];
  scanf("%d%d%lld", &n, &m, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &h[i]);
    dis[i] = INF;
  }
  while (m--) {
    scanf("%d%d%lld", &l, &r, &d);
    if (h[l] - d >= 0)
      graph[l].PB({r, d});
    if (h[r] - d >= 0)
      graph[r].PB({l, d});
  }
  went.reset();
  pq.push({0LL, {1, x}});
  while (!pq.empty()) {
    nxt = pq.top();
    pq.pop();
    if (nxt.F >= ans)
      continue;
    if (nxt.S.F == n) {
      ans = min(ans, nxt.F + h[n] - nxt.S.S);
      break;
    } else if (nxt.S.S == 0) {
      if (!went[nxt.S.F] && dis[nxt.S.F] >= nxt.F) {
        went[nxt.S.F] = true;
        for (pi i : graph[nxt.S.F])
          if (nxt.F + i.S * 2 < dis[i.F]) {
            dis[i.F] = nxt.F + i.S * 2;
            pq.push({nxt.F + i.S * 2, {i.F, 0}});
          }
      }
    } else if (!went[nxt.S.F]) {
      went[nxt.S.F] = true;
      for (pi i : graph[nxt.S.F]) {
        if (nxt.S.S - i.S < 0) {
          if (dis[i.F] > nxt.F - nxt.S.S + i.S * 2) {
            dis[i.F] = nxt.F - nxt.S.S + i.S * 2;
            pq.push({nxt.F - nxt.S.S + i.S * 2, {i.F, 0}});
          }
        } else if (nxt.S.S - i.S > h[i.F]) {
          if (dis[i.F] > nxt.F + nxt.S.S - h[i.F] * 2) {
            dis[i.F] = min(dis[i.F], nxt.F + nxt.S.S);
            pq.push({nxt.F + nxt.S.S - h[i.F], {i.F, h[i.F]}});
          }
        } else {
          if (dis[i.F] > nxt.F + i.S * 2 - nxt.S.S) {
            dis[i.F] = min(dis[i.F], nxt.S.S + nxt.F);
            pq.push({nxt.F + i.S, {i.F, nxt.S.S - i.S}});
          }
        }
      }
    }
  }
  if (ans == INF)
    printf("-1\\n");
  else
    printf("%lld\\n", ans);
}


Error: 0

Fixed submission:

#include <bitset>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, long long int> pi;
typedef pair<long long int, pi> plpi;
#define F first
#define S second
#define PB push_back
const int N = 1e5 + 10;
const long long int INF = 1LL << 60;
int main() {
  int n, m, l, r;
  long long int ans = INF, dis[N], d, h[N], x;
  bitset<N> went;
  queue<plpi> q;
  priority_queue<plpi, vector<plpi>, greater<plpi>> pq;
  plpi nxt;
  vector<pi> graph[N];
  scanf("%d%d%lld", &n, &m, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &h[i]);
    dis[i] = INF;
  }
  while (m--) {
    scanf("%d%d%lld", &l, &r, &d);
    if (h[l] - d >= 0)
      graph[l].PB({r, d});
    if (h[r] - d >= 0)
      graph[r].PB({l, d});
  }
  went.reset();
  pq.push({0LL, {1, x}});
  while (!pq.empty()) {
    nxt = pq.top();
    pq.pop();
    if (nxt.F >= ans)
      continue;
    if (nxt.S.F == n) {
      ans = min(ans, nxt.F + h[n] - nxt.S.S);
      break;
    } else if (nxt.S.S == 0) {
      if (!went[nxt.S.F] && dis[nxt.S.F] >= nxt.F) {
        went[nxt.S.F] = true;
        for (pi i : graph[nxt.S.F])
          if (nxt.F + i.S * 2 < dis[i.F]) {
            dis[i.F] = nxt.F + i.S * 2;
            pq.push({nxt.F + i.S * 2, {i.F, 0}});
          }
      }
    } else if (!went[nxt.S.F]) {
      for (pi i : graph[nxt.S.F]) {
        if (nxt.S.S - i.S < 0) {
          if (dis[i.F] > nxt.F - nxt.S.S + i.S * 2) {
            dis[i.F] = nxt.F - nxt.S.S + i.S * 2;
            pq.push({nxt.F - nxt.S.S + i.S * 2, {i.F, 0}});
          }
        } else if (nxt.S.S - i.S > h[i.F]) {
          if (dis[i.F] > nxt.F + nxt.S.S - h[i.F] * 2) {
            dis[i.F] = min(dis[i.F], nxt.F + nxt.S.S);
            pq.push({nxt.F + nxt.S.S - h[i.F], {i.F, h[i.F]}});
          }
        } else {
          if (dis[i.F] > nxt.F + i.S * 2 - nxt.S.S) {
            dis[i.F] = min(dis[i.F], nxt.S.S + nxt.F);
            pq.push({nxt.F + i.S, {i.F, nxt.S.S - i.S}});
          }
        }
      }
    }
  }
  if (ans == INF)
    printf("-1\\n");
  else
    printf("%lld\\n", ans);
}