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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define TP make_pair
#define rep(i, x) for (int i = 0; i < (x); ++i)
#define rep1(i, x) for (int i = 1; i <= (x); ++i)
#define rrep(i, x) for (int i = (x)-1; i >= 0; --i)
#define rrep1(i, x) for (int i = (x); i >= 1; --i)
#define int long long
#define FOR(i, a, x) for (int i = (a); i < (x); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  rep(i, vec.size()) is >> vec[i];
  return is;
}
template <typename T> vector<T> &sort(vector<T> &a) {
  sort(all(a));
  return a;
}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int d[101][101];
bool used[101];

int N, T, F, M, S, G;
int freezer[101];

int memo[101][101][101];

int dfs(int depth, int from, int t) {
  if (~memo[depth][from][t])
    return memo[depth][from][t];

  int mi = inf;

  rep(i, F) {
    int frzr = freezer[i];

    if (used[frzr] || d[from][frzr] > T)
      continue;

    int cost = d[from][frzr];

    if (d[from][frzr] > t) {
      cost += d[from][frzr] - t;
    }

    if (d[frzr][G] <= T) {
      int now_t = max(t - d[from][frzr], 0ll);
      int heal = max(d[frzr][G] - now_t, 0ll);
      chmin(mi, cost + heal + d[frzr][G]);
    } else {
      used[frzr] = true;

      chmin(mi, cost + dfs(depth + 1, frzr, max(t - d[from][frzr], 0ll)));

      used[frzr] = false;
    }
  }

  return memo[depth][from][t] = mi;
}

signed main() {
  int cnt = 0;
  while (cin >> N >> T >> F >> M >> S >> G, N || T || F || M || S || G) {
    memset(memo, -1, sizeof(memo));
    rep(i, F) cin >> freezer[i];

    rep(i, N) rep(j, N) d[i][j] = i == j ? 0 : inf;

    rep(i, M) {
      int x, y, t;
      cin >> x >> y >> t;
      d[x][y] = d[y][x] = t;
    }

    rep(k, N) {
      rep(i, N) {
        rep(j, N) { chmin(d[i][j], d[i][k] + d[k][j]); }
      }
    }

    if (d[S][G] <= T) {
      cout << d[S][G] << endl;
    } else {
      int a = dfs(0, S, T);

      if (a == inf)
        cout << "Help!\\n";
      else
        cout << a << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define TP make_pair
#define rep(i, x) for (int i = 0; i < (x); ++i)
#define rep1(i, x) for (int i = 1; i <= (x); ++i)
#define rrep(i, x) for (int i = (x)-1; i >= 0; --i)
#define rrep1(i, x) for (int i = (x); i >= 1; --i)
#define int long long
#define FOR(i, a, x) for (int i = (a); i < (x); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  rep(i, vec.size()) is >> vec[i];
  return is;
}
template <typename T> vector<T> &sort(vector<T> &a) {
  sort(all(a));
  return a;
}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int d[101][101];
bool used[101];

int N, T, F, M, S, G;
int freezer[101];

int dfs(int from, int t) {
  int mi = inf;

  rep(i, F) {
    int frzr = freezer[i];

    if (used[frzr] || d[from][frzr] > T)
      continue;

    int cost = d[from][frzr];

    if (d[from][frzr] > t) {
      cost += d[from][frzr] - t;
    }

    if (d[frzr][G] <= T) {
      int now_t = max(t - d[from][frzr], 0ll);
      int heal = max(d[frzr][G] - now_t, 0ll);
      chmin(mi, cost + heal + d[frzr][G]);
    } else {
      used[frzr] = true;

      chmin(mi, cost + dfs(frzr, max(t - d[from][frzr], 0ll)));

      used[frzr] = false;
    }
  }

  return mi;
}

signed main() {
  int cnt = 0;
  while (cin >> N >> T >> F >> M >> S >> G, N || T || F || M || S || G) {
    rep(i, F) cin >> freezer[i];

    rep(i, N) rep(j, N) d[i][j] = i == j ? 0 : inf;

    rep(i, M) {
      int x, y, t;
      cin >> x >> y >> t;
      d[x][y] = d[y][x] = t;
    }

    rep(k, N) {
      rep(i, N) {
        rep(j, N) { chmin(d[i][j], d[i][k] + d[k][j]); }
      }
    }

    if (d[S][G] <= T) {
      cout << d[S][G] << endl;
    } else {
      int a = dfs(S, T);

      if (a == inf)
        cout << "Help!\\n";
      else
        cout << a << endl;
    }
  }
}