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

//??????????§????(??????or???????????????????????£?????????)???????????¨,
//????¨??????????t??§??¨?????°???????????¨???, t<X??????X-t[m],
//t>=X??????0[m]?????????????????????
//??????????????????????????????????????£???, (??????,
//????????§?????????)????????§??????????§????????±??????????
//????????????????????????????????????????????§????????????????°?????????????(2?§???????2m????????????????????£?????¨?????????,
//2?§????????????¨??????????????°??????)?????§????????????????????????????????§?§£?????????
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
using namespace std;

int n, m, x;
int h[100000];
vector<int> et[100000];
vector<int> ec[100000];

signed main() {
  cin >> n >> m >> x;
  for (int i = 0; i < n; i++)
    cin >> h[i];

  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    et[a].push_back(b);
    et[b].push_back(a);
    ec[a].push_back(c);
    ec[b].push_back(c);
  }

  static int minCost[100000];
  for (int i = 0; i < n; i++)
    minCost[i] = 1145141919893810;

  typedef pair<int, int> P; // cost, pos
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P now = que.top();
    que.pop();
    int cst = now.first;
    int pos = now.second;
    if (minCost[pos] <= cst)
      continue;
    minCost[pos] = cst;

    for (int i = 0; i < et[pos].size(); i++) {
      int height = max(0LL, x - cst);
      int npos = et[pos][i];

      if (height - ec[pos][i] > h[npos]) {
        que.push(P(x - h[npos], npos)); // Down
      } else if (height - ec[pos][i] > 0) {
        que.push(P(cst + ec[pos][i], npos)); // Sonomama
      } else {
        if (h[pos] < ec[pos][i])
          continue;                                       // Cannot move
        que.push(P(cst + 2 * ec[pos][i] - height, npos)); // Up -> move
      }
    }
  }
  if (minCost[n - 1] >= 1145141919893810) {
    cout << -1 << endl;
  } else {
    cout << minCost[n - 1] + h[n - 1] - max(0LL, x - minCost[n - 1]) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

//??????????§????(??????or???????????????????????£?????????)???????????¨,
//????¨??????????t??§??¨?????°???????????¨???, t<X??????X-t[m],
//t>=X??????0[m]?????????????????????
//??????????????????????????????????????£???, (??????,
//????????§?????????)????????§??????????§????????±??????????
//????????????????????????????????????????????§????????????????°?????????????(2?§???????2m????????????????????£?????¨?????????,
//2?§????????????¨??????????????°??????)?????§????????????????????????????????§?§£?????????
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
using namespace std;

int n, m, x;
int h[100000];
vector<int> et[100000];
vector<int> ec[100000];

signed main() {
  cin >> n >> m >> x;
  for (int i = 0; i < n; i++)
    cin >> h[i];

  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    et[a].push_back(b);
    et[b].push_back(a);
    ec[a].push_back(c);
    ec[b].push_back(c);
  }

  static int minCost[100000];
  for (int i = 0; i < n; i++)
    minCost[i] = 1145141919893810;

  typedef pair<int, int> P; // cost, pos
  priority_queue<P> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P now = que.top();
    que.pop();
    int cst = now.first;
    int pos = now.second;
    if (minCost[pos] <= cst)
      continue;
    minCost[pos] = cst;

    for (int i = 0; i < et[pos].size(); i++) {
      int height = max(0LL, x - cst);
      int npos = et[pos][i];

      if (height - ec[pos][i] > h[npos]) {
        que.push(P(x - h[npos], npos)); // Down
      } else if (height - ec[pos][i] > 0) {
        que.push(P(cst + ec[pos][i], npos)); // Sonomama
      } else {
        if (h[pos] < ec[pos][i])
          continue;                                       // Cannot move
        que.push(P(cst + 2 * ec[pos][i] - height, npos)); // Up -> move
      }
    }
  }
  if (minCost[n - 1] >= 1145141919893810) {
    cout << -1 << endl;
  } else {
    cout << minCost[n - 1] + h[n - 1] - max(0LL, x - minCost[n - 1]) << endl;
  }
  return 0;
}