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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;

#define MAX_N 200
#define INF 101010101

int N, M, L, K, A, H, a1, a2, a3;
vector<pair<int, int>> X[MAX_N];
int dp[MAX_N][MAX_N];
int p[MAX_N];

priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
               greater<tuple<int, int, int>>>
    Q;

int main() {
  while (true) {
    for (int i = 0; i < MAX_N; i++) {
      for (int j = 0; j < MAX_N; j++) {
        dp[i][j] = INF;
      }
    }
    for (int i = 0; i < MAX_N; i++) {
      X[i].clear();
      p[i] = 0;
    }
    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 < L; i++) {
      cin >> a1;
      p[a1] = 1;
    }
    for (int i = 0; i < K; i++) {
      cin >> a1 >> a2 >> a3;
      X[a1].push_back(make_pair(a2, a3));
      X[a2].push_back(make_pair(a1, a3));
    }
    Q.push(make_tuple(0, A, M));
    dp[A][M] = 0;
    while (!Q.empty()) {
      tuple<int, int, int> tup = Q.top();
      Q.pop();
      int b1 = get<0>(tup);
      int b2 = get<1>(tup);
      int b3 = get<2>(tup);
      if (b3 < M && p[b2] == 1) {
        if (dp[b2][b3 + 1] > dp[b2][b3] + 1) {
          dp[b2][b3 + 1] = dp[b2][b3] + 1;
          Q.push(make_tuple(dp[b2][b3 + 1], b2, b3 + 1));
        }
      }
      for (int i = 0; i < X[b2].size(); i++) {
        int c1 = X[b2][i].first;
        int c2 = X[b2][i].second;
        if (b3 >= c2) {
          if (dp[c1][b3 - c2] > dp[b2][b3] + c2) {
            dp[c1][b3 - c2] = dp[b2][b3] + c2;
            Q.push(make_tuple(dp[c1][b3 - c2], c1, b3 - c2));
          }
        }
      }
    }
    int minx = INF;
    for (int i = 0; i <= M; i++) {
      minx = min(minx, dp[H][i]);
    }
    if (minx == INF) {
      cout << "Help!" << endl;
    } else {
      cout << minx << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[200][201], n, m, s, t, u, v, a, b, c, loop, p[200][200], y[200], minx;
int main() {
  while (true) {
    minx = 114514;
    cin >> n >> s >> t >> m >> u >> v;
    if (!n) {
      break;
    }
    memset(p, 10, sizeof(p));
    memset(y, 0, sizeof(y));
    memset(x, 10, sizeof(x));
    for (int i = 0; i < t; i++) {
      cin >> a;
      y[a] = 1;
    }
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      p[a][b] = c;
      p[b][a] = c;
    }
    x[u][s] = 0;
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          if (p[j][k] < (1 << 15)) {
            for (int l = m; l >= p[j][k]; l--) {
              x[k][l - p[j][k]] = min(x[k][l - p[j][k]], x[j][l] + p[j][k]);
            }
            if (y[k] == 1) {
              for (int l = 0; l < m; l++) {
                for (int o = m; o <= m; o++) {
                  x[k][o] = min(x[k][o], x[k][l] + (o - l));
                }
              }
            }
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      minx = min(minx, x[v][i]);
    }
    if (minx == 114514) {
      minx = -1;
    }
    if (minx != -1) {
      cout << minx << endl;
    } else {
      cout << "Help" << endl;
    }
  }
}