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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> pii;

struct node {
  int p, t, m;
  node(int pp, int tt, int mm) {
    p = pp;
    t = tt;
    m = mm;
  }
};

bool operator<(const node &lhs, const node &rhs) { return lhs.t > rhs.t; }

vector<pii> cost[100];
bool visit[100][101];

int main() {
  int n, m, l, k, a, h;
  while (cin >> n >> m >> l >> k >> a >> h, n) {
    memset(visit, 0, sizeof(visit));
    bool bld[100] = {};
    rep(i, l) {
      int x;
      cin >> x;
      bld[x] = true;
    }
    bld[h] = bld[a] = true;
    rep(i, n) cost[i].clear();
    rep(i, k) {
      int x, y, t;
      cin >> x >> y >> t;
      if (t <= m) {
        cost[x].push_back(pii(y, t));
        cost[y].push_back(pii(x, t));
      }
    }
    priority_queue<node> que;
    que.push(node(a, 0, m));
    bool found = false;
    while (!que.empty()) {
      node nd = que.top();
      que.pop();
      if (nd.p == h) {
        found = true;
        cout << nd.t << endl;
        break;
      }
      if (visit[nd.p][nd.m])
        continue;
      visit[nd.p][nd.m] = true;
      if (bld[nd.p]) {
        for (int nm = nd.m + 1; nm <= m; nm++) {
          if (!visit[nd.p][nm]) {
            que.push(node(nd.p, nd.t + nm - nd.m, nm));
          }
        }
      }
      rep(i, cost[nd.p].size()) {
        int nm = nd.m - cost[nd.p][i].second;
        if (nm >= 0 && !visit[cost[nd.p][i].first][nm]) {
          que.push(node(cost[nd.p][i].first, nd.t + cost[nd.p][i].second, nm));
        }
      }
    }
    if (!found) {
      cout << "Help!" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;

struct node {
  int p, t, m;
  node(int pp, int tt, int mm) {
    p = pp;
    t = tt;
    m = mm;
  }
};

bool operator<(const node &lhs, const node &rhs) { return lhs.t > rhs.t; }

int cost[100][100];
const int INF = 1000000;
bool visit[100][100];

int main() {
  int n, m, l, k, a, h;
  while (cin >> n >> m >> l >> k >> a >> h, n) {
    rep(i, n) rep(j, n) cost[i][j] = INF;
    memset(visit, 0, sizeof(visit));
    bool bld[100] = {};
    rep(i, l) {
      int x;
      cin >> x;
      bld[x] = true;
    }
    rep(i, k) {
      int x, y, t;
      cin >> x >> y >> t;
      cost[x][y] = cost[y][x] = t;
    }
    priority_queue<node> que;
    que.push(node(h, 0, m));
    bool found = false;
    while (!que.empty()) {
      node nd = que.top();
      que.pop();
      cout << nd.p << " " << nd.t << " " << nd.m << endl;
      if (nd.p == a) {
        found = true;
        cout << nd.t << endl;
        break;
      }
      if (visit[nd.p][nd.m])
        continue;
      visit[nd.p][nd.m] = true;
      if (bld[nd.p]) {
        for (int nm = nd.m + 1; nm <= m; nm++) {
          if (!visit[nd.p][nm]) {
            que.push(node(nd.p, nd.t + nm - nd.m, nm));
          }
        }
      }
      rep(i, n) {
        if (nd.m - cost[i][nd.p] >= 0 && !visit[i][nd.m - cost[i][nd.p]]) {
          que.push(node(i, nd.t + cost[i][nd.p], nd.m - cost[i][nd.p]));
        }
      }
    }
    if (!found) {
      cout << "Help!" << endl;
    }
  }
  return 0;
}