Problem p01146 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>

#define INF (1 << 29)

using namespace std;

int V, E, L, M, A, H, P, Q, R, C;

int main() {
  while (true) {
    scanf("%d", &V);
    scanf("%d", &M);
    scanf("%d", &L);
    scanf("%d", &E);
    scanf("%d", &A);
    scanf("%d", &H);

    if (V == 0) {
      break;
    }

    vector<vector<pair<int, int>>> G(V);

    vector<bool> ok(V, false);

    for (int i = 0; i < L; i++) {
      scanf("%d", &C);

      ok[C] = true;
    }

    for (int i = 0; i < E; i++) {
      scanf("%d", &P);
      scanf("%d", &Q);
      scanf("%d", &R);

      G[P].push_back(make_pair(Q, R));
      G[Q].push_back(make_pair(P, R));
    }

    vector<vector<int>> dp(V, vector<int>(M + 1, INF));

    dp[A][M] = 0;

    for (int rep = 0; rep < V - 1; rep++) {
      for (int i = 0; i < V; i++) {
        if (i != A) {
          for (int j = 0; j <= M; j++) {
            for (int k = 0; k < G[i].size(); k++) {
              if (G[i][k].second + j <= M) {
                dp[i][j] = min(dp[i][j], dp[G[i][k].first][j + G[i][k].second] +
                                             G[i][k].second);
              }
            }
          }
        }

        if (ok[i]) {
          for (int j = 0; j < M; j++) {
            for (int k = j + 1; k <= M; k++) {
              dp[i][k] = min(dp[i][k], dp[i][j] + k - j);
            }
          }
        }
      }
    }

    int ret = INF;

    for (int i = 0; i <= M; i++) {
      ret = min(ret, dp[H][i]);
    }

    if (ret != INF) {
      printf("%d\n", ret);
    } else {
      printf("Help!\n");
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>

#define INF (1 << 29)

using namespace std;

int V, E, L, M, A, H, P, Q, R, C;

int main() {
  while (true) {
    scanf("%d", &V);
    scanf("%d", &M);
    scanf("%d", &L);
    scanf("%d", &E);
    scanf("%d", &A);
    scanf("%d", &H);

    if (V == 0) {
      break;
    }

    vector<vector<pair<int, int>>> G(V);

    vector<bool> ok(V, false);

    for (int i = 0; i < L; i++) {
      scanf("%d", &C);

      ok[C] = true;
    }

    for (int i = 0; i < E; i++) {
      scanf("%d", &P);
      scanf("%d", &Q);
      scanf("%d", &R);

      G[P].push_back(make_pair(Q, R));
      G[Q].push_back(make_pair(P, R));
    }

    vector<vector<int>> dp(V, vector<int>(M + 1, INF));

    dp[A][M] = 0;

    for (int rep = 0; rep < V - 1; rep++) {
      for (int i = 0; i < V; i++) {
        if (i != A) {
          for (int j = 0; j <= M; j++) {
            for (int k = 0; k < G[i].size(); k++) {
              if (G[i][k].second + j <= M) {
                dp[i][j] = min(dp[i][j], dp[G[i][k].first][j + G[i][k].second] +
                                             G[i][k].second);
              }
            }
          }
        }

        if (ok[i]) {
          int t = INF;

          for (int j = 0; j <= M; j++) {
            t = min(dp[i][j], ++t);

            dp[i][j] = t;
          }
        }
      }
    }

    int ret = INF;

    for (int i = 0; i <= M; i++) {
      ret = min(ret, dp[H][i]);
    }

    if (ret != INF) {
      printf("%d\n", ret);
    } else {
      printf("Help!\n");
    }
  }

  return 0;
}