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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int N, M, L, K, A, H;
int dist[101][101];

struct State {
  int pos, time;
};

bool operator<(const State &l, const State &r) { return l.time > r.time; }

int main() {
  while (true) {
    cin >> N >> M >> L >> K >> A >> H;
    if (N == 0)
      break;
    memset(dist, -1, sizeof(dist));
    vector<int> freezer;
    for (int i = 0; i < L; ++i) {
      int l;
      cin >> l;
      freezer.push_back(l);
    }
    freezer.push_back(H);
    for (int i = 0; i < K; ++i) {
      int X, Y, T;
      cin >> X >> Y >> T;
      dist[X][Y] = dist[Y][X] = T;
    }
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        if (dist[i][j] == -1)
          continue;
        for (int k = 0; k < N; ++k) {
          if (dist[i][k] == -1)
            continue;
          if (dist[j][k] == -1 || dist[j][k] > dist[j][i] + dist[i][k]) {
            dist[j][k] = dist[k][j] = dist[j][i] + dist[i][k];
          }
        }
      }
    }
    priority_queue<State> q;
    q.push({A, 0});
    vector<int> mi(N, 1 << 30);
    int ans = -1;
    while (!q.empty()) {
      State cur = q.top();
      q.pop();
      if (cur.pos == H) {
        ans = cur.time;
        if (ans > M)
          ans += ans - M;
        break;
      }
      mi[cur.pos] = cur.time;
      for (int n : freezer) {
        int len = dist[cur.pos][n];
        if (len == -1 || len > M)
          continue;
        if (cur.time + len >= mi[n])
          continue;
        q.push({n, cur.time + len});
        mi[n] = cur.time + len;
      }
    }
    if (ans == -1) {
      cout << "Help!" << endl;
    } else {
      cout << ans << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int N, M, L, K, A, H;
int dist[101][101];

struct State {
  int pos, time;
};

bool operator<(const State &l, const State &r) { return l.time > r.time; }

int main() {
  while (true) {
    cin >> N >> M >> L >> K >> A >> H;
    if (N == 0)
      break;
    memset(dist, -1, sizeof(dist));
    vector<int> freezer;
    for (int i = 0; i < L; ++i) {
      int l;
      cin >> l;
      freezer.push_back(l);
    }
    freezer.push_back(H);
    for (int i = 0; i < K; ++i) {
      int X, Y, T;
      cin >> X >> Y >> T;
      dist[X][Y] = dist[Y][X] = T;
    }
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        if (dist[i][j] == -1)
          continue;
        for (int k = 0; k < N; ++k) {
          if (dist[i][k] == -1)
            continue;
          if (dist[j][k] == -1 || dist[j][k] > dist[j][i] + dist[i][k]) {
            dist[j][k] = dist[k][j] = dist[j][i] + dist[i][k];
          }
        }
      }
    }
    priority_queue<State> q;
    q.push({A, 0});
    vector<bool> visited(N);
    int ans = -1;
    while (!q.empty()) {
      State cur = q.top();
      q.pop();
      if (cur.pos == H) {
        ans = cur.time;
        if (ans > M)
          ans += ans - M;
        break;
      }
      visited[cur.pos] = true;
      for (int n : freezer) {
        if (visited[n])
          continue;
        int len = dist[cur.pos][n];
        if (len == -1 || len > M)
          continue;
        q.push({n, cur.time + len});
      }
    }
    if (ans == -1) {
      cout << "Help!" << endl;
    } else {
      cout << ans << endl;
    }
  }
}