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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_V 111

int n, m, l, k, a, h;
bool rf[111];
int d[111][111];

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n >> m >> l >> k >> a >> h;
    if (n == 0)
      break;
    memset(rf, 0, sizeof(rf));
    rep(i, l) {
      int r;
      cin >> r;
      rf[r] = true;
    }
    vector<P> G[111];
    rep(i, k) {
      int v, u, t;
      cin >> v >> u >> t;
      G[v].pb(P(u, t));
      G[u].pb(P(v, t));
    }
    rep(i, 111) rep(j, 111) d[i][j] = INF;
    priority_queue<PPI, vector<PPI>, greater<PPI>> que;
    que.push(PPI(P(0, m), a));
    d[a][m] = 0;
    while (!que.empty()) {
      int v = que.top().se, dist = que.top().fi.fi, rest = que.top().fi.se;
      que.pop();
      if (d[v][rest] < dist)
        continue;
      if (!rf[v]) {
        rep(i, G[v].size()) {
          int nv = G[v][i].fi, ncost = G[v][i].se;
          if (rest - ncost >= 0 && d[nv][rest - ncost] > d[v][rest] + ncost) {
            d[nv][rest - ncost] = d[v][rest] + ncost;
            que.push(PPI(P(d[v][rest] + ncost, rest - ncost), nv));
          }
        }
      } else {
        rep(i, G[v].size()) {
          int nv = G[v][i].fi, ncost = G[v][i].se;
          repl(nrest, rest, m + 1) {
            if (nrest - ncost >= 0 &&
                d[nv][nrest - ncost] > d[v][rest] + ncost + (nrest - rest)) {
              d[nv][nrest - ncost] = d[v][rest] + ncost + (nrest - rest);
              que.push(PPI(
                  P(d[v][rest] + ncost + (nrest - rest), nrest - ncost), nv));
            }
          }
        }
      }
    }
    int res = INF;
    rep(i, 111) minch(res, d[h][i]);
    if (res == INF)
      cout << "Help!" << endl;
    else
      cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_V 111

int n, m, l, k, a, h;
bool rf[111];
int d[111][111];

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n >> m >> l >> k >> a >> h;
    if (n == 0)
      break;
    memset(rf, 0, sizeof(rf));
    rep(i, l) {
      int r;
      cin >> r;
      rf[r] = true;
    }
    vector<P> G[111];
    rep(i, k) {
      int v, u, t;
      cin >> v >> u >> t;
      G[v].pb(P(u, t));
      G[u].pb(P(v, t));
    }
    rep(i, 111) rep(j, 111) d[i][j] = INF;
    priority_queue<PPI, vector<PPI>, greater<PPI>> que;
    que.push(PPI(P(0, m), a));
    while (!que.empty()) {
      int v = que.top().se, dist = que.top().fi.fi, rest = que.top().fi.se;
      que.pop();
      if (d[v][rest] < dist)
        continue;
      d[v][rest] = dist;
      if (!rf[v]) {
        rep(i, G[v].size()) {
          int nv = G[v][i].fi, ncost = G[v][i].se;
          if (rest - ncost >= 0)
            que.push(PPI(P(dist + ncost, rest - ncost), nv));
        }
      } else {
        rep(i, G[v].size()) {
          int nv = G[v][i].fi, ncost = G[v][i].se;
          repl(nrest, rest, m + 1) {
            if (nrest - ncost >= 0)
              que.push(
                  PPI(P(dist + ncost + (nrest - rest), nrest - ncost), nv));
          }
        }
      }
    }
    int res = INF;
    rep(i, 111) minch(res, d[h][i]);
    if (res == INF)
      cout << "Help!" << endl;
    else
      cout << res << endl;
  }
  return 0;
}