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

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

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define dbg(x) cerr << #x << " = " << (x) << endl

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int n, m, l, k, a, h;
int d[100][100];
bool f[100];

int main() {
  while (scanf("%d%d%d%d%d%d", &n, &m, &l, &k, &a, &h), n) {
    int s, t, c, ans;
    rep(i, n) f[i] = 0;
    rep(i, n) rep(j, n) d[i][j] = inf;
    rep(i, l) scanf("%d", &s), f[s] = 1;
    f[a] = f[h] = 1;
    rep(i, k) scanf("%d%d%d", &s, &t, &c), d[s][t] = d[t][s] = c;
    rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    rep(i, n) rep(j, n) if (!f[i] || !f[j] || d[i][j] > m) d[i][j] = inf;
    rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    ans = d[a][h];
    if (ans > m)
      ans += ans - m;
    if (ans > inf)
      puts("Help!");
    else
      printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define mp make_pair
#define pb push_back

using namespace std;

struct Node {
  int time, cur, blood;
  bool operator<(const Node &r) const { return time > r.time; }
};

int main() {
  int n, m, l, k, s, g;
  while (cin >> n >> m >> l >> k >> s >> g, n) {

    bool freez[n];
    fill(freez, freez + n, 0);
    rep(i, l) {
      int t;
      cin >> t;
      freez[t] = 1;
    }
    vector<vector<pair<int, int>>> edge(n);
    rep(i, k) {
      int a, b, c;
      cin >> a >> b >> c;
      edge[a].pb(mp(b, c)), edge[b].pb(mp(a, c));
    }

    bool V[n][m + 1];
    rep(i, n) rep(j, m + 1) V[i][j] = 0;
    Node node;
    node.cur = s, node.time = 0, node.blood = m;
    priority_queue<Node> Q;
    Q.push(node);

    while (!Q.empty()) {
      node = Q.top();
      Q.pop();
      V[node.cur][node.blood] = 1;

      if (node.cur == g) {
        cout << node.time << endl;
        goto END;
      }

      foreach (i, edge[node.cur]) {
        if (i->second > node.blood)
          continue;
        Node next = node;
        next.cur = i->first, next.time += i->second, next.blood -= i->second;
        if (V[next.cur][next.blood])
          continue;

        Q.push(next);
      }
      if (freez[node.cur])
        for (int i = 1; i + node.blood <= m; i++) {
          Node next = node;
          next.time += i, next.blood += i;
          if (V[next.cur][next.blood])
            continue;
          Q.push(next);
        }
    }

    cout << "Help!" << endl;
  END:;
  }
  return 0;
}