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

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int n, m, l, k, a, h;
  while (cin >> n >> m >> l >> k >> a >> h && n) {
    vector<int> d(l);
    for (int i = 0; i < l; i++)
      cin >> d[i];
    l += 2;
    d.push_back(a);
    d.push_back(h);

    vector<vector<int>> e(n, vector<int>(n, 101010));
    for (int i = 0; i < n; i++)
      e[i][i] = 0;
    for (int i = 0; i < k; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      e[x][y] = z;
      e[y][x] = z;
    }

    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          e[i][j] = min(e[i][j], e[i][k] + e[k][j]);
          e[j][i] = e[i][j];
        }
      }
    }
    for (int i = 0; i < l - 1; i++) {
      for (int j = i + 1; j < l; j++) {
        int x = d[i];
        int y = d[j];
        if (e[x][y] > m)
          e[x][y] = 101010, e[y][x] = 101010;
      }
    }

    for (int k = 0; k < l; k++) {
      for (int i = 0; i < l; i++) {
        for (int j = 0; j < l; j++) {
          int x = d[i];
          int y = d[j];
          int z = d[k];
          e[x][y] = min(e[x][y], e[x][z] + e[z][y]);
          e[y][x] = e[x][y];
        }
      }
    }

    if (e[a][h] >= 101010)
      cout << "Help!" << endl;
    else if (e[a][h] > m)
      cout << e[a][h] * 2 - m << endl;
    else
      cout << e[a][h] << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define inf 1e9
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> pp;

int main(void) {
  int N, M, L, K, A, H;
  while (cin >> N >> M >> L >> K >> A >> H && N) {
    vector<vector<int>> g(N, vector<int>(N, inf));
    for (int i = 0; i < N; i++)
      g[i][i] = 0;
    vector<int> fz(L);
    for (int i = 0; i < L; i++)
      scanf("%d", &fz[i]);
    fz.push_back(A);
    for (int i = 0; i < K; i++) {
      int x, y, t;
      scanf("%d%d%d", &x, &y, &t);
      g[x][y] = t;
      g[y][x] = t;
    }

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          continue;
        for (int k = 0; k < N; k++) {
          if (g[i][j] > g[i][k] + g[k][j]) {
            g[i][j] = g[i][k] + g[k][j];
            g[j][i] = g[i][j];
          }
        }
      }
    }

    int ans = inf;
    priority_queue<pp> que;
    for (int i = 0; i < fz.size(); i++) {
      int u = fz[i];
      if (u == A && g[H][u] <= M) {
        ans = min(ans, g[H][u]);
        continue;
      }
      if (g[H][u] <= M)
        que.push(make_pair(g[H][u], make_pair(u, M - g[H][u])));
    }

    while (!que.empty() && ans == inf) {
      pp p = que.top();
      que.pop();
      int cost = p.first, t = p.second.first, ice = p.second.second;
      for (int i = 0; i < fz.size(); i++) {
        int u = fz[i];
        if (u == t || g[t][u] > M || g[u][A] > g[t][A])
          continue;

        int a = 0;
        if (ice < g[t][u])
          a = g[t][u] - ice;

        if (u == A)
          ans = min(ans, cost + g[t][u] + a);
        else
          que.push(
              make_pair(cost + g[t][u] + a, make_pair(u, ice - g[t][u] + a)));
      }
    }
    if (ans == inf)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }
}