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

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
#define INF 0x7FFFFFFF

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> PP;

int main(void) {
  int n, m, l, k, a, h, inp, r[100], t[100][101], d[100][100], ans, i, j, x, y,
      z;
  priority_queue<PP, vector<PP>, greater<PP>> que; // 時間 残り時間 町の番号
  while (1) {
    scanf("%d%d%d%d%d%d", &n, &m, &l, &k, &a, &h);
    if (n + m + l + k + a + h == 0)
      break;
    memset(r, 0, sizeof(r));
    for (i = 0; i < l; i++) {
      scanf("%d", &inp);
      r[inp] = 1;
    }
    memset(d, -1, sizeof(d));
    for (i = 0; i < k; i++) {
      scanf("%d%d%d", &x, &y, &z);
      d[x][y] = z;
      d[y][x] = z;
    }
    memset(t, -1, sizeof(t));
    t[a][m] = 0;
    que.push(PP(0, P(m, a)));
    while (que.size()) {
      x = que.top().first, y = que.top().second.first,
      z = que.top().second.second;
      //			printf("t[%d][%d] = %d\\n",z,y,t[z][y]);
      que.pop();
      if (r[z]) {
        for (i = y + 1; i <= m; i++) {
          if (t[z][i] == -1 || t[z][i] > x + i - y) {
            t[z][i] = x + i - y;
            que.push(PP(x + i - y, P(i, z)));
          }
        }
      }
      for (i = 0; i < n; i++) {
        if (d[z][i] != -1 && d[z][i] <= y &&
            (t[i][y - d[z][i]] == -1 || t[i][y - d[z][i]] > x + d[z][i])) {
          t[i][y - d[z][i]] = x + d[z][i];
          que.push(PP(x + d[z][i], P(y - d[z][i], i)));
        }
      }
    }
    ans = -1;
    for (i = 0; i <= m; i++) {
      if (t[h][i] != -1 && (ans == -1 || t[h][i] < ans))
        ans = t[h][i];
    }
    if (ans != -1)
      printf("%d\\n", ans);
    else
      printf("Help!\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string.h>
#define INF 0x1FFFFFFF

using namespace std;

int main(void) {
  int n, m, l, k, a, h, inp, rest[100], u[100][101], c[100][100], s[100][101],
      v, r, ans, i, j, x, y, t;

  while (1) {
    scanf("%d%d%d%d%d%d", &n, &m, &l, &k, &a, &h);
    if (n == 0 && m == 0 && l == 0 && k == 0 && a == 0 && h == 0)
      break;
    memset(rest, 0, sizeof(rest));
    for (i = 0; i < l; i++) {
      scanf("%d", &inp);
      rest[inp] = 1;
    }
    for (i = 0; i < n; i++)
      for (j = 0; j <= m; j++)
        s[i][j] = INF;

    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
        c[i][j] = INF;

    for (i = 0; i < k; i++) { // コストの入力
      scanf("%d%d%d", &x, &y, &t);
      c[x][y] = t;
      c[y][x] = t;
    }
    s[a][m] = 0;
    memset(u, 0, sizeof(u));
    while (1) {
      v = -1;
      for (i = 0; i < n; i++) {
        for (j = 0; j <= m; j++) {
          if (!u[i][j] && s[i][j] != INF && (v == -1 || s[v][r] > s[i][j])) {
            v = i;
            r = j;
          }
        }
      }
      u[v][r] = 1;
      if (v == -1)
        break;
      for (i = 0; i < n; i++) {
        if (r - c[v][i] >= 0 &&
            s[i][rest[i] ? m : (r - c[v][i])] >
                s[v][r] + c[v][i] + (rest[i] ? m - r + c[v][i] : 0)) {
          s[i][rest[i] ? m : (r - c[v][i])] =
              s[v][r] + c[v][i] + (rest[i] ? m - r + c[v][i] : 0);
          u[i][rest[i] ? m : (r - c[v][i])] = 0;
        }
      }
    }
    ans = INF;
    for (i = 0; i <= m; i++)
      ans = min(ans, s[h][i] - i);
    if (ans < INF - m)
      printf("%d\\n", ans);
    else
      printf("Help!\\n");
  }
  return 0;
}