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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};
int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main() {
  int n, m, l, k, a, h;
  while (scanf("%d%d%d%d%d%d", &n, &m, &l, &k, &a, &h), n + m + l + k + a + h) {
    vector<bool> blood(n, false);
    vector<vector<bool>> flag(n, vector<bool>(n, false));
    vector<vector<int>> load(n, vector<int>(n, 0));

    REP(i, l) {
      int tmp;
      scanf("%d", &tmp);
      blood[tmp] = true;
    }
    REP(i, k) {
      int x, y, t;
      scanf("%d%d%d", &x, &y, &t);
      load[x][y] = load[y][x] = t;
    }

    typedef pair<int, pair<int, pair<int, int>>> data;
    priority_queue<data, vector<data>, greater<data>> pq;
    pq.push(mp(0, mp(a, mp(m, a))));
    while (pq.size()) {
      data d = pq.top();
      int time = d.f;
      int now = d.s.f;
      int b = d.s.s.f;
      int last = d.s.s.s;

      if (now == h) {
        int tt = time;
        if (time > m)
          tt += time - m;
        printf("%d\\n", tt);
        break;
      }

      if (!flag[last][now]) {
        flag[last][now] = true;
        if (blood[now]) {
          b = m;
          last = now;
        }

        REP(i, n) {
          if (load[now][i] != 0) {
            int tt = time + load[now][i];
            int bb = b - load[now][i];
            if (bb < 0)
              continue;
            pq.push(mp(tt, mp(i, mp(bb, last))));
          }
        }
      }

      pq.pop();
      if (pq.empty()) {
        puts("Help!");
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};
int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main() {
  int n, m, l, k, a, h;
  while (scanf("%d%d%d%d%d%d", &n, &m, &l, &k, &a, &h), n + m + l + k + a + h) {
    vector<bool> blood(n, false);
    vector<bool> flag(n, false);
    vector<vector<int>> load(n, vector<int>(n, 0));

    REP(i, l) {
      int tmp;
      scanf("%d", &tmp);
      blood[tmp] = true;
    }
    REP(i, k) {
      int x, y, t;
      scanf("%d%d%d", &x, &y, &t);
      load[x][y] = load[y][x] = t;
    }

    typedef pair<int, pair<int, int>> data;
    priority_queue<data, vector<data>, greater<data>> pq;
    pq.push(mp(0, mp(a, m)));
    while (pq.size()) {
      data d = pq.top();
      int time = d.f;
      int now = d.s.f;
      int b = d.s.s;

      if (now == h) {
        int tt = time;
        if (time > m)
          tt += time - m;
        printf("%d\\n", tt);
        break;
      }

      if (!flag[now]) {
        if (blood[now])
          flag[now] = true;
        if (blood[now])
          b = m;

        REP(i, n) {
          if (load[now][i] != 0) {
            int tt = time + load[now][i];
            int bb = b - load[now][i];
            if (bb < 0)
              continue;
            pq.push(mp(tt, mp(i, bb)));
          }
        }
      }

      pq.pop();
      if (pq.empty()) {
        puts("Help!");
        break;
      }
    }
  }
  return 0;
}