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

#include <bits/stdc++.h>

int32_t N, X;
int32_t H[100000];
constexpr int64_t INF = 1000000000000000;
struct edge {
  int32_t to, cost;
  edge() {}
  edge(int32_t to, int32_t cost) : to(to), cost(cost) {}
};
std::vector<edge> graph[100000];
using PASS_TIME = std::pair<int64_t, int32_t>; // cost,height
PASS_TIME D[100000];
void dijk(int32_t s) {
  static bool used[100000] = {};
  using P = std::pair<PASS_TIME, int32_t>;
  std::priority_queue<P, std::vector<P>, std::greater<P>> que;
  for (auto &i : D) {
    i.first = INF;
  }
  D[s].first = 0;
  D[s].second = X;
  que.emplace(D[s], s);
  while (!que.empty()) {
    auto now_state = que.top();
    que.pop();
    const auto &pos = now_state.second;
    const auto &height = now_state.first.second;
    const auto &cost = now_state.first.first;

    if (used[pos]) {
      continue;
    }
    used[pos] = true;

    for (auto &e : graph[pos]) {
      auto ncost = cost + e.cost;
      auto nheight = 0;
      if (e.cost <= height) {
        nheight = height - e.cost;
        if (H[e.to] < nheight) {
          ncost += nheight - H[e.to];
          nheight = H[e.to];
        }
      } else {
        if (H[pos] < e.cost) {
#ifdef DEBUG
          std::cout << pos << '>' << e.to << ':' << H[pos] << ' ' << e.cost
                    << "\\n";
#endif
          continue;
        }
        ncost += (e.cost - height);
      }
      if (D[e.to].first > ncost) {
        D[e.to].first = ncost;
        D[e.to].second = nheight;
        que.emplace(D[e.to], e.to);
      }
    }
  }
}

int main() {
  int32_t M;
  std::cin.sync_with_stdio(false);
  std::cout.sync_with_stdio(false);
  std::cin >> N >> M >> X;
  for (int32_t i = 0; i < N; ++i) {
    std::cin >> H[i];
  }
  for (int32_t i = 0; i < M; ++i) {
    int32_t a, b, T;
    std::cin >> a >> b >> T;
    --a;
    --b;
    graph[a].emplace_back(b, T);
    graph[b].emplace_back(a, T);
  }

#ifdef DEBUG
  std::cout << "=graph====" << graph[0].size() << "=====\\n";
  std::cout << "===================\\n";
#endif
  dijk(0);
#ifdef DEBUG
  for (int32_t i = 0; i < N; ++i) {
    std::cout << "===================\\n";
    std::cout << D[i].first << ' ' << D[i].second << std::endl;
  }
  std::cout << "===================\\n";
#endif

  if (D[N - 1].first < INF) {
    std::cout << D[N - 1].first + (H[N - 1] - D[N - 1].second) << std::endl;
  } else {
    std::cout << -1 << std::endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

int32_t N, X;
int32_t H[100000];
int32_t T[100000];
constexpr int64_t INF = 1000000000000000;
struct edge {
  int32_t to, cost;
  edge() {}
  edge(int32_t to, int32_t cost) : to(to), cost(cost) {}
};
std::vector<edge> graph[100000];
using PASS_TIME = std::pair<int64_t, int32_t>; // cost,height
PASS_TIME D[100000];
void dijk(int32_t s) {
  static bool used[100000] = {};
  using P = std::pair<PASS_TIME, int32_t>;
  std::priority_queue<P, std::vector<P>, std::greater<P>> que;
  for (auto &i : D) {
    i.first = INF;
  }
  D[s].first = 0;
  D[s].second = X;
  que.emplace(D[s], s);
  while (!que.empty()) {
    auto now_state = que.top();
    que.pop();
    const auto &pos = now_state.second;
    const auto &height = now_state.first.second;
    const auto &cost = now_state.first.first;

    if (used[pos]) {
      continue;
    }
    used[pos] = true;

    for (auto &e : graph[pos]) {
      auto ncost = cost + e.cost;
      auto nheight = 0;
      if (e.cost <= height) {
        nheight = height - e.cost;
        if (H[e.to] < nheight) {
          ncost += nheight - H[e.to];
          nheight = H[e.to];
        }
      } else {
        if (H[pos] < e.cost) {
#ifdef DEBUG
          std::cout << pos << '>' << e.to << ':' << H[pos] << ' ' << e.cost
                    << "\\n";
#endif
          continue;
        }
        ncost += (e.cost - height);
      }
      if (D[e.to].first > ncost) {
        D[e.to].first = ncost;
        D[e.to].second = nheight;
        que.emplace(D[e.to], e.to);
      }
    }
  }
}

int main() {
  int32_t M;
  std::cin.sync_with_stdio(false);
  std::cout.sync_with_stdio(false);
  std::cin >> N >> M >> X;
  for (int32_t i = 0; i < N; ++i) {
    std::cin >> H[i];
  }
  for (int32_t i = 0; i < M; ++i) {
    int32_t a, b;
    std::cin >> a >> b >> T[i];
    --a;
    --b;
    graph[a].emplace_back(b, T[i]);
    graph[b].emplace_back(a, T[i]);
  }

#ifdef DEBUG
  std::cout << "=graph====" << graph[0].size() << "=====\\n";
  std::cout << "===================\\n";
#endif
  dijk(0);
#ifdef DEBUG
  for (int32_t i = 0; i < N; ++i) {
    std::cout << "===================\\n";
    std::cout << D[i].first << ' ' << D[i].second << std::endl;
  }
  std::cout << "===================\\n";
#endif

  if (D[N - 1].first < INF) {
    std::cout << D[N - 1].first + (H[N - 1] - D[N - 1].second) << std::endl;
  } else {
    std::cout << -1 << std::endl;
  }
}