Problem p00524 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define N 100000
#define M 300000
#define HT 1000000000
#define INF 2e18
#define int long long
#define MP(i, j) make_pair((i), (j))
typedef pair<int, int> P;
int n, m, x, ans = INF, tree[N + 1];
vector<P> g[N + 1];
int flag[N + 1] = {};

struct S {
  int to, ti, nh;
  S(int i_to, int i_ti, int i_nh) : to(i_to), ti(i_ti), nh(i_nh) {}
  bool operator<(const S &s) const {
    if (ti != s.ti)
      return ti > s.ti;
  }
};
priority_queue<S> pq;
void dijkstra(int sta) {
  for (int i = 0; i < g[1].size(); i++) {
    if (tree[1] >= g[1][i].second) {
      if (sta >= g[1][i].second) {
        if (sta > g[1][i].second + tree[g[1][i].first]) {
          pq.push(
              {g[1][i].first, sta - tree[g[1][i].first], tree[g[1][i].first]});
        } else {
          pq.push({g[1][i].first, g[1][i].second, sta - g[1][i].second});
        }
      } else {
        if (abs(sta - g[1][i].second) <= tree[1])
          pq.push(
              {g[1][i].first, abs(sta - g[1][i].second) + g[1][i].second, 0});
      }
    }
  }
  while (!pq.empty()) {
    S s = pq.top();
    pq.pop();
    if (flag[s.to] && flag[s.to] < s.ti)
      continue;
    flag[s.to] = s.ti;
    if (s.to == n) {
      ans = min(ans, tree[n] - s.nh + flag[s.to]);
      break;
    }
    for (int i = 0; i < g[s.to].size(); i++) {
      if (tree[s.to] >= g[s.to][i].second) {
        if (s.nh >= g[s.to][i].second) {
          if (s.nh > g[s.to][i].second + tree[g[s.to][i].first]) {
            pq.push({g[s.to][i].first, s.nh - tree[g[s.to][i].first] + s.ti,
                     tree[g[s.to][i].first]});
          } else {
            pq.push({g[s.to][i].first, g[s.to][i].second + s.ti,
                     s.nh - g[s.to][i].second});
          }
        } else {
          if (g[s.to][i].second - s.nh <= tree[s.to])
            pq.push({g[s.to][i].first,
                     g[s.to][i].second - s.nh + g[s.to][i].second + s.ti, 0});
        }
      }
    }
  }
}
signed main() {
  scanf("%lld%lld%lld", &n, &m, &x);
  for (int i = 1; i <= n; i++)
    scanf("%lld", tree + i);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%lld%lld%lld", &a, &b, &c);
    g[a].push_back(MP(b, c));
    g[b].push_back(MP(a, c));
  }
  dijkstra(x);
  if (ans != INF)
    cout << ans << endl;
  else
    cout << -1 << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define N 100000
#define M 300000
#define HT 1000000000
#define INF 2e18
#define int long long
#define MP(i, j) make_pair((i), (j))
typedef pair<int, int> P;
int n, m, x, ans = INF, tree[N + 1];
vector<P> g[N + 1];
int flag[N + 1] = {};

struct S {
  int to, ti, nh;
  S(int i_to, int i_ti, int i_nh) : to(i_to), ti(i_ti), nh(i_nh) {}
  bool operator<(const S &s) const {
    if (ti != s.ti)
      return ti > s.ti;
  }
};
priority_queue<S> pq;
void dijkstra(int sta) {
  for (int i = 0; i < g[1].size(); i++) {
    if (tree[1] >= g[1][i].second) {
      if (sta >= g[1][i].second) {
        if (sta > g[1][i].second + tree[g[1][i].first]) {
          pq.push(
              {g[1][i].first, sta - tree[g[1][i].first], tree[g[1][i].first]});
        } else {
          pq.push({g[1][i].first, g[1][i].second, sta - g[1][i].second});
        }
      } else {
        if (abs(sta - g[1][i].second) <= tree[1])
          pq.push(
              {g[1][i].first, abs(sta - g[1][i].second) + g[1][i].second, 0});
      }
    }
  }
  while (!pq.empty()) {
    S s = pq.top();
    pq.pop();
    if (flag[s.to] && flag[s.to] <= s.ti)
      continue;
    flag[s.to] = s.ti;
    if (s.to == n) {
      ans = min(ans, tree[n] - s.nh + flag[s.to]);
      break;
    }
    for (int i = 0; i < g[s.to].size(); i++) {
      if (tree[s.to] >= g[s.to][i].second) {
        if (s.nh >= g[s.to][i].second) {
          if (s.nh > g[s.to][i].second + tree[g[s.to][i].first]) {
            pq.push({g[s.to][i].first, s.nh - tree[g[s.to][i].first] + s.ti,
                     tree[g[s.to][i].first]});
          } else {
            pq.push({g[s.to][i].first, g[s.to][i].second + s.ti,
                     s.nh - g[s.to][i].second});
          }
        } else {
          if (g[s.to][i].second - s.nh <= tree[s.to])
            pq.push({g[s.to][i].first,
                     g[s.to][i].second - s.nh + g[s.to][i].second + s.ti, 0});
        }
      }
    }
  }
}
signed main() {
  scanf("%lld%lld%lld", &n, &m, &x);
  for (int i = 1; i <= n; i++)
    scanf("%lld", tree + i);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%lld%lld%lld", &a, &b, &c);
    g[a].push_back(MP(b, c));
    g[b].push_back(MP(a, c));
  }
  dijkstra(x);
  if (ans != INF)
    cout << ans << endl;
  else
    cout << -1 << endl;
}