Problem p01146 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()

int const inf = 1 << 29;
struct Edge {
  Edge(int dst, int weight) : dst(dst), weight(weight) {}
  int dst, weight;
};

struct State {
  State(int dst, int weight, int rest) : dst(dst), weight(weight), rest(rest) {}
  int dst, weight, rest;
  bool operator<(const State &s) const { return weight > s.weight; }
};

typedef vector<vector<Edge>> Graph;

// M : 制限時間
// L : 冷凍できる頂点の数
// A : 始点
// H : 終点
int V, M, L, E, A, H;
bool isL[128];
Graph g;

int dp[100][100];

int solve() {
  priority_queue<State> q;
  rep(i, V) rep(j, M + 1) dp[i][j] = inf;
  dp[A][M] = 0;
  q.emplace(A, 0, M);
  while (q.size()) {
    State s = q.top();
    q.pop();
    int src = s.dst, weight = s.weight, rest = s.rest;
    if (src == H)
      return weight;
    if (dp[src][rest] < weight)
      continue;
    rep(i, g[src].size()) {
      int dst = g[src][i].dst;
      int weight = g[src][i].weight;
      if (rest - weight >= 0 &&
          dp[dst][rest - weight] > dp[src][rest] + weight) {
        dp[dst][rest - weight] = dp[src][rest] + weight;
        q.push(State(dst, dp[src][rest] + weight, rest - weight));
      }
      if (isL[src]) {
        for (int j = 1; rest + j <= M; j++) {
          if (rest + j - weight >= 0 &&
              dp[dst][rest + j - weight] > dp[src][rest] + weight + j) {
            dp[dst][rest + j - weight] = dp[src][rest] + weight + j;
            q.push(State(dst, dp[src][rest] + weight + j, rest - weight + j));
          }
        }
      }
    }
  }
  return inf;
}

int main() {
  while (cin >> V >> M >> L >> E >> A >> H && V) {
    memset(isL, 0, sizeof(isL));
    g.assign(V, {});
    isL[A] = isL[H] = true;
    rep(i, L) {
      int v;
      cin >> v;
      isL[v] = true;
    }
    rep(i, E) {
      int u, v, t;
      cin >> u >> v >> t;
      g[u].push_back(Edge(v, t));
      g[v].push_back(Edge(u, t));
    }
    int ans = solve();
    if (ans >= inf)
      cout << "Help!";
    else
      cout << ans;
    cout << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()

int const inf = 1 << 29;
struct Edge {
  Edge(int dst, int weight) : dst(dst), weight(weight) {}
  int dst, weight;
};

struct State {
  State(int dst, int weight, int rest) : dst(dst), weight(weight), rest(rest) {}
  int dst, weight, rest;
  bool operator<(const State &s) const { return weight > s.weight; }
};

typedef vector<vector<Edge>> Graph;

// M : 制限時間
// L : 冷凍できる頂点の数
// A : 始点
// H : 終点
int V, M, L, E, A, H;
bool isL[128];
Graph g;

int dp[128][128];

int solve() {
  priority_queue<State> q;
  rep(i, V) rep(j, M + 1) dp[i][j] = inf;
  dp[A][M] = 0;
  q.emplace(A, 0, M);
  while (q.size()) {
    State s = q.top();
    q.pop();
    int src = s.dst, weight = s.weight, rest = s.rest;
    if (src == H)
      return weight;
    if (dp[src][rest] < weight)
      continue;
    rep(i, g[src].size()) {
      int dst = g[src][i].dst;
      int weight = g[src][i].weight;
      if (rest - weight >= 0 &&
          dp[dst][rest - weight] > dp[src][rest] + weight) {
        dp[dst][rest - weight] = dp[src][rest] + weight;
        q.push(State(dst, dp[src][rest] + weight, rest - weight));
      }
      if (isL[src]) {
        for (int j = 1; rest + j <= M; j++) {
          if (rest + j - weight >= 0 &&
              dp[dst][rest + j - weight] > dp[src][rest] + weight + j) {
            dp[dst][rest + j - weight] = dp[src][rest] + weight + j;
            q.push(State(dst, dp[src][rest] + weight + j, rest - weight + j));
          }
        }
      }
    }
  }
  return inf;
}

int main() {
  while (cin >> V >> M >> L >> E >> A >> H && V) {
    memset(isL, 0, sizeof(isL));
    g.assign(V, {});
    isL[A] = isL[H] = true;
    rep(i, L) {
      int v;
      cin >> v;
      isL[v] = true;
    }
    rep(i, E) {
      int u, v, t;
      cin >> u >> v >> t;
      g[u].push_back(Edge(v, t));
      g[v].push_back(Edge(u, t));
    }
    int ans = solve();
    if (ans >= inf)
      cout << "Help!";
    else
      cout << ans;
    cout << endl;
  }
}