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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define LL long long
#define PAIR pair<LL, int>
using namespace std;
int main() {
  int N, M, K, S, P, Q;
  cin >> N >> M >> K >> S >> P >> Q;
  int *zombie;
  zombie = new int[K];
  short *danger;
  danger = new short[N + 1]{0};
  for (int i = 0; i < K; ++i) {
    cin >> zombie[i];
    danger[zombie[i]] = 2;
  }
  vector<vector<int>> road(N + 1);
  int a, b;
  for (int i = 0; i < M; ++i) {
    cin >> a >> b;
    road[a].push_back(b);
    road[b].push_back(a);
  }
  queue<int> date, stock;
  for (int i = 0; i < K; ++i) {
    date.push(zombie[i]);
  }
  int dummy;
  for (int i = 0; i < S; ++i) {
    while (!date.empty()) {
      dummy = date.front();
      date.pop();
      for (int m = road[dummy].size(), j = 0; j < m; ++j) {
        if (danger[road[dummy][j]] == 0) {
          danger[road[dummy][j]] = 1;
          stock.push(road[dummy][j]);
        }
      }
    }
    swap(date, stock);
  }
  LL inf = N * Q * 2;
  PAIR *map;
  map = new PAIR[N + 1];
  for (int i = 1; i <= N; ++i) {
    map[i].first = inf;
    map[i].second = i;
  }
  bool *used;
  used = new bool[N + 1]{false};
  map[1].first = 0;
  priority_queue<PAIR, vector<PAIR>, greater<PAIR>> que;
  que.push(map[1]);
  PAIR top;
  while (map[N].first == inf) {
    top = que.top();
    que.pop();
    if (used[top.second])
      continue;
    used[top.second] = true;
    for (int m = road[top.second].size(), i = 0; i < m; ++i) {
      if (danger[road[top.second][i]] == 1) {
        if (!used[road[top.second][i]]) {
          map[road[top.second][i]].first = top.first + Q;
          que.push(map[road[top.second][i]]);
        }
      } else if (danger[road[top.second][i]] == 0) {
        if (!used[road[top.second][i]]) {
          map[road[top.second][i]].first = top.first + P;
          que.push(map[road[top.second][i]]);
        }
      }
    }
  }
  if (danger[N] == 1) {
    cout << map[N].first - Q << endl;
  } else {
    cout << map[N].first - P << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define LL long long
#define PAIR pair<LL, int>
using namespace std;
int main() {
  int N, M, K, S, P, Q;
  cin >> N >> M >> K >> S >> P >> Q;
  int *zombie;
  zombie = new int[K];
  short *danger;
  danger = new short[N + 1]{0};
  for (int i = 0; i < K; ++i) {
    cin >> zombie[i];
    danger[zombie[i]] = 2;
  }
  vector<vector<int>> road(N + 1);
  int a, b;
  for (int i = 0; i < M; ++i) {
    cin >> a >> b;
    road[a].push_back(b);
    road[b].push_back(a);
  }
  queue<int> date, stock;
  for (int i = 0; i < K; ++i) {
    date.push(zombie[i]);
  }
  int dummy;
  for (int i = 0; i < S; ++i) {
    while (!date.empty()) {
      dummy = date.front();
      date.pop();
      for (int m = road[dummy].size(), j = 0; j < m; ++j) {
        if (danger[road[dummy][j]] == 0) {
          danger[road[dummy][j]] = 1;
          stock.push(road[dummy][j]);
        }
      }
    }
    swap(date, stock);
  }
  LL inf = N * Q * 2;
  PAIR *map;
  map = new PAIR[N + 1];
  for (int i = 1; i <= N; ++i) {
    map[i].first = inf;
    map[i].second = i;
  }
  map[1].first = 0;
  priority_queue<PAIR, vector<PAIR>, greater<PAIR>> que;
  que.push(map[1]);
  PAIR top;
  while (map[N].first == inf) {
    top = que.top();
    que.pop();
    if (map[top.second].first < top.first)
      continue;
    for (int m = road[top.second].size(), i = 0; i < m; ++i) {
      if (danger[road[top.second][i]] == 1) {
        if (map[road[top.second][i]].first > top.first + Q) {
          map[road[top.second][i]].first = top.first + Q;
          que.push(map[road[top.second][i]]);
        }
      } else if (danger[road[top.second][i]] == 0) {
        if (map[road[top.second][i]].first > top.first + P) {
          map[road[top.second][i]].first = top.first + P;
          que.push(map[road[top.second][i]]);
        }
      }
    }
  }
  if (danger[N] == 1) {
    cout << map[N].first - Q << endl;
  } else {
    cout << map[N].first - P << endl;
  }
  return 0;
}