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

#include <bits/stdc++.h>

#define DEBUG(C) cerr << #C << " = " << C << endl

using namespace std;

const int INF = 1e9;
const int MAX = 111;
set<int> floze;
int dist[MAX][MAX];
vector<pair<int, int>> graph[MAX];
using state = tuple<int, int, int>;
// keika, nokori, now town

void solve(int n) {
  for (int i = 0; i < MAX; i++) {
    graph[i].clear();
    for (int j = 0; j < MAX; j++) {
      dist[i][j] = INF;
    }
  }
  floze.clear();
  int m, l, k, start, goal;
  scanf("%d%d%d%d%d", &m, &l, &k, &start, &goal);
  floze.insert(start);
  floze.insert(goal);
  for (int i = 0; i < l; i++) {
    int buf;
    scanf("%d", &buf);
    floze.insert(buf);
  }
  for (int i = 0; i < k; i++) {
    int x, y, t;
    scanf("%d%d%d", &x, &y, &t);
    graph[x].emplace_back(make_pair(y, t));
    graph[y].emplace_back(make_pair(x, t));
  }
  priority_queue<state, vector<state>, greater<state>> pq;
  pq.push(make_tuple(0, m, start));
  dist[start][m] = 0;
  while (!pq.empty()) {
    //    DEBUG(pq.size());
    int now, keika, nokori;
    tie(keika, nokori, now) = pq.top();
    pq.pop();
    if (dist[now][nokori] < keika) {
      continue;
    }
    if (floze.count(now)) {
      for (int i = nokori + 1; i <= m; i++) {
        if (dist[now][i] > keika + i - nokori) {
          dist[now][i] = keika + i - nokori;
          pq.push(make_tuple(keika + i - nokori, i, now));
        }
      }
    }
    for (auto e : graph[now]) {
      if (nokori - e.second < 0) {
        continue;
      }
      if (dist[e.first][nokori - e.second] <= keika + e.second) {
        continue;
      }
      dist[e.first][nokori - e.second] = keika + e.second;
      pq.push(make_tuple(keika + e.second, nokori - e.second, e.first));
    }
  }
  int ans = INF;
  for (int i = 0; i < MAX; i++) {
    ans = min(ans, dist[goal][i]);
  }
  if (ans == INF) {
    puts("Help!");
  } else {
    printf("%d\\n", ans);
  }
}

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    solve(n);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define DEBUG(C) cerr << #C << " = " << C << endl

using namespace std;

const int INF = 1e9;
const int MAX = 111;
set<int> floze;
int dist[MAX][MAX];
vector<pair<int, int>> graph[MAX];
using state = tuple<int, int, int>;
// keika, nokori, now town

void solve(int n) {
  for (int i = 0; i < MAX; i++) {
    graph[i].clear();
    for (int j = 0; j < MAX; j++) {
      dist[i][j] = INF;
    }
  }
  floze.clear();
  int m, l, k, start, goal;
  scanf("%d%d%d%d%d", &m, &l, &k, &start, &goal);
  floze.insert(start);
  floze.insert(goal);
  for (int i = 0; i < l; i++) {
    int buf;
    scanf("%d", &buf);
    floze.insert(buf);
  }
  for (int i = 0; i < k; i++) {
    int x, y, t;
    scanf("%d%d%d", &x, &y, &t);
    graph[x].emplace_back(make_pair(y, t));
    graph[y].emplace_back(make_pair(x, t));
  }
  priority_queue<state, vector<state>, greater<state>> pq;
  pq.push(make_tuple(0, m, start));
  dist[start][m] = 0;
  while (!pq.empty()) {
    int now, keika, nokori;
    tie(keika, nokori, now) = pq.top();
    pq.pop();
    if (dist[now][nokori] < keika) {
      continue;
    }
    if (floze.count(now)) {
      for (int i = nokori + 1; i <= m; i++) {
        pq.push(make_tuple(keika + i - nokori, i, now));
      }
    }
    for (auto e : graph[now]) {
      if (nokori - e.second < 0) {
        continue;
      }
      if (dist[e.first][nokori - e.second] <= keika + e.second) {
        continue;
      }
      dist[e.first][nokori - e.second] = keika + e.second;
      pq.push(make_tuple(keika + e.second, nokori - e.second, e.first));
    }
  }
  int ans = INF;
  for (int i = 0; i < MAX; i++) {
    ans = min(ans, dist[goal][i]);
  }
  if (ans == INF) {
    puts("Help!");
  } else {
    printf("%d\\n", ans);
  }
}

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    solve(n);
  }
  return 0;
}