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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define F first
#define S second
using namespace std;
int n, m, l, k, a, h;
bool p[111];
vector<pair<int, int>> edge[111];
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
               greater<pair<int, pair<int, int>>>>
    que;
int d[111][111];
int main(void) {
  while (cin >> n >> m >> l >> k >> a >> h && n) {
    for (int i = 0; i < 111; i++) {
      p[i] = false;
      edge[i].clear();
    }
    for (int i = 0; i < l; i++) {
      int b;
      cin >> b;
      p[b] = true;
    }
    for (int i = 0; i < k; i++) {
      int b, c, t;
      cin >> b >> c >> t;
      edge[b].push_back(make_pair(c, t));
      edge[c].push_back(make_pair(b, t));
    }

    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                   greater<pair<int, pair<int, int>>>>
        quet;
    que = quet;
    que.push(make_pair(0, make_pair(m, a)));
    for (int i = 0; i < 111; i++) {
      for (int j = 0; j < 111; j++) {
        d[i][j] = 1 << 30;
      }
    }
    /*
    for(int i = 0; i < 111; i++){
      d[i][a] = 0;
    }
    */
    d[m][a] = 0;
    while (!que.empty()) {
      int c = que.top().F;
      int t = que.top().S.F;
      int v = que.top().S.S;
      // printf("aaa %d %d %d\\n",c,t,v);
      que.pop();
      if (d[t][v] < c)
        continue;
      for (int i = 0; i < (int)edge[v].size(); i++) {
        int u = edge[v][i].F;
        int cc = edge[v][i].S;
        if (t < cc)
          continue;
        if (d[t - cc][u] <= c + cc)
          continue;
        d[t - cc][u] = c + cc;
        que.push(make_pair(c + cc, make_pair(t - cc, u)));
      }
      if (p[v]) {
        for (t++, c++; t <= m; t++, c++) {
          if (d[t][v] < c)
            continue;
          for (int i = 0; i < (int)edge[v].size(); i++) {
            int u = edge[v][i].F;
            int cc = edge[v][i].S;
            if (t < cc || d[t - cc][u] <= c + cc)
              continue;
            d[t - cc][u] = c + cc;
            que.push(make_pair(c + cc, make_pair(t - cc, u)));
          }
        }
      }
    }
    int best = 1 << 28;
    for (int i = 0; i < 111; i++) {
      best = min(best, d[i][h]);
    }
    if (best >= 1 << 28)
      cout << "Help!" << endl;
    else
      cout << best << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define F first
#define S second
using namespace std;
int n, m, l, k, a, h;
bool p[111];
vector<pair<int, int>> edge[111];
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
               greater<pair<int, pair<int, int>>>>
    que;
int d[111][111];
int main(void) {
  while (cin >> n >> m >> l >> k >> a >> h && n) {
    for (int i = 0; i < 111; i++) {
      p[i] = false;
      edge[i].clear();
    }
    for (int i = 0; i < l; i++) {
      int b;
      cin >> b;
      p[b] = true;
    }
    for (int i = 0; i < k; i++) {
      int b, c, t;
      cin >> b >> c >> t;
      edge[b].push_back(make_pair(c, t));
      edge[c].push_back(make_pair(b, t));
    }

    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                   greater<pair<int, pair<int, int>>>>
        quet;
    que = quet;
    que.push(make_pair(0, make_pair(m, a)));
    for (int i = 0; i < 111; i++) {
      for (int j = 0; j < 111; j++) {
        d[i][j] = 1 << 30;
      }
    }
    /*
    for(int i = 0; i < 111; i++){
      d[i][a] = 0;
    }
    */
    d[m][a] = 0;
    while (!que.empty()) {
      int c = que.top().F;
      int t = que.top().S.F;
      int v = que.top().S.S;
      // printf("aaa %d %d %d\\n",c,t,v);
      que.pop();
      if (d[t][v] < c)
        continue;
      for (int i = 0; i < (int)edge[v].size(); i++) {
        int u = edge[v][i].F;
        int cc = edge[v][i].S;
        if (t < cc)
          continue;
        if (d[t - cc][u] < c + cc)
          continue;
        d[t - cc][u] = c + cc;
        que.push(make_pair(c + cc, make_pair(t - cc, u)));
      }
      if (p[v]) {
        for (t++, c++; t <= m; t++, c++) {
          if (d[t][v] < c)
            continue;
          for (int i = 0; i < (int)edge[v].size(); i++) {
            int u = edge[v][i].F;
            int cc = edge[v][i].S;
            if (t < cc || d[t - cc][u] < c + cc)
              continue;
            d[t - cc][u] = c + cc;
            que.push(make_pair(c + cc, make_pair(t - cc, u)));
          }
        }
      }
    }
    int best = 1 << 28;
    for (int i = 0; i < 111; i++) {
      best = min(best, d[i][h]);
    }
    if (best >= 1 << 28)
      cout << "Help!" << endl;
    else
      cout << best << endl;
  }
}