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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int MAX_N = 110;
int N, M, L, K, A, H;
struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};
vector<int> facility(MAX_N);
vector<edge> G[MAX_N];
int bfs() {
  vector<vector<int>> used(MAX_N, vector<int>(MAX_N, INF));
  queue<P> que;
  que.push(P(A, M));
  used[A][M] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop();
    int v = p.first;
    int t = p.second;
    for (int u = 0; u < G[v].size(); u++) {
      edge e = G[v][u];
      if (facility[v]) {
        for (int nt = t; nt <= M; nt++) {
          if (nt - e.cost < 0)
            continue;
          if (used[v][t] + e.cost + nt - t < used[e.to][nt - e.cost]) {
            used[e.to][nt - e.cost] = used[v][t] + e.cost + nt - t;
            que.push(P(e.to, nt - e.cost));
          }
        }
      } else {
        if (t - e.cost < 0)
          continue;
        if (used[v][t] + e.cost < used[e.to][t - e.cost]) {
          used[e.to][t - e.cost] = used[v][t] + e.cost;
          que.push(P(e.to, t - e.cost));
        }
      }
    }
  }
  int res = INF;
  for (int i = 0; i <= M; i++)
    res = min(res, used[H][i]);
  return res;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> N >> M >> L >> K >> A >> H, N or M or L or K) {
    facility.assign(N, 0);
    for (int i = 0; i < MAX_N; i++)
      G[i].clear();
    for (int i = 0; i < L; i++) {
      int x;
      cin >> x;
      facility[x] = true;
    }
    facility[A] = facility[H] = 1;
    for (int i = 0; i < K; i++) {
      int x, y, t;
      cin >> x >> y >> t;
      G[x].push_back(edge(y, t));
      G[y].push_back(edge(x, t));
    }
    int ans = bfs();
    if (ans == INF)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PP = pair<int, P>;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int MAX_N = 110;
int N, M, L, K, A, H;
struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};
vector<int> facility;
vector<edge> G[MAX_N];
vector<vector<int>> used(MAX_N, vector<int>(MAX_N));
int bfs() {
  priority_queue<PP> que;
  que.push(PP(0, P(A, M)));
  while (que.size()) {
    PP p = que.top();
    que.pop();
    int c = p.first;
    int v = p.second.first;
    int t = p.second.second;
    if (used[v][t] < c)
      continue;
    used[v][t] = c;
    for (int u = 0; u < G[v].size(); u++) {
      edge e = G[v][u];
      if (t - e.cost < 0)
        continue;
      int tmp = t - e.cost;
      if (facility[e.to]) {
        for (int nt = tmp; nt <= M; nt++) {
          que.push(PP(c + e.cost + nt - tmp, P(e.to, nt)));
        }
      } else
        que.push(PP(c + e.cost, P(e.to, tmp)));
    }
  }
  int res = INF;
  for (int i = 0; i <= M; i++) {
    res = min(res, used[H][i]);
  }
  return res;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> N >> M >> L >> K >> A >> H, N or M or L or K) {
    for (int i = 0; i < MAX_N; i++)
      G[i].clear();
    for (int i = 0; i < MAX_N; i++)
      for (int j = 0; j < MAX_N; j++)
        used[i][j] = INF;
    facility.resize(N, 0);
    for (int i = 0; i < L; i++) {
      int x;
      cin >> x;
      facility[x] = true;
    }
    // facility[A] = facility[H] = 1;
    for (int i = 0; i < K; i++) {
      int x, y, t;
      cin >> x >> y >> t;
      G[x].push_back(edge(y, t));
      G[y].push_back(edge(x, t));
    }
    int ans = bfs();
    if (ans == INF)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}