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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> edge;
const int INF = 1 << 22;
struct state {
  int town, time, fz_time;
  state(int town_, int time_, int fz_time_)
      : town(town_), time(time_), fz_time(fz_time_) {}
  bool operator<(const state &other) const {
    return time > other.time; //
  }
};
int N, M, L, K, A, H;
int dijkstra(vector<vector<edge>> graph, bool has_fz[]) {
  priority_queue<state> pq;
  pq.push(state(A, 0, M));
  bool visited[N][M + 1];
  fill_n((bool *)visited, N * (M + 1), false);
  while (!pq.empty()) {
    state cur = pq.top();
    pq.pop();
    // cout << "town: " << cur.town << ", time: " << cur.time << ", fz: " <<
    // cur.fz_time << endl;
    if (cur.town == H) {
      return cur.time;
    }
    if (visited[cur.town][cur.fz_time]) {
      continue;
    }
    visited[cur.town][cur.fz_time] = true;
    for (int i = 0; i < graph[cur.town].size(); ++i) {
      int to = graph[cur.town][i].first;
      int cost = graph[cur.town][i].second;
      int r = 0;
      if (cur.fz_time - cost < 0) {
        r = -1 * (cur.fz_time - cost);
      }
      if (r + cur.fz_time <= M) {
        pq.push(state(to, cur.time + cost + r, cur.fz_time - cost + r));
      }
    }
  }
  return -1;
}
void compress(vector<vector<edge>> &graph, bool has_fz[]) {
  int adj[N][N];
  fill_n((int *)adj, N * N, INF);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < graph[i].size(); ++j) {
      int t = graph[i][j].first;
      int c = graph[i][j].second;
      adj[i][t] = c;
    }
  }
  for (int k = 0; k < N; ++k) {
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }
  graph.clear();
  graph = vector<vector<edge>>(N);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (adj[i][j] < INF && has_fz[j]) {
        graph[i].push_back(make_pair(j, adj[i][j]));
      }
    }
  }
}
int main() {
  while (true) {
    cin >> N >> M >> L >> K >> A >> H;
    if ((N | M | L | K | A | H) == 0) {
      break;
    }
    bool has_fz[N];
    fill_n(has_fz, N, false);
    has_fz[A] = has_fz[H] = true;
    for (int i = 0; i < L; ++i) {
      int tmp;
      cin >> tmp;
      has_fz[tmp] = true;
    }
    vector<vector<edge>> graph(N);
    for (int i = 0; i < K; ++i) {
      int a, b, t;
      cin >> a >> b >> t;
      graph[a].push_back(make_pair(b, t));
      graph[b].push_back(make_pair(a, t));
    }
    compress(graph, has_fz);
    int ans = dijkstra(graph, has_fz);
    if (ans == -1) {
      cout << "Help!" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> edge;
const int INF = 1 << 22;
struct state {
  int town, time, fz_time;
  state(int town_, int time_, int fz_time_)
      : town(town_), time(time_), fz_time(fz_time_) {}
  bool operator<(const state &other) const {
    return time > other.time; //
  }
};
int N, M, L, K, A, H;
int dijkstra(vector<vector<edge>> graph, bool has_fz[]) {
  priority_queue<state> pq;
  pq.push(state(A, 0, M));
  bool visited[N][M + 1];
  fill_n((bool *)visited, N * (M + 1), false);
  while (!pq.empty()) {
    state cur = pq.top();
    pq.pop();
    // cout << "town: " << cur.town << ", time: " << cur.time << ", fz: " <<
    // cur.fz_time << endl;
    if (cur.fz_time < 0) {
      continue;
    }
    if (cur.town == H) {
      return cur.time;
    }
    if (visited[cur.town][cur.fz_time]) {
      continue;
    }
    visited[cur.town][cur.fz_time] = true;
    for (int i = 0; i < graph[cur.town].size(); ++i) {
      int to = graph[cur.town][i].first;
      int cost = graph[cur.town][i].second;
      if (has_fz[cur.town]) {
        for (int r = 0; r <= (M - cur.fz_time); ++r) {
          pq.push(state(to, cur.time + cost + r, cur.fz_time - cost + r));
        }
      } else {
        pq.push(state(to, cur.time + cost, cur.fz_time - cost));
      }
    }
  }
  return -1;
}
void compress(vector<vector<edge>> &graph, bool has_fz[]) {
  int adj[N][N];
  fill_n((int *)adj, N * N, INF);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < graph[i].size(); ++j) {
      int t = graph[i][j].first;
      int c = graph[i][j].second;
      adj[i][t] = c;
    }
  }
  for (int k = 0; k < N; ++k) {
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }
  graph.clear();
  graph = vector<vector<edge>>(N);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (adj[i][j] < INF && has_fz[j]) {
        graph[i].push_back(make_pair(j, adj[i][j]));
      }
    }
  }
}
int main() {
  while (true) {
    cin >> N >> M >> L >> K >> A >> H;
    if ((N | M | L | K | A | H) == 0) {
      break;
    }
    bool has_fz[N];
    fill_n(has_fz, N, false);
    has_fz[A] = has_fz[H] = true;
    for (int i = 0; i < L; ++i) {
      int tmp;
      cin >> tmp;
      has_fz[tmp] = true;
    }
    vector<vector<edge>> graph(N);
    for (int i = 0; i < K; ++i) {
      int a, b, t;
      cin >> a >> b >> t;
      graph[a].push_back(make_pair(b, t));
      graph[b].push_back(make_pair(a, t));
    }
    compress(graph, has_fz);
    int ans = dijkstra(graph, has_fz);
    if (ans == -1) {
      cout << "Help!" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}