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

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, pii> P;

const ll INF = 100000000000007;

int n, m, x;
int h[100000];
ll d[100000];
vector<pii> G[100000];
priority_queue<P, vector<P>, greater<P>> q;

int main() {
  scanf("%d %d %d", &n, &m, &x);
  for (int i = 0; i < n; i++)
    scanf("%d", h + i);
  for (int i = 0; i < m; i++) {
    int a, b, t;
    scanf("%d %d %d", &a, &b, &t);
    a--;
    b--;
    G[a].push_back(pii(b, t));
    G[b].push_back(pii(a, t));
  }
  fill(d, d + n, INF);
  d[0] = 0;
  q.push(P(0, pii(0, x)));
  ll ans = INF;
  while (!q.empty()) {
    int tree = q.top().second.first, pos = q.top().second.second;
    ll c = q.top().first;
    if (tree == n - 1) {
      ans = min(ans, c + h[n - 1] - pos);
    }
    q.pop();
    if (d[tree] < c)
      continue;
    for (int i = 0; i < G[tree].size(); i++) {
      int next_tree = G[tree][i].first, next_pos = pos - G[tree][i].second;
      ll cost = d[tree] + G[tree][i].second;
      if (h[tree] - G[tree][i].second < 0)
        cost = INF;
      else if (next_pos < 0) {
        cost -= next_pos;
        next_pos = 0;
      } else if (next_pos > h[next_tree]) {
        cost += next_pos - h[next_tree];
        next_pos = h[next_tree];
      }
      if (d[next_tree] > cost) {
        d[next_tree] = min(d[next_tree], cost);
        q.push(P(d[next_tree], pii(next_tree, next_pos)));
      }
    }
  }
  if (ans == INF)
    printf("-1\\n");
  else
    printf("%lld\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, pii> P;

const ll INF = 100000000000007;

int n, m, x;
int h[100000];
ll d[100000];
vector<pii> G[100000];
priority_queue<P, vector<P>, greater<P>> q;

int main() {
  scanf("%d %d %d", &n, &m, &x);
  for (int i = 0; i < n; i++)
    scanf("%d", h + i);
  for (int i = 0; i < m; i++) {
    int a, b, t;
    scanf("%d %d %d", &a, &b, &t);
    a--;
    b--;
    G[a].push_back(pii(b, t));
    G[b].push_back(pii(a, t));
  }
  fill(d, d + n, INF);
  d[0] = 0;
  q.push(P(0, pii(0, x)));
  ll ans = INF;
  while (!q.empty()) {
    int tree = q.top().second.first, pos = q.top().second.second;
    ll c = q.top().first;
    if (tree == n - 1) {
      ans = min(ans, c + h[n - 1] - pos);
    }
    q.pop();
    if (d[tree] < c)
      continue;
    for (int i = 0; i < G[tree].size(); i++) {
      int next_tree = G[tree][i].first, next_pos = pos - G[tree][i].second;
      ll cost = d[tree] + G[tree][i].second;
      if (h[tree] - G[tree][i].second < 0)
        cost = INF;
      else if (next_pos < 0) {
        cost -= next_pos;
        next_pos = 0;
      } else if (next_pos > h[next_tree]) {
        cost += next_pos - h[next_tree];
        next_pos = h[next_tree];
      }
      if (d[next_tree] >= cost) {
        d[next_tree] = min(d[next_tree], cost);
        q.push(P(d[next_tree], pii(next_tree, next_pos)));
      }
    }
  }
  if (ans == INF)
    printf("-1\\n");
  else
    printf("%lld\\n", ans);
  return 0;
}