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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, int> pl;
typedef pair<ll, pl> pll;

#define fir first
#define sec second.first;
#define thr second.second;
#define INF (1ll << 60)

int n, m;
ll h[100800], dist[100800], ah[100800];
vector<pl> g[100800];

void dijkstra(ll height) {
  int i, from, to;
  ll hi, d, l, nw;
  pll tmp;
  priority_queue<pll, vector<pll>, greater<pll>> pq;
  fill(dist, dist + n, INF);
  fill(ah, ah + n, 0ll);
  dist[0] = 0ll;
  ah[i] = height;
  pq.push(pll(dist[0], pl(height, 0)));
  while (!pq.empty()) {
    tmp = pq.top();
    pq.pop();
    from = tmp.thr;
    hi = tmp.sec;
    d = tmp.fir;
    for (i = (g[from].size() - 1); i >= 0; i--) {
      to = g[from][i].second;
      l = g[from][i].first;
      if (l <= hi) {
        nw = d + l;
        if (dist[to] >= nw && h[to] >= hi - l) {
          if (dist[to] != nw) {
            ah[to] = hi - l;
            dist[to] = nw;
            pq.push(pll(dist[to], pl(ah[to], to)));
          } else if (ah[to] < hi - l) {
            ah[to] = hi - l;
            dist[to] = nw;
            pq.push(pll(dist[to], pl(ah[to], to)));
          }
        } else if (dist[to] >= nw) {
          nw += hi - l - h[to];
          if (dist[to] > nw) {
            dist[to] = nw;
            ah[to] = h[to];
            pq.push(pll(dist[to], pl(ah[to], to)));
          } else if (dist[to] == nw && ah[to] < h[to]) {
            dist[to] = nw;
            ah[to] = h[to];
            pq.push(pll(dist[to], pl(ah[to], to)));
          }
        }
      } else if (l <= h[from]) {
        nw = d + l - hi + l;
        if (dist[to] > nw) {
          ah[to] = 0ll;
          dist[to] = nw;
          pq.push(pll(dist[to], pl(ah[to], to)));
        }
      }
    }
  }
}

int main(void) {
  int i, a, b;
  ll x, t;
  cin >> n >> m >> x;
  for (i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (i = 0; i < m; i++) {
    cin >> a >> b >> t;
    a--;
    b--;
    g[a].push_back(pl(t, b));
    g[b].push_back(pl(t, a));
  }
  dijkstra(x);
  if (dist[n - 1] == INF) {
    dist[n - 1] = -1;
  } else {
    dist[n - 1] += h[n - 1] - ah[n - 1];
  }
  cout << dist[n - 1] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, int> pl;
typedef pair<ll, pl> pll;

#define fir first
#define sec second.first;
#define thr second.second;
#define INF (1ll << 60)

int n, m;
ll h[100800], dist[100800], ah[100800];
vector<pl> g[100800];

void dijkstra(ll height) {
  int i, from, to;
  ll hi, d, l, nw;
  pll tmp;
  priority_queue<pll, vector<pll>, greater<pll>> pq;
  fill(dist, dist + n, INF);
  fill(ah, ah + n, 0ll);
  dist[0] = 0ll;
  ah[i] = height;
  pq.push(pll(dist[0], pl(height, 0)));
  while (!pq.empty()) {
    tmp = pq.top();
    pq.pop();
    from = tmp.thr;
    hi = tmp.sec;
    d = tmp.fir;
    for (i = (g[from].size() - 1); i >= 0; i--) {
      to = g[from][i].second;
      l = g[from][i].first;
      if (l <= hi) {
        nw = d + l;
        if (dist[to] >= nw && h[to] >= hi - l) {
          if (dist[to] == nw) {
            ah[to] = max(ah[to], hi - l);
          } else {
            ah[to] = hi - l;
          }
          dist[to] = nw;
          pq.push(pll(dist[to], pl(ah[to], to)));
        } else if (dist[to] >= nw) {
          nw += hi - l - h[to];
          if (dist[to] > nw) {
            dist[to] = nw;
            ah[to] = h[to];
            pq.push(pll(dist[to], pl(ah[to], to)));
          } else if (dist[to] == nw) {
            dist[to] = nw;
            ah[to] = max(ah[to], h[to]);
            pq.push(pll(dist[to], pl(ah[to], to)));
          }
        }
      } else if (l <= h[from]) {
        nw = d + l - hi + l;
        if (dist[to] >= nw) {
          if (dist[to] != nw) {
            ah[to] = 0ll;
          }
          dist[to] = nw;
          pq.push(pll(dist[to], pl(ah[to], to)));
        }
      }
    }
  }
}

int main(void) {
  int i, a, b;
  ll x, t;
  cin >> n >> m >> x;
  for (i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (i = 0; i < m; i++) {
    cin >> a >> b >> t;
    a--;
    b--;
    g[a].push_back(pl(t, b));
    g[b].push_back(pl(t, a));
  }
  dijkstra(x);
  if (dist[n - 1] == INF) {
    dist[n - 1] = -1;
  } else {
    dist[n - 1] += h[n - 1] - ah[n - 1];
  }
  cout << dist[n - 1] << endl;
  return 0;
}