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

#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define INF 900000000

using namespace std;

int n, k, c[5010], r[5010], cost[5010];
vector<int> road[5010];
vector<pair<int, int>> adj[5010];
bool done[5010];
pair<int, bool> alr[5010];
set<int> st;

int dijkstra() {
  for (int i = 0; i < 5010; i++)
    cost[i] = INF;
  cost[1] = 0;
  priority_queue<pair<int, int>> que;
  memset(done, false, sizeof(done));
  que.push(make_pair(0, 1));
  while (!que.empty()) {
    int a = que.top().second;
    que.pop();
    if (done[a])
      continue;
    done[a] = true;
    for (auto u : adj[a]) {
      int b = u.first, w = u.second;
      if (cost[b] > cost[a] + w) {
        cost[b] = cost[a] + w;
        que.push(make_pair(-cost[b], b));
      }
    }
  }
  return cost[n];
}

int main() {
  int a, b;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> r[i];
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    road[a].push_back(b);
    road[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    for (int i = 0; i < 5010; i++) {
      alr[i].first = INF;
      alr[i].second = false;
    }
    st.clear();
    // dfs(1, i, i);
  }

  //****
  for (int i = 1; i <= n; i++) {
    queue<pair<int, int>> Q;
    Q.push(make_pair(i, 0));
    bool vis[5010] = {0};
    while (!Q.empty()) {
      int x = Q.front().first, y = Q.front().second;
      Q.pop();
      if (vis[x])
        continue;
      vis[x] = true;
      if (y < r[i]) {
        for (auto z : road[x])
          Q.push(make_pair(z, y + 1));
      }
    }
    for (int j = 1; j <= n; j++)
      if (vis[j]) {
        adj[i].push_back(make_pair(j, c[i]));
      }
  }
  //*****

  cout << dijkstra() << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define INF 900000000

using namespace std;

int n, k, c[5010], r[5010], cost[5010];
vector<int> road[5010];
vector<pair<int, int>> adj[5010];
bool done[5010];
pair<int, bool> alr[5010];
set<int> st;

void dfs(int dep, int lim, int num) {
  if (dep > r[lim])
    return;
  alr[num].second = true;
  alr[num].first = dep;
  for (auto u : road[num]) {
    if (!st.count(u)) {
      st.insert(u);
      adj[lim].push_back({u, c[lim]});
    }
    if (alr[u].second = false || alr[u].first > dep)
      dfs(dep + 1, lim, u);
  }
}

int dijkstra() {
  for (int i = 0; i < 5010; i++)
    cost[i] = INF;
  cost[1] = 0;
  priority_queue<pair<int, int>> que;
  memset(done, false, sizeof(done));
  que.push({0, 1});
  while (!que.empty()) {
    int a = que.top().second;
    que.pop();
    if (done[a])
      continue;
    done[a] = true;
    for (auto u : adj[a]) {
      int b = u.first, w = u.second;
      if (done[b])
        continue;
      if (cost[b] > cost[a] + w) {
        cost[b] = cost[a] + w;
        que.push({-cost[b], b});
      }
    }
  }
  return cost[n];
}

int main() {
  int a, b;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> r[i];
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    road[a].push_back(b);
    road[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    for (int i = 0; i < 5010; i++) {
      alr[i].first = INF;
      alr[i].second = false;
    }
    st.clear();
    dfs(1, i, i);
  }
  cout << dijkstra() << '\\n';
  return 0;
}