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

#include <algorithm>
#include <iostream>

using namespace std;

const int INF = (1 << 20);

int main(void) {
  int n, m, l, k, a, h;
  int graph[100][100];
  bool flg[100];

  while (cin >> n >> m >> l >> k >> a >> h, n | m | l | k | a | h) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        graph[i][j] = INF;
      }
      graph[i][i] = 0;
      flg[i] = false;
    }

    flg[a] = flg[h] = true;
    for (int i = 0, x; i < l; ++i) {
      cin >> x;
      flg[x] = true;
    }

    for (int i = 0, f, t, c; i < k; ++i) {
      cin >> f >> t >> c;
      graph[f][t] = graph[t][f] = c;
    }

    for (int z = 0; z < n; ++z)
      for (int x = 0; x < n; ++x)
        for (int y = 0; y < n; ++y)
          graph[x][y] = min(graph[x][y], graph[x][z] + graph[z][y]);

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) {
        if (!flg[i] || !flg[j]) {
          graph[i][j] = INF;
        } else if (graph[i][j] > m) {
          graph[i][j] = INF;
        }
      }

    for (int z = 0; z < n; ++z)
      for (int x = 0; x < n; ++x)
        for (int y = 0; y < n; ++y)
          graph[x][y] = min(graph[x][y], graph[x][z] + graph[z][y]);

    if (graph[a][h] == INF)
      cout << "Help!" << endl;
    else
      cout << graph[a][h] + max(0, graph[a][h] - m) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

#define INF (1 << 29)
#define rep2(i, b, n) for (int i = b; i < n; ++i)
#define rep(i, n) rep2(i, 0, n)
#define repeq2(i, b, n) for (int i = b; i <= n; ++i)
#define repeq(i, n) repeq2(i, 0, n)
#define MEMSET(x, v) memset(x, v, sizeof(x))
#define MEMZERO(x) memset(x, 0, sizeof(x))
#define mk(x, y) make_pair(x, y)
#define X first
#define Y second.first
#define Z second.second

using namespace std;

typedef pair<int, pair<int, int>> piii;

int cost[100][101];
int mCost[100][101];
bool End[100][101];

int main(void) {
  bool hit[100];
  int n, m, l, k, a, h;
  while (cin >> n >> m >> l >> k >> a >> h, n | m | l | k | a | h) {
    MEMSET(hit, false);
    MEMSET(End, false);
    rep(i, n) repeq(t, m) { mCost[i][t] = INF; }
    rep(i, n) fill(cost[i], cost[i] + n, INF);
    rep(i, l) {
      int no;
      cin >> no;
      hit[no] = true;
    }
    rep(i, k) {
      int x, y, t;
      cin >> x >> y >> t;
      cost[x][y] = cost[y][x] = t;
    }

    priority_queue<piii, vector<piii>, greater<piii>> q;
    mCost[a][m] = 0;
    End[a][m] = true;
    q.push(mk(0, mk(a, m)));

    while (true) {
      int ii = -1, jj = -1, nn = (INF);
      // rep(i, n) {
      //	repeq(j, m) {
      //		if(End[i][j] && nn > mCost[i][j]) {
      //			nn = mCost[i][j];
      //			ii = i;
      //			jj = j;
      //		}
      //	}
      // }
      if (q.empty())
        break;
      piii p = q.top();
      q.pop();
      ii = p.Y;
      jj = p.Z;
      if (ii == -1 || jj == -1)
        break;
      End[ii][jj] = false;
      rep(i, n) {
        int jjj = jj;
        if (!hit[ii]) {
          jjj = m;
        }
        for (int j = 0; j + jjj <= m; ++j) {
          int t = j + jj - cost[ii][i];
          if (mCost[ii][jj + j] > mCost[ii][jj] + j) {
            mCost[ii][jj + j] = mCost[ii][jj] + j;
            q.push(mk(mCost[ii][jj + j], mk(ii, jj + j)));
            End[ii][jj + j] = true;
          }
          if (0 <= t && mCost[i][t] > mCost[ii][jj + j] + cost[ii][i] + j) {
            mCost[i][t] = mCost[ii][jj + j] + cost[ii][i] + j;
            q.push(mk(mCost[i][t], mk(i, t)));
            End[i][t] = true;
          }
        }
      }
    }

    int ans = INF;
    repeq(i, m) { ans = min(ans, mCost[h][i]); }
    if (ans == INF)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}