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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define INF 100000000
#define dis first
#define v second.first
#define m second.second
using namespace std;
typedef pair<int, pair<int, int>> P;

int N, M, L, K, A, H;
vector<P> mp[101];
int ice[101];

void dijkstra(int start, int goal) {
  int visited[101][101] = {}, D[101][101];
  for (int i = 0; i <= M; i++)
    for (int j = 0; j <= N; j++)
      D[i][j] = -INF;
  P t;
  t.dis = 0, t.v = start, t.m = 0;
  priority_queue<P> Q;
  Q.push(t);
  while (!Q.empty()) {
    t = Q.top(), Q.pop();
    if (t.m > M || visited[t.m][t.v] == 1)
      continue;
    visited[t.m][t.v] = 1;

    if (ice[t.v] == 1)
      for (int i = t.m - 1; i >= 0; i--) {
        P nt = t;
        nt.m = i;
        nt.dis = t.dis - (t.m - i);
        if (nt.m <= M && D[nt.m][nt.v] < nt.dis)
          Q.push(nt), D[nt.m][nt.v] = nt.dis;
      }

    for (int i = 0; i < mp[t.v].size(); i++) {
      P nt;
      nt.dis = t.dis - mp[t.v][i].dis;
      nt.v = mp[t.v][i].v;
      nt.m = t.m + mp[t.v][i].dis;
      if (nt.m <= M && D[nt.m][nt.v] < nt.dis)
        Q.push(nt), D[nt.m][nt.v] = nt.dis;
    }
  }
  int ans = -INF;
  for (int i = 0; i <= M; i++)
    ans = max(ans, D[i][goal]);

  if (ans == -INF)
    cout << "Help!" << endl;
  else
    cout << -ans << endl;
}

int main() {
  while (1) {
    cin >> N >> M >> L >> K >> A >> H;
    if (N == 0 && M == 0 && L == 0 && K == 0 && A == 0 && H == 0)
      break;

    for (int i = 0; i <= N; i++)
      ice[i] = 0, mp[i].clear();
    for (int i = 0; i < L; i++) {
      int a;
      cin >> a;
      ice[a] = 1;
    }

    for (int i = 0; i < K; i++) {
      int x, y, cost;
      cin >> x >> y >> cost;
      P t;
      t.v = y, t.dis = cost, t.m = cost;
      mp[x].push_back(t);
      t.v = x, t.dis = cost, t.m = cost;
      mp[y].push_back(t);
    }
    dijkstra(A, H);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define INF 100000000
#define dis first
#define v second.first
#define m second.second
using namespace std;
typedef pair<int, pair<int, int>> P;

int N, M, L, K, A, H;
vector<P> mp[101];
int ice[101];
priority_queue<P> Q;
void dijkstra(int start, int goal) {
  int visited[101][101] = {};
  int D[101][101];
  for (int i = 0; i <= M; i++)
    for (int j = 0; j <= N; j++)
      D[i][j] = -INF;
  P t;
  t.dis = 0, t.v = start, t.m = 0;
  Q.push(t);
  while (!Q.empty()) {
    t = Q.top(), Q.pop();
    if (t.m > M || visited[t.m][t.v] == 1)
      continue;
    visited[t.m][t.v] = 1;

    if (ice[t.v] == 1)
      for (int i = t.m - 1; i >= 0; i--) {
        P nt = t;
        nt.m = i;
        nt.dis = t.dis - (t.m - i);
        if (D[nt.m][nt.v] < nt.dis)
          Q.push(nt), D[nt.m][nt.v] = nt.dis;
      }

    for (int i = 0; i < mp[t.v].size(); i++) {
      P nt;
      nt.dis = t.dis - mp[t.v][i].dis;
      nt.v = mp[t.v][i].v;
      nt.m = t.m + mp[t.v][i].dis;
      if (D[nt.m][nt.v] < nt.dis)
        Q.push(nt), D[nt.m][nt.v] = nt.dis;
    }
  }
  int ans = -INF;
  for (int i = 0; i <= M; i++)
    ans = max(ans, D[i][goal]);

  if (ans == -INF)
    cout << "Help!" << endl;
  else
    cout << -ans << endl;
}

int main() {
  while (1) {
    cin >> N >> M >> L >> K >> A >> H;
    if (N == 0 && M == 0 && L == 0 && K == 0 && A == 0 && H == 0)
      break;

    for (int i = 0; i <= N; i++)
      ice[i] = 0, mp[i].clear();
    for (int i = 0; i < L; i++) {
      int a;
      cin >> a;
      ice[a] = 1;
    }

    for (int i = 0; i < K; i++) {
      int x, y, cost;
      cin >> x >> y >> cost;
      P t;
      t.v = y, t.dis = cost, t.m = cost;
      mp[x].push_back(t);
      t.v = x, t.dis = cost, t.m = cost;
      mp[y].push_back(t);
    }
    dijkstra(A, H);
  }
  return 0;
}