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

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
#define INF 100000000
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int main() {
  int n, m, k, l, a, h;
  while (cin >> n >> m >> l >> k >> a >> h, n || m || l || k || h || a) {
    vector<vector<int>> cost(n, vector<int>(n, INF));
    map<int, bool> hos;
    for (int i = 0; i < l; i++) {
      int x;
      cin >> x;
      hos[x] = true;
    }

    for (int i = 0; i < k; i++) {
      int x, y, t;
      cin >> x >> y >> t;
      cost[x][y] = t;
      cost[y][x] = t;
    }

    priority_queue<PP, vector<PP>, greater<PP>> que;
    vector<vector<int>> d(101, vector<int>(101, INF));
    que.push(PP(0, P(a, m)));
    // 時間、位置、残り
    d[a][m] = 0;
    // 場所,残り
    int ans = -1;
    while (!que.empty()) {
      //							cout<<"--"<<endl;
      PP p = que.top();
      que.pop();
      int now = p.second.first, time = p.first, left = p.second.second;
      if (now == h) {
        ans = time;
        break;
      }
      if (hos[now])
        for (int i = 1; i + left <= m; i++) {
          if (d[now][left + i] > time + i) {
            que.push(PP(time + i, P(now, left + i)));
            d[now][left + i] = time + i;
          }
        }
      for (int i = 0; i < n; i++) {
        if (cost[now][i] == INF) {
          continue;
        }
        //				cout<<"aaaaaaaaa"<<endl;
        if (left - cost[now][i] >= 0 &&
            d[i][left - cost[now][i]] > time + cost[now][i]) {
          que.push(PP(time + cost[now][i], P(i, left - cost[now][i])));
          d[i][left - cost[now][i]] = time + cost[now][i];
        }
      }
    }
    if (ans == -1)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
#define INF 100000000
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int main() {
  int n, m, k, l, a, h;
  while (cin >> n >> m >> l >> k >> a >> h, n || m || l || k || h || a) {
    vector<vector<int>> cost(n, vector<int>(n, INF));
    map<int, bool> hos;
    for (int i = 0; i < l; i++) {
      int x;
      cin >> x;
      hos[x] = true;
    }

    for (int i = 0; i < k; i++) {
      int x, y, t;
      cin >> x >> y >> t;
      cost[x][y] = t;
      cost[y][x] = t;
    }

    priority_queue<PP, vector<PP>, greater<PP>> que;
    vector<vector<int>> d(101, vector<int>(101, INF));
    que.push(PP(0, P(a, m)));
    // 時間、位置、残り
    d[a][m] = 0;
    // 場所,残り
    int ans = -1;
    while (!que.empty()) {
      //							cout<<"--"<<endl;
      PP p = que.top();
      que.pop();
      int now = p.second.first, time = p.first, left = p.second.second;
      if (now == h) {
        ans = time;
        break;
      }
      if (hos[now])
        for (int i = 1; i + left <= m; i++) {
          if (d[now][left + i] > time + i) {
            que.push(PP(time + i, P(now, left + i)));
          }
        }
      for (int i = 0; i < n; i++) {
        if (cost[now][i] == INF) {
          continue;
        }
        //				cout<<"aaaaaaaaa"<<endl;
        if (left - cost[now][i] >= 0 &&
            d[i][left - cost[now][i]] > time + cost[now][i]) {
          que.push(PP(time + cost[now][i], P(i, left - cost[now][i])));
          d[i][left - cost[now][i]] = time + cost[now][i];
        }
      }
    }
    if (ans == -1)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}