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

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;
const int INF = 1 << 29;
struct elem {
  int node, cost;
};

bool operator>(const elem &x, const elem &y) {
  return tie(x.cost, x.node) > tie(y.cost, y.node);
}

int main() {
  while (1) {
  END:
    int n, m, l, k, a, h;
    cin >> n >> m >> l >> k >> a >> h;
    if (!n)
      break;
    vector<int> L;
    loop(l, i) {
      int v;
      cin >> v;
      L.push_back(v);
    }
    L.push_back(h);
    vector<vector<int>> d(n, vector<int>(n, INF));
    loop(k, i) {
      int x, y, t;
      cin >> x >> y >> t;
      d[x][y] = t;
      d[y][x] = t;
    }
    loop(n, j) loop(n, i) loop(n, k) {
      d[i][k] = min(d[i][k], d[i][j] + d[j][k]);
    }

    priority_queue<elem, vector<elem>, greater<elem>> Q;
    Q.push({a, 0});
    vector<int> vis(n);
    while (Q.size()) {
      elem crr = Q.top();
      Q.pop();

      if (vis[crr.node])
        continue;
      vis[crr.node] = 1;
      if (crr.node == h) {
        int ans = crr.cost;
        if (ans > m)
          ans += ans - m;
        cout << ans << endl;
        goto END;
      }
      for (int nxt_node : L) {
        int cost = d[crr.node][nxt_node];
        if (cost <= m) {
          Q.push({nxt_node, crr.cost + cost});
        }
      }
    }
    cout << "Help!" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;
const int INF = 1 << 29;
typedef pair<int, int> P;
typedef struct {
  int node, cost, tlimit;
} elem;

bool operator>(const elem &x, const elem &y) {
  return tie(x.cost, x.tlimit, x.node) > tie(y.cost, y.tlimit, y.node);
}

int main() {
  while (1) {
  END:
    int n, m, l, k, a, h;
    cin >> n >> m >> l >> k >> a >> h;
    if (!n)
      break;
    vector<int> L(n);
    loop(l, i) {
      int v;
      cin >> v;
      L[v] = 1;
    }
    vector<vector<P>> G(n);
    loop(k, i) {
      int x, y, t;
      cin >> x >> y >> t;
      G[x].emplace_back(y, t);
      G[y].emplace_back(x, t);
    }

    priority_queue<elem, vector<elem>, greater<elem>> Q;
    vector<vector<int>> vis(n, vector<int>(m + 1));
    Q.push({a, 0, m});
    while (Q.size()) {
      elem crr = Q.top();
      Q.pop();
      if (vis[crr.node][crr.tlimit])
        continue;
      vis[crr.node][crr.tlimit] = 1;
      if (crr.node == h) {
        cout << crr.cost << endl;
        goto END;
      }
      for (P p : G[crr.node]) {
        int wait = L[crr.node] ? (m - crr.tlimit) : 0;
        for (int i = 0; i <= wait; i++) {
          int cost = crr.cost + p.second + i;
          int tlimit = crr.tlimit + i - p.second;
          if (tlimit < 0 || vis[p.first][tlimit])
            continue;
          Q.push({p.first, cost, tlimit});
        }
      }
    }
    cout << "Help!" << endl;
  }
  return 0;
}