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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define INF 999999999

#define MAX_V 101

int n, m, l, k, a, h;

bool cold[200];

struct edge {
  int to, cost;
};
vector<edge> G[MAX_V];
int d[MAX_V][101];

void dijkstra(int s, int V) {
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      que;
  rep(i, MAX_V) rep(j, 101) d[i][j] = INF;
  d[s][m] = 0;
  que.push(mp(0, pii(s, m)));

  while (que.size()) {
    pair<int, pii> p = que.top();
    que.pop();

    int v = p.second.first;
    int b = p.second.second;
    if (d[v][b] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (b - e.cost >= 0 && d[e.to][b - e.cost] > d[v][b] + e.cost) {
        d[e.to][b - e.cost] = d[v][b] + e.cost;
        que.push(mp(d[e.to][b - e.cost], pii(e.to, b - e.cost)));
      }
    }
    if (cold[v]) {
      for (int i = m; i >= b; i--) {
        if (d[v][i] > d[v][b] + i - b) {
          d[v][i] = d[v][b] + i - b;
          que.push(mp(d[v][i], pii(v, i)));
        }
      }
    }
  }
}

int main() {
  while (cin >> n >> m >> l >> k >> a >> h && (n || m || l || k || a || h)) {
    rep(i, MAX_V) G[i].clear();
    rep(i, 200) cold[i] = false;
    rep(i, l) {
      int a;
      cin >> a;
      cold[a] = true;
    }
    rep(i, k) {
      int x, y, t;
      cin >> x >> y >> t;
      G[x].pb(edge{y, t});
      G[y].pb(edge{x, t});
    }

    dijkstra(a, n);

    int mini = INF;
    rep(i, 101) mini = min(mini, d[h][i]);
    if (mini == INF)
      cout << "Help!" << endl;
    else
      cout << mini << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

#define MAX_V 101

int n, m, l, k, a, h;

bool used[MAX_V][101];
int d[MAX_V][101];
vector<pii> G[MAX_V];
bool cold[200];

void dijkstra(int s) {
  rep(i, MAX_V) rep(j, 101) used[i][j] = false;
  rep(i, MAX_V) rep(j, 101) d[i][j] = INF;
  d[s][m] = 0;

  while (1) {
    int v = -1;
    int b = -1;
    rep(u, MAX_V) {
      rep(i, 101) {
        if (!used[u][i] && (v == -1 || d[u][i] < d[v][b]))
          v = u, b = i;
      }
    }
    if (v == -1)
      break;
    used[v][b] = true;

    rep(i, G[v].size()) {
      int u = G[v][i].first;
      int cost = G[v][i].second;
      if (b - cost >= 0)
        d[u][b - cost] = min(d[u][b - cost], d[v][b] + cost);
    }
    if (cold[v]) {
      for (int i = m; i >= b; i--) {
        d[v][i] = min(d[v][i], d[v][b] + i - b);
      }
    }
  }
}

int main() {
  while (cin >> n >> m >> l >> k >> a >> h && (n || m || l || k || a || h)) {
    rep(i, MAX_V) G[i].clear();
    rep(i, 200) cold[i] = false;
    rep(i, l) {
      int a;
      cin >> a;
      cold[a] = true;
    }
    rep(i, k) {
      int x, y, t;
      cin >> x >> y >> t;
      G[x].pb(pii(y, t));
      G[y].pb(pii(x, t));
    }

    dijkstra(a);

    int mini = INF;
    rep(i, 101) mini = min(mini, d[h][i]);
    if (mini == INF)
      cout << "Help!" << endl;
    else
      cout << mini << endl;
  }
}