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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define INF 1152921504606846976

typedef pair<long long, int> P;

int N, M, K, S;
int Pcost, Qcost;
vector<int> path[100010];
long long cost[100010];
long long d[100010];
bool ng[100010];
bool dangerous[100010];

long long dijkstra() {
  for (int i = 0; i < N; i++)
    d[i] = INF;
  d[0] = 0;

  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, 0));

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int r = p.first;
    int s = p.second;
    if (d[s] < r)
      continue;

    for (auto t : path[s]) {
      if (d[t] > d[s] + cost[t]) {
        d[t] = d[s] + cost[t];
        q.push(P(d[t], t));
      }
    }
  }
  return d[N - 1];
}
/*
long long dijkstra() {
  vector<bool> used(N, false);
  vector<long long> d(N, INF);
  d[0] = 0;

  while (true) {
    int v = -1;
    for (int u=0; u<N; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
    }
    if (v == -1) break;
    used[v] = true;

    for (auto u : path[v]) {
      d[u] = min(d[u], d[v] + cost[u]);
    }
  }
  return d[N-1];
}
*/

int main() {
  cin >> N >> M >> K >> S;
  cin >> Pcost >> Qcost;

  queue<int> q;

  for (int i = 0; i < K; i++) {
    int c;
    cin >> c;
    dangerous[c - 1] = true;
    ng[c - 1] = true;
    q.push(c - 1);
    q.push(S);
  }
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    path[a - 1].push_back(b - 1);
    path[b - 1].push_back(a - 1);
  }
  // bfs
  vector<int> used(N, -1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int r = q.front();
    q.pop();
    if (r <= 0)
      continue;

    for (auto d : path[x]) {
      if (r <= used[d])
        continue;
      dangerous[d] = true;
      used[d] = r;
      q.push(d);
      q.push(r - 1);
    }
  }
  for (int i = 0; i < N; i++) {
    if (ng[i])
      cost[i] = INF;
    else {
      if (dangerous[i])
        cost[i] = Qcost;
      else
        cost[i] = Pcost;
    }
  }
  cost[N - 1] = 0;
  cout << dijkstra() << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define INF 1152921504606846976

int N, M, K, S;
int P, Q;
vector<int> path[100010];
long long cost[100010];
bool ng[100010];
bool dangerous[100010];

// O(N^2)
long long dijkstra() {
  vector<bool> used(N, false);
  vector<long long> d(N, INF);
  d[0] = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < N; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;

    for (auto u : path[v]) {
      d[u] = min(d[u], d[v] + cost[u]);
    }
  }
  return d[N - 1];
}

int main() {
  cin >> N >> M >> K >> S;
  cin >> P >> Q;

  queue<int> q;

  for (int i = 0; i < K; i++) {
    int c;
    cin >> c;
    dangerous[c - 1] = true;
    ng[c - 1] = true;
    q.push(c - 1);
    q.push(S);
  }
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    path[a - 1].push_back(b - 1);
    path[b - 1].push_back(a - 1);
  }
  // bfs
  vector<int> used(N, -1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int r = q.front();
    q.pop();
    if (r <= 0)
      continue;

    for (auto d : path[x]) {
      if (r <= used[d])
        continue;
      dangerous[d] = true;
      used[d] = r;
      q.push(d);
      q.push(r - 1);
    }
  }
  for (int i = 0; i < N; i++) {
    if (ng[i])
      cost[i] = INF;
    else {
      if (dangerous[i])
        cost[i] = Q;
      else
        cost[i] = P;
    }
  }
  cost[N - 1] = 0;
  cout << dijkstra() << "\\n";
  return 0;
}