Problem p01146 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)

using namespace std;

const int INF = 1000000000;

int N, M, L, K, A, H;
int ans;
bool isfreeze[100];
int v[100][100];

int main() {

  while (1) {
    cin >> N >> M >> L >> K >> A >> H;
    if (N == 0)
      break;
    REP(i, 0, 100) isfreeze[i] = false;
    isfreeze[A] = true;
    isfreeze[H] = true;
    REP(i, 0, L) {
      int t;
      cin >> t;
      isfreeze[t] = true;
    }
    REP(i, 0, 100) REP(j, 0, 100) {
      if (i == j)
        v[i][j] = 0;
      else
        v[i][j] = INF;
    }
    REP(i, 0, K) {
      int x, y, t;
      cin >> x >> y >> t;
      v[x][y] = t;
      v[y][x] = t;
    }

    // WF
    REP(k, 0, N) {
      REP(i, 0, N) {
        REP(j, 0, N) { v[i][j] = min(v[i][j], v[i][k] + v[k][j]); }
      }
    }

    //??????????¨???????????????\\????????????????????¨????????????????????±????¶???????
    //??????????¨???????????????§??????M??\\??????????????????????????°????????±????¶???????
    REP(i, 0, N) {
      REP(j, 0, N) {
        if (!isfreeze[i] || !isfreeze[j] || v[i][j] > M)
          v[i][j] = INF;
      }
    }

    //??´??°???????????°????????§WF
    REP(k, 0, N) {
      REP(i, 0, N) {
        REP(j, 0, N) { v[i][j] = min(v[i][j], v[i][k] + v[k][j]); }
      }
    }

    if (v[A][H] == INF)
      cout << "Help!" << endl;
    else
      cout << v[A][H] + max(0, v[A][H] - M) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

//???????????????????????´????????????????????¨????????????
//-> ??????step,
//??????limit??§?????????????????\\???????????§????????????????????°??§?¨???????????????????

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)

using namespace std;

const int INF = 1000000000;

int N, M, L, K, A, H;
int ans;
bool isfreeze[100];
int visited[100][100];
vector<pair<int, int>> v[100];

void dfs(int town, int limit, int step) {
  if (limit < 0 || visited[town][limit] < step)
    return;
  visited[town][limit] = step;
  if (town == H) {
    ans = min(ans, step);
    return;
  }
  // move
  REP(i, 0, v[town].size()) {
    dfs(v[town][i].first, limit - v[town][i].second, step + v[town][i].second);
  }
  // freeze
  if (isfreeze[town]) {
    REP(i, limit, M) { dfs(town, limit + 1, step + 1); }
  }
  return;
}

int main() {

  while (1) {
    cin >> N >> M >> L >> K >> A >> H;
    if (N == 0)
      break;
    REP(i, 0, 100) isfreeze[i] = false;
    isfreeze[A] = true;
    isfreeze[H] = true; //??´????????????????????????????????????
    REP(i, 0, L) {
      int t;
      cin >> t;
      isfreeze[t] = true;
    }
    REP(i, 0, 100) v[i].clear();
    REP(i, 0, K) {
      int x, y, t;
      cin >> x >> y >> t;
      v[x].push_back(make_pair(y, t));
      v[y].push_back(make_pair(x, t));
    }

    ans = INF;
    REP(i, 0, 100) REP(j, 0, 100) visited[i][j] = INF;

    dfs(A, M, 0);

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

  return 0;
}