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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct Edge {
  int to, cost;

  Edge(int t, int c) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
};

typedef vector<vector<Edge>> Graph;

int N, M, L, K, A, B;
int prv[101];
int ans = 123456789;
Graph g;
vector<bool> lb;
int shortest[101][101];
int dfs(int now, int ttl, int cost) {
  if (ans <= cost)
    return -1;
  if (lb[now]) {
    ttl = M;
    if (shortest[ttl][now] <= cost)
      return -1;
    shortest[ttl][now] = cost;
  }

  if (ttl < 0)
    return -1;
  if (now == B) {
    cost += max(0, cost - M);
    ans = min(ans, cost);
    return cost;
  }

  for (auto &e : g[now]) {
    int ttl_ = ttl - e.cost;
    int cost_ = cost + e.cost;
    if (ttl_ < 0 || shortest[ttl_][e.to] <= cost_)
      continue;
    prv[e.to] = now;
    shortest[ttl_][e.to] = cost_;
    dfs(e.to, ttl_, cost_);
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (cin >> N >> M >> L >> K >> A >> B, N) {
    VI dp(N, 0);
    lb.assign(N, false);
    fill((int *)shortest, (int *)shortest + 101 * 101, 123456789);
    REP(i, L) {
      int x;
      cin >> x;
      lb[x] = true;
    }
    ans = 123456789;
    g.assign(N, vector<Edge>());
    REP(i, K) {
      int a, b, t;
      cin >> a >> b >> t;
      g[a].PB(Edge(b, t));
      g[b].PB(Edge(a, t));
    }
    dfs(A, M, 0);
    if (ans >= 123456789)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct Edge {
  int to, cost;

  Edge(int t, int c) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
};

typedef vector<vector<Edge>> Graph;

int N, M, L, K, A, B;
bool visited[101];
int prv[101];
int ans = 123456789;
Graph g;
vector<bool> lb;
VI shortest;
int dfs(int now, int ttl, int cost) {
  if (lb[now]) {
    if (shortest[now] <= cost)
      return -1;
    shortest[now] = cost;
    ttl = M;
  }
  if (ttl < 0)
    return -1;
  // if(visited[now]) return -1;
  if (now == B) {
    cost += max(0, cost - M);
    ans = min(ans, cost);
    return cost;
  }
  visited[now] = true;

  for (auto &e : g[now]) {
    prv[e.to] = now;
    dfs(e.to, ttl - e.cost, cost + e.cost);
  }
  visited[now] = false;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (cin >> N >> M >> L >> K >> A >> B, N) {
    VI dp(N, 0);
    lb.assign(N, false);
    shortest.assign(N, 123456789);
    REP(i, L) {
      int x;
      cin >> x;
      lb[x] = true;
    }
    ans = 123456789;
    g.assign(N, vector<Edge>());
    REP(i, K) {
      int a, b, t;
      cin >> a >> b >> t;
      g[a].PB(Edge(b, t));
      g[b].PB(Edge(a, t));
    }
    dfs(A, M, 0);
    if (ans >= 123456789)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}