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

#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define INF (1000000000000000000LL)

typedef pair<long long, int> PLLI;

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

  long long P, Q;
  cin >> P >> Q;

  vector<long long> town(N);
  for (int i = 0; i < N; ++i)
    town[i] = P;

  town[N - 1] = 0;

  queue<PLLI> q;
  set<int> zom;
  for (int i = 0; i < K; ++i) {
    int temp;
    cin >> temp;
    town[temp - 1] = INF;
    q.push(PLLI(S, temp - 1));
    zom.insert(temp - 1);
  }

  vector<int> road[N];
  vector<int> temp_road[N];
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    temp_road[a - 1].push_back(b - 1);
    temp_road[b - 1].push_back(a - 1);
    if (zom.count(a - 1) == 0 && zom.count(b - 1) == 0) {
      road[a - 1].push_back(b - 1);
      road[b - 1].push_back(a - 1);
    }
  }

  while (!q.empty()) {
    PLLI p = q.front();
    q.pop();
    long long d = p.first;
    int t = p.second;
    --d;
    for (auto x : temp_road[t]) {
      if (town[x] == P) {
        town[x] = Q;
        if (d != 0)
          q.push(PLLI(d, x));
      }
    }
  }

  priority_queue<PLLI, vector<PLLI>, greater<PLLI>> pq;
  vector<long long> cost(N);

  for (int i = 0; i < N; ++i)
    cost[i] = INF;

  cost[0] = 0;
  pq.push(PLLI(0, 0));

  while (!pq.empty()) {
    PLLI p = pq.top();
    pq.pop();
    long long c = p.first;
    int t = p.second;

    if (t == N - 1)
      break;
    if (cost[t] < c)
      continue;

    for (auto x : road[t]) {
      if (cost[x] <= c + town[x])
        continue;
      cost[x] = c + town[x];
      pq.push(PLLI(cost[x], x));
    }
  }

  cout << cost[N - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define INF (1000000000000000000LL)

typedef pair<long long, int> PLLI;

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

  long long P, Q;
  cin >> P >> Q;

  vector<long long> town(N);
  for (int i = 0; i < N; ++i)
    town[i] = P;

  town[N - 1] = 0;

  queue<PLLI> q;
  set<int> zom;
  for (int i = 0; i < K; ++i) {
    int temp;
    cin >> temp;
    town[temp - 1] = INF;
    q.push(PLLI(S, temp - 1));
    zom.insert(temp - 1);
  }

  vector<int> road[N];
  vector<int> temp_road[N];
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    temp_road[a - 1].push_back(b - 1);
    temp_road[b - 1].push_back(a - 1);
    if (zom.count(a - 1) == 0 && zom.count(b - 1) == 0) {
      road[a - 1].push_back(b - 1);
      road[b - 1].push_back(a - 1);
    }
  }

  while (!q.empty()) {
    PLLI p = q.front();
    q.pop();
    long long d = p.first;
    int t = p.second;
    --d;
    for (auto x : temp_road[t]) {
      if (town[x] == P) {
        town[x] = Q;
        if (d != 0)
          q.push(PLLI(d, x));
      }
    }
  }

  priority_queue<PLLI, vector<PLLI>, greater<PLLI>> pq;
  vector<long long> cost(N);

  for (int i = 0; i < N; ++i)
    cost[i] = INF;

  cost[0] = 0;
  pq.push(PLLI(0, 0));

  while (!pq.empty()) {
    PLLI p = pq.top();
    pq.pop();
    long long c = p.first;
    int t = p.second;

    if (t == N - 1)
      break;
    if (cost[t] < c)
      continue;

    for (auto x : road[t]) {
      if (cost[x] < c + town[x])
        continue;
      cost[x] = c + town[x];
      pq.push(PLLI(cost[x], x));
    }
  }

  cout << cost[N - 1] << endl;

  return 0;
}