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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct node {
  int index, life, cost;
  node(int index, int life, int cost) : index(index), life(life), cost(cost) {}
  bool operator<(const node &other) const { return cost > other.cost; }
};
const int MAX = INT_MAX / 10;
const int NONE = -1;

int main() {
  int n, m, l, k, a, h;
  while (cin >> n >> m >> l >> k >> a >> h, n) {
    vvi dist(n, vi(n, NONE));
    vi reito(n);
    REP(i, l) {
      int r;
      cin >> r;
      reito[r] = 1;
    }
    REP(i, n) { dist[i][i] = 0; }
    REP(i, k) {
      int src, dst, d;
      cin >> src >> dst >> d;
      dist[src][dst] = d;
      dist[dst][src] = d;
    }

    priority_queue<node> q;
    q.push(node(a, m, 0));
    vvi cost(n, vi(m + 1, MAX));
    cost[a][m] = 0;
    while (!q.empty()) {
      node cnode = q.top();
      q.pop();
      if (cost[cnode.index][cnode.life] < cnode.cost) {
        continue;
      }
      REP(charge, (reito[cnode.index] ? m - cnode.life + 1 : 1)) {
        FOR(i, (!charge ? 0 : cnode.index), (!charge ? n : cnode.index + 1)) {
          if ((charge > 0 && cnode.index == i) ||
              (charge == 0 && i != cnode.index &&
               dist[cnode.index][i] != NONE)) {
            int newlife = cnode.life + charge - dist[cnode.index][i];
            if (newlife >= 0) {
              int newcost = cnode.cost + dist[cnode.index][i] + charge;
              if (cost[i][newlife] > newcost) {
                cost[i][newlife] = newcost;
                q.push(node(i, newlife, newcost));
              }
            }
          }
        }
      }
    }
    int ans = MAX;
    FOR(i, 0, m + 1) { ans = min(ans, cost[h][i]); }
    if (ans == MAX) {
      cout << "Help!" << endl;
    } else {
      cout << ans << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct node {
  int index, life, cost;
  node(int index, int life, int cost) : index(index), life(life), cost(cost) {}
  bool operator<(const node &other) const { return cost > other.cost; }
};
const int MAX = INT_MAX / 10;
const int NONE = -1;

int main() {
  int n, m, l, k, a, h;
  while (scanf("%d %d %d %d %d %d", &n, &m, &l, &k, &a, &h), n) {
    vvi dist(n, vi(n, NONE));
    vi reito(n);
    REP(i, l) {
      int r;
      scanf("%d", &r);
      reito[r] = 1;
    }
    REP(i, n) { dist[i][i] = 0; }
    REP(i, k) {
      int src, dst, d;
      scanf("%d %d %d", &src, &dst, &d);
      dist[src][dst] = d;
      dist[dst][src] = d;
    }

    priority_queue<node> q;
    q.push(node(a, m, 0));
    vvi cost(n, vi(m + 1, MAX));
    cost[a][m] = 0;
    while (!q.empty()) {
      node cnode = q.top();
      q.pop();
      if (cost[cnode.index][cnode.life] < cnode.cost) {
        continue;
      }
      REP(charge, (reito[cnode.index] ? m - cnode.life + 1 : 1)) {
        REP(i, n) {
          if ((charge > 0 && cnode.index == i) ||
              (charge == 0 && i != cnode.index &&
               dist[cnode.index][i] != NONE)) {
            int newlife = cnode.life + charge - dist[cnode.index][i];
            if (newlife >= 0) {
              int newcost = cnode.cost + dist[cnode.index][i] + charge;
              if (cost[i][newlife] > newcost) {
                cost[i][newlife] = newcost;
                q.push(node(i, newlife, newcost));
              }
            }
          }
        }
      }
    }
    int ans = MAX;
    FOR(i, 0, m + 1) { ans = min(ans, cost[h][i]); }
    if (ans == MAX) {
      printf("Help!\\n");
    } else {
      printf("%d\\n", ans);
    }
  }
}