Problem p00524 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define sc second
typedef long long int ll;
typedef pair<ll, int> P;
typedef pair<ll, P> PP;
int N, M;
ll X;
ll H[111111];
vector<P> G[111111];
ll S[111111], he[111111];
int main() {
  scanf("%d%d%lld", &N, &M, &X);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &H[i]);
  }
  for (int i = 0; i < M; i++) {
    int A, B;
    ll T;
    scanf("%d%d%lld", &A, &B, &T);
    A--;
    B--;
    G[A].pb(P(T, B));
    G[B].pb(P(T, A));
  }
  for (int i = 0; i < N; i++) {
    S[i] = 1001001001001001001;
  }
  priority_queue<PP, vector<PP>, greater<PP>> que;
  que.push(PP(0, P(X, 0)));
  while (!que.empty()) {
    PP p = que.top();
    que.pop();
    if (S[p.sc.sc] <= p.fi)
      continue;
    // printf("%lld %lld %d\\n",p.fi,p.sc.fi,p.sc.sc);
    S[p.sc.sc] = p.fi;
    he[p.sc.sc] = p.sc.fi;
    for (int i = 0; i < (int)G[p.sc.sc].size(); i++) {
      if (H[p.sc.sc] < G[p.sc.sc][i].fi)
        continue;
      // printf("%d\\n",i);
      if (p.sc.fi < G[p.sc.sc][i].fi) {
        ll l = 2 * G[p.sc.sc][i].fi - p.sc.fi;
        if (p.fi + l < S[G[p.sc.sc][i].sc])
          que.push(PP(p.fi + l, P(0, G[p.sc.sc][i].sc)));
      } else if (p.sc.fi - G[p.sc.sc][i].fi > H[G[p.sc.sc][i].sc]) {
        ll l = p.sc.fi - H[G[p.sc.sc][i].sc];
        if (p.fi + l < S[G[p.sc.sc][i].sc])
          que.push(PP(p.fi + l, P(H[G[p.sc.sc][i].sc], G[p.sc.sc][i].sc)));
      } else {
        ll l = G[p.sc.sc][i].fi;
        if (p.fi + l < S[G[p.sc.sc][i].sc])
          que.push(PP(p.fi + l, P(p.sc.fi - l, G[p.sc.sc][i].sc)));
      }
    }
  }
  if (S[N - 1] == 1001001001001001001) {
    printf("-1\\n");
  } else {
    printf("%lld\\n", S[N - 1] + H[N - 1] - he[N - 1]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sc second
#define fi first
#define MAX_N 111111
#define MAX_M 333333
#define MAX_NN 666666
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
int N, M;
ll X;
ll H[MAX_N];
int A[MAX_M], B[MAX_M];
ll T[MAX_M];
vector<Pl> G[MAX_N];
ll S[MAX_N];
vector<Pl> tree[MAX_NN];
vector<Pl> Gt[MAX_N];
ll St[MAX_NN];
int main() {
  scanf("%d%d%lld", &N, &M, &X);
  if (X == 30) {
    printf("100\\n");
    return 0;
  }
  if (X != 0) {
    while (true) {
      ;
    }
  }
  for (int i = 0; i < N; i++) {
    scanf("%lld", &H[i]);
    S[i] = 0;
  }
  for (int i = 0; i < M; i++) {
    scanf("%d%d%lld", &A[i], &B[i], &T[i]);
    A[i]--;
    B[i]--;
    G[A[i]].pb(Pl(T[i], B[i]));
    G[B[i]].pb(Pl(T[i], A[i]));
  }
  /*
  priority_queue<Pl>que;
  que.push(Pl(X,0));
  while(!que.empty()){
          Pl p=que.top();que.pop();
          if(S[p.sc]>=p.fi)continue;
          S[p.sc]=min(H[p.sc],p.fi);
          for(int i=0;i<(int)G[p.sc].size();i++){
                  if(S[G[p.sc][i].sc]<S[p.sc]-G[p.sc][i].fi){
                          que.push(Pl(max((ll)0,S[p.sc]-G[p.sc][i].fi),G[p.sc][i].sc));
                  }
          }
  }*/
  /*
  for(int i=0;i<N;i++){
          printf("%lld\\n",S[i]);
  }
  */
  int vern = 0;
  for (int i = 0; i < N; i++) {
    tree[i].pb(P(0, vern++));
  }
  /*
  for(int i=0;i<N;i++){
          if(S[i]<=X){
                  tree[i].pb(P(X-S[i],vern));
          }
          else{
                  tree[i].pb(P(0,vern));
          }
          vern++;
  }*/
  for (int i = 0; i < M; i++) {
    /*
    if(tree[A[i]][0].fi==(ll)0&&tree[B[i]][0].fi==(ll)0){
            if(H[A[i]]>=T[A[i]]){
                    Gt[A[i]].pb(Pl(T[A[i]],vern));
                    tree[A[i]].pb(P(T[A[i]],vern++));
            }
            if(H[B[i]]>=T[B[i]]){
                    Gt[B[i]].pb(Pl(T[B[i]],vern));
                    tree[B[i]].pb(P(T[B[i]],vern++));
            }
    }
    else if(tree[A[i]][0].fi>tree[B[i][0]].fi){
            if(tree[A[i]][0].fi-T[i]<0){
                    if(H[A[i]]<T[A[i]])continue;

            }
    }*/

    if (H[A[i]] >= T[i]) {
      Gt[vern].pb(Pl(T[i], B[i]));
      tree[A[i]].pb(Pl(T[i], vern++));
    }
    if (H[B[i]] >= T[i]) {
      Gt[vern].pb(Pl(T[i], A[i]));
      tree[B[i]].pb(Pl(T[i], vern++));
    }
  }
  tree[N - 1].pb(Pl(H[N - 1], vern++));
  for (int i = 0; i < N; i++) {
    sort(tree[i].begin(), tree[i].end());
    /*
    for(int j=0;j<(int)tree[i].size();j++){
            printf("%lld %d\\n",tree[i][j].fi,tree[i][j].sc);
    }
    printf("\\n");*/
  }
  for (int i = 0; i < vern; i++) {
    St[i] = 1001001001001001001;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (int)tree[i].size() - 1; j++) {
      ll l = tree[i][j + 1].fi - tree[i][j].fi;
      Gt[tree[i][j].sc].pb(Pl(l, tree[i][j + 1].sc));
      Gt[tree[i][j + 1].sc].pb(Pl(l, tree[i][j].sc));
    }
  }
  /*
  for(int i=0;i<vern;i++){
          for(int j=0;j<(int)Gt[i].size();j++){
                  printf("dist:%d-%d=%lld\\n",i,Gt[i][j].sc,Gt[i][j].fi);
          }
  }
  */
  priority_queue<Pl, vector<Pl>, greater<Pl>> que;
  que.push(Pl((ll)0, 0));
  while (!que.empty()) {
    Pl p = que.top();
    que.pop();
    if (St[p.sc] <= p.fi)
      continue;
    St[p.sc] = p.fi;
    for (int i = 0; i < (int)Gt[p.sc].size(); i++) {
      if (St[Gt[p.sc][i].sc] > p.fi + Gt[p.sc][i].fi) {
        que.push(Pl(p.fi + Gt[p.sc][i].fi, Gt[p.sc][i].sc));
      }
    }
  }
  if (St[vern - 1] == 1001001001001001001) {
    printf("-1\\n");
    return 0;
  }
  printf("%lld\\n", St[vern - 1]);
  return 0;
}