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

/*
    なんか変な勘違いをしていた
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

#define INF 0x7fffffffffffffff

typedef pair<long long int, int> Data;
typedef pair<Data, long long int> Pair;
typedef pair<long long int, long long int> Dist;

struct Edge {
  int to;
  long long int len;
};

int n;
int m;
long long int x;
long long int h[100000];
vector<struct Edge> es[100000];
Dist shortest[100000];
priority_queue<Pair, vector<Pair>, greater<Pair>> q;

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;
    struct Edge tmp;

    scanf("%d %d %lld", &a, &b, &tmp.len);
    --a;
    --b;
    tmp.to = b;
    es[a].push_back(tmp);
    tmp.to = a;
    es[b].push_back(tmp);
  }

  fill(shortest, shortest + n, Dist(INF, 0));
  shortest[0].first = 0;
  shortest[0].second = x;
  q.push(Pair(Data(0, 0), x));
  while (!q.empty()) {
    Pair p = q.top();
    q.pop();
    long long int cost = p.first.first;
    int u = p.first.second;
    long long int arrived = p.second;

    if (shortest[u].first < cost - llabs(shortest[u].second - arrived))
      continue;
    for (int i = 0; i < es[u].size(); i++) {
      int v = es[u][i].to;
      long long int len = es[u][i].len;
      long long int newcost;
      long long int pos;

      // printf("%d: updating %d\\n", u, v);
      if (h[u] < len)
        continue;

      if (arrived - len > h[v]) {
        newcost = cost + arrived - h[v];
        pos = h[v];
      } else if (arrived - len < 0) {
        newcost = cost + len * 2 - arrived;
        pos = 0;
      } else {
        newcost = cost + len;
        pos = arrived - len;
      }

      if (shortest[v].first > newcost - llabs(shortest[v].second - pos)) {
        // printf("newcost: %lld pos: %lld\\n", newcost, pos);
        shortest[v].first = newcost;
        shortest[v].second = pos;
        q.push(Pair(Data(newcost, v), pos));
      }
    }
  }

  /*for (int i=0; i<n; i++) {
      printf("%lld ", shortest[i].first);
  }
  puts("");
  for (int i=0; i<n; i++) {
      printf("%lld ", shortest[i].second);
  }
  puts("");*/

  if (shortest[n - 1].first == INF)
    puts("-1");
  else
    printf("%lld\\n", shortest[n - 1].first + h[n - 1] - shortest[n - 1].second);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

#define INF 0x7fffffffffffffff

typedef pair<long long int, int> Data;
typedef pair<Data, long long int> Pair;
typedef pair<long long int, long long int> Dist;

struct Edge {
  int to;
  long long int len;
};

int n;
int m;
long long int x;
long long int h[100000];
vector<struct Edge> es[100000];
Dist shortest[100000];
priority_queue<Pair, vector<Pair>, greater<Pair>> q;

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;
    struct Edge tmp;

    scanf("%d %d %lld", &a, &b, &tmp.len);
    --a;
    --b;
    tmp.to = b;
    es[a].push_back(tmp);
    tmp.to = a;
    es[b].push_back(tmp);
  }

  fill(shortest, shortest + n, Dist(INF, 0));
  shortest[0].first = 0;
  shortest[0].second = x;
  q.push(Pair(Data(0, 0), x));
  while (!q.empty()) {
    Pair p = q.top();
    q.pop();
    long long int cost = p.first.first;
    int u = p.first.second;
    long long int arrived = p.second;

    if (shortest[u].first < cost - llabs(shortest[u].second - arrived))
      continue;
    for (int i = 0; i < es[u].size(); i++) {
      int v = es[u][i].to;
      long long int len = es[u][i].len;
      long long int newcost;
      long long int pos;

      // printf("%d: updating %d\\n", u, v);
      if (h[u] < len)
        continue;

      if (arrived - len > h[v]) {
        newcost = cost + arrived - h[v];
        pos = h[v];
      } else if (arrived - len < 0) {
        newcost = cost + len * 2 - arrived;
        pos = 0;
      } else {
        newcost = cost + len;
        pos = arrived - len;
      }

      if (shortest[v].first >= newcost - llabs(shortest[v].second - pos)) {
        // printf("newcost: %lld pos: %lld\\n", newcost, pos);
        shortest[v].first = newcost;
        shortest[v].second = pos;
        q.push(Pair(Data(newcost, v), pos));
      }
    }
  }

  /*for (int i=0; i<n; i++) {
      printf("%lld ", shortest[i].first);
  }
  puts("");
  for (int i=0; i<n; i++) {
      printf("%lld ", shortest[i].second);
  }
  puts("");*/

  if (shortest[n - 1].first == INF)
    puts("-1");
  else
    printf("%lld\\n", shortest[n - 1].first + h[n - 1] - shortest[n - 1].second);
}