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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int solve() {
  int N, M, L, K, A, H;
  bool freezer[SIZE] = {};
  int x, y, t;
  vector<pair<int, int>> way[SIZE];

  scanf("%d%d%d%d%d%d", &N, &M, &L, &K, &A, &H);
  freezer[A] = freezer[H] = true;

  if (N == 0)
    return false;

  for (int i = 0; i < L; i++) {
    scanf("%d", &x);
    freezer[x] = true;
  }

  for (int i = 0; i < K; i++) {
    scanf("%d%d%d", &x, &y, &t);
    way[x].push_back({y, t});
    way[y].push_back({x, t});
  }

  bool visited[101][101] = {};
  int costMemo[101][101] = {};

  priority_queue<pair<int, pair<int, int>>> pq;
  pq.push({0, {A, M}});

  while (pq.size()) {
    auto p = pq.top();
    pq.pop();

    int cost = p.first;
    int pos = p.second.first;
    int rest = p.second.second;

    if (visited[pos][rest])
      continue;
    visited[pos][rest] = true;
    costMemo[pos][rest] = -cost;

    if (pos == H) {
      printf("%d\\n", -cost);
      return true;
    }

    if (freezer[pos] && rest < M)
      pq.push({cost - 1, {pos, rest + 1}});

    for (int i = 0; i < way[pos].size(); i++) {
      if (way[pos][i].second <= rest &&
          !visited[way[pos][i].first][rest - way[pos][i].second])
        pq.push({cost - way[pos][i].second,
                 {way[pos][i].first, rest - way[pos][i].second}});
    }
  }

  puts("Help!");

  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int solve() {
  int N, M, L, K, A, H;
  bool freezer[SIZE] = {};
  int x, y, t;
  vector<pair<int, int>> way[SIZE];

  scanf("%d%d%d%d%d%d", &N, &M, &L, &K, &A, &H);
  freezer[A] = freezer[H] = true;

  if (N == 0)
    return false;

  for (int i = 0; i < L; i++) {
    scanf("%d", &x);
    freezer[x] = true;
  }

  for (int i = 0; i < K; i++) {
    scanf("%d%d%d", &x, &y, &t);
    way[x].push_back({y, t});
    way[y].push_back({x, t});
  }

  bool visited[101][101] = {};

  priority_queue<pair<int, pair<int, int>>> pq;

  pq.push({0, {A, M}});

  while (pq.size()) {
    auto p = pq.top();
    pq.pop();

    int cost = p.first;
    int pos = p.second.first;
    int rest = p.second.second;

    if (visited[pos][rest])
      continue;
    visited[pos][rest] = true;

    if (pos == H) {
      printf("%d\\n", -cost);
      return true;
    }

    if (freezer[pos])
      for (int i = rest + 1; i <= M; i++) {
        if (!visited[pos][i])
          pq.push({cost - i + rest, {pos, i}});
      }

    for (int i = 0; i < way[pos].size(); i++) {
      if (way[pos][i].second <= rest &&
          !visited[way[pos][i].first][rest - way[pos][i].second])
        pq.push({cost - way[pos][i].second,
                 {way[pos][i].first, rest - way[pos][i].second}});
    }
  }

  puts("Help!");

  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}