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

#include <bits/stdc++.h>

#define DEBUG(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))
#define is_in(x, y, w, h)                                                      \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};
using namespace std;
typedef long long ll;
typedef vector<vector<int>> VVI;
const int INF = 1e9;
typedef pair<int, int> PII;

struct Edge {
  int to, cost;
  Edge(int _to, int _cost) : to(_to), cost(_cost) {}
};

struct State {
  int v, totalTime, remainTime;
  State(int _v, int _t, int _r) : v(_v), totalTime(_t), remainTime(_r) {}
  bool operator<(const State &b) const { return totalTime < b.totalTime; }
  bool operator>(const State &b) const { return totalTime > b.totalTime; }
};

const int MAX_V = 100;
vector<Edge> G[MAX_V];
bool canFreeze[MAX_V];

int main() {
  int N, M, L, K, A, H;
  while (cin >> N >> M >> L >> K >> A >> H, N) {
    fill(canFreeze, canFreeze + MAX_V, false);
    rep(i, MAX_V) G[i].clear();

    rep(i, L) {
      int v;
      cin >> v;
      canFreeze[v] = true;
    }

    rep(i, K) {
      int X, Y, T;
      cin >> X >> Y >> T;
      G[X].emplace_back(Y, T);
      G[Y].emplace_back(X, T);
    }

    // map<PII, int> d;
    int d[110][110];
    fill(d[0], d[0] + 110 * 110, INF);
    priority_queue<State, vector<State>, greater<State>> Q;

    // d[PII(A, M)] = 0;
    d[A][M] = 0;
    Q.emplace(A, 0, M);

    int ans = -1;
    while (Q.size()) {
      State s = Q.top();
      Q.pop();
      // cerr << "(s.v, s.totalTime, s.remainTime) = (" << s.v << ", " <<
      // s.totalTime << ", " << s.remainTime << ")" << endl;

      if (s.v == H) {
        ans = s.totalTime;
        break;
      }

      // if (d.count(PII(s.v, s.remainTime)) && d[PII(s.v, s.remainTime)] <
      // s.totalTime) continue;
      if (d[s.v][s.remainTime] < s.totalTime)
        continue;

      for (int j = 0; j < G[s.v].size(); j++) {
        Edge e = G[s.v][j];
        int ncost = e.cost + s.totalTime;

        for (int i = 0; i + s.remainTime <= M; i++) {
          if (i > 0 && !canFreeze[s.v])
            break;

          if (s.remainTime - e.cost + i < 0)
            continue;
          // if (!d.count(PII(e.to, s.remainTime - e.cost + i)) || d[PII(e.to,
          // s.remainTime - e.cost + i)] > ncost + i) {
          if (d[e.to][s.remainTime - e.cost + i] > ncost + i) {
            // d[PII(e.to, s.remainTime-e.cost + i)] = ncost + i;
            d[e.to][s.remainTime - e.cost + i] = ncost + i;
            Q.emplace(e.to, ncost + i, s.remainTime - e.cost + i);
          }
        }
      }
    }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define DEBUG(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))
#define is_in(x, y, w, h)                                                      \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};
using namespace std;
typedef long long ll;
typedef vector<vector<int>> VVI;
const int INF = 1e9;
typedef pair<int, int> PII;

struct Edge {
  int to, cost;
  Edge(int _to, int _cost) : to(_to), cost(_cost) {}
};

struct State {
  int v, totalTime, remainTime;
  State(int _v, int _t, int _r) : v(_v), totalTime(_t), remainTime(_r) {}
  bool operator<(const State &b) const { return totalTime < b.totalTime; }
  bool operator>(const State &b) const { return totalTime > b.totalTime; }
};

const int MAX_V = 100;
vector<Edge> G[MAX_V];
bool canFreeze[MAX_V];

int main() {
  int N, M, L, K, A, H;
  while (cin >> N >> M >> L >> K >> A >> H, N) {
    fill(canFreeze, canFreeze + MAX_V, false);
    rep(i, MAX_V) G[i].clear();

    rep(i, L) {
      int v;
      cin >> v;
      canFreeze[v] = true;
    }

    rep(i, K) {
      int X, Y, T;
      cin >> X >> Y >> T;
      G[X].emplace_back(Y, T);
      G[Y].emplace_back(X, T);
    }

    map<PII, int> d;
    priority_queue<State, vector<State>, greater<State>> Q;

    d[PII(A, M)] = 0;
    Q.emplace(A, 0, M);

    int ans = -1;
    while (Q.size()) {
      State s = Q.top();
      Q.pop();
      // cerr << "(s.v, s.totalTime, s.remainTime) = (" << s.v << ", " <<
      // s.totalTime << ", " << s.remainTime << ")" << endl;

      if (s.v == H) {
        ans = s.totalTime;
        break;
      }

      if (d.count(PII(s.v, s.remainTime)) &&
          d[PII(s.v, s.remainTime)] < s.totalTime)
        continue;

      for (int j = 0; j < G[s.v].size(); j++) {
        Edge e = G[s.v][j];
        int ncost = e.cost + s.totalTime;

        for (int i = 0; i + s.remainTime <= M; i++) {
          if (i > 0 && !canFreeze[s.v])
            break;

          if (s.remainTime - e.cost + i < 0)
            continue;
          if (!d.count(PII(e.to, s.remainTime - e.cost + i)) ||
              d[PII(e.to, s.remainTime - e.cost + i)] > ncost + i) {
            d[PII(e.to, s.remainTime - e.cost + i)] = ncost + i;
            Q.emplace(e.to, ncost + i, s.remainTime - e.cost + i);
          }
        }
      }
    }

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