Problem p01146 (C++)

Buggy submission (status: Runtime Error):
#ifndef _WIN32
#include <iostream>
#endif // !_WIN32

#include <algorithm>
#include <queue>
#include <tuple>
#include <vector>

using namespace std;

typedef long long LL;
const LL INF = 1e17;

#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)

typedef tuple<LL, int, int> edge;
typedef priority_queue<edge> QUE;
typedef vector<LL> V;
typedef vector<V> VV;
int N, M, L, E, s, t;
int a[1123];
int b[1123];
LL c[1123];
int l[1123];
VV dist(int s, VV g) {
  VV res(N, V(M + 1, INF));
  QUE que;
  que.push(edge{0, s, M});
  res[s][M] = 0;
  while (que.size()) {
    LL cost;
    int v, m;
    tie(cost, v, m) = que.top();
    que.pop();
    cost = -cost;
    if (res[v][m] < cost)
      continue;
    if (l[v] == 1 && m < M) {
      if (res[v][m + 1] > cost + 1) {
        res[v][m + 1] = cost + 1;
        que.push(edge{-cost - 1, v, m + 1});
      }
    }
    for (auto &e : g[v]) {
      int u = v ^ a[e] ^ b[e];
      int nxt_m = m - c[e];
      if (nxt_m < 0)
        continue;
      LL nxt_cost = cost + c[e];
      if (res[u][nxt_m] > nxt_cost) {
        res[u][nxt_m] = nxt_cost;
        que.push(edge{-nxt_cost, u, nxt_m});
      }
    }
  }

  return res;
}
int main() {
  while (cin >> N >> M >> L >> E >> s >> t, N + M + L + E + s + t) {
    REP(i, N) l[i] = 0;
    REP(i, L) {
      int p;
      cin >> p;
      l[p] = 1;
    }
    VV g(N);
    REP(i, E) {
      cin >> a[i] >> b[i] >> c[i];
      g[a[i]].push_back(i);
      g[b[i]].push_back(i);
    }
    auto d = dist(s, g);
    LL res = INF;
    REP(i, M + 1) res = min(res, d[t][i]);
    if (res == INF)
      cout << "Help!" << endl;
    else
      cout << res << endl;
  }
}

Fixed submission:
#ifndef _WIN32
#include <iostream>
#endif // !_WIN32

#include <algorithm>
#include <queue>
#include <tuple>
#include <vector>

using namespace std;

typedef long long LL;
const LL INF = 1e17;

#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)

typedef tuple<LL, int, int> edge;
typedef priority_queue<edge> QUE;
typedef vector<LL> V;
typedef vector<V> VV;
int N, M, L, E, s, t;
int a[112345];
int b[112345];
LL c[112345];
int l[1123];
VV dist(int s, VV g) {
  VV res(N, V(M + 1, INF));
  QUE que;
  que.push(edge{0, s, M});
  res[s][M] = 0;
  while (que.size()) {
    LL cost;
    int v, m;
    tie(cost, v, m) = que.top();
    que.pop();
    cost = -cost;
    if (res[v][m] < cost)
      continue;
    if (l[v] == 1 && m < M) {
      if (res[v][m + 1] > cost + 1) {
        res[v][m + 1] = cost + 1;
        que.push(edge{-cost - 1, v, m + 1});
      }
    }
    for (auto &e : g[v]) {
      int u = v ^ a[e] ^ b[e];
      int nxt_m = m - c[e];
      if (nxt_m < 0)
        continue;
      LL nxt_cost = cost + c[e];
      if (res[u][nxt_m] > nxt_cost) {
        res[u][nxt_m] = nxt_cost;
        que.push(edge{-nxt_cost, u, nxt_m});
      }
    }
  }

  return res;
}
int main() {
  while (cin >> N >> M >> L >> E >> s >> t, N + M + L + E + s + t) {
    REP(i, N) l[i] = 0;
    REP(i, L) {
      int p;
      cin >> p;
      l[p] = 1;
    }
    VV g(N);
    REP(i, E) {
      cin >> a[i] >> b[i] >> c[i];
      g[a[i]].push_back(i);
      g[b[i]].push_back(i);
    }
    auto d = dist(s, g);
    LL res = INF;
    REP(i, M + 1) res = min(res, d[t][i]);
    if (res == INF)
      cout << "Help!" << endl;
    else
      cout << res << endl;
  }
}