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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int n, m, l, k, a, h;
  while (1) {
    cin >> n >> m >> l >> k >> a >> h;
    if (n == 0)
      break;
    int needtime[101][101];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        needtime[i][j] = mod;
      }
    }
    vector<int> hospital;
    for (int i = 0; i < l; i++) {
      int tmp;
      cin >> tmp;
      hospital.push_back(tmp);
    }
    hospital.push_back(h);
    for (int i = 0; i < k; i++) {
      int x, y, t;
      cin >> x >> y >> t;
      needtime[x][y] = t;
      needtime[y][x] = t;
    }
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          needtime[i][j] = min(needtime[i][j], needtime[i][k] + needtime[k][j]);
        }
      }
    }
    priority_queue<pair<int, pair<int, int>>> qu;
    vector<int> result(101, mod);
    qu.push(make_pair(0, make_pair(a, m)));
    while (!qu.empty()) {
      int nowtime = -(qu.top()).first;
      int nowplace = ((qu.top()).second).first;
      int resttime = ((qu.top()).second).second;
      qu.pop();
      if (result[nowplace] <= nowtime)
        continue;
      // cout << nowtime << " " << nowplasce << " " << resttime << endl;
      result[nowplace] = nowtime;
      for (int i = 0; i < l + 1; i++) {
        int nextplace = hospital[i];
        if (needtime[nowplace][nextplace] > m)
          continue;
        int nexttime = nowtime + needtime[nowplace][nextplace];
        if (resttime >= needtime[nowplace][nextplace]) {
          qu.push(make_pair(
              -nexttime,
              make_pair(nextplace, resttime - needtime[nowplace][nextplace])));
        } else {
          int freezetime = needtime[nowplace][nextplace] - resttime;
          qu.push(make_pair(-(nexttime + freezetime), make_pair(nextplace, 0)));
        }
      }
    }
    if (result[h] == mod)
      cout << "Help!" << endl;
    else
      cout << result[h] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n, m, l, k, a, h;
int road[101][101];
bool canfreeze[101];
int fromato[101];
int limitt[101];
bool freezed;

void solve(int nowplace, int limit, int nowt) {
  int taizait = 0, nowlim = limit;
  if (canfreeze[nowplace]) {
    taizait = m - limit;
    nowlim = m;
    freezed = true;
  }
  // cout << nowplace << " " << nowt << endl;
  for (int i = 0; i < n; i++) {
    if (road[nowplace][i] == 0 || nowlim < road[nowplace][i])
      continue;
    if (i == h) {
      if (freezed)
        fromato[h] = min(fromato[h], nowt + taizait + road[nowplace][i] -
                                         (nowlim - road[nowplace][i]));
      else
        fromato[h] = min(fromato[h], nowt + taizait + road[nowplace][i]);
    } else if (nowt + taizait + road[nowplace][i] < fromato[i] ||
               limitt[i] < nowlim - road[nowplace][i]) {
      fromato[i] = nowt + taizait + road[nowplace][i];
      limitt[i] = nowlim - road[nowplace][i];
      solve(i, limitt[i], fromato[i]);
    }
  }
  return;
}

int main() {
  while (1) {
    cin >> n >> m >> l >> k >> a >> h;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        road[i][j] = 0;
      }
      canfreeze[i] = false;
      fromato[i] = 1000000000;
      limitt[i] = 0;
    }
    freezed = false;
    int p;
    for (int i = 0; i < l; i++) {
      cin >> p;
      canfreeze[p] = true;
    }
    int x, y, t;
    for (int i = 0; i < k; i++) {
      cin >> x >> y >> t;
      road[x][y] = t;
      road[y][x] = t;
    }
    fromato[a] = 0;
    limitt[a] = m;
    solve(a, m, 0);
    // for(int i = 0; i < n; i++){
    // 	cout << fromato[i] << endl;
    // }
    if (fromato[h] != 1000000000)
      cout << fromato[h] << endl;
    else
      cout << "Help!" << endl;
  }
  return 0;
}