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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> vector<T> &sort(vector<T> &a) {
  sort(all(a));
  return a;
}

const int inf = 3e18;
const ll linf = 3e18;
const double eps = 1e-9;

int N, M, K, S;
int P, Q;

struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};
typedef vector<edge> edges;

vi zombie;

vi G1[100010];
edges G2[100010];

int zombie_dist[100010];
bool is_zombie[100010];
bool is_danger[100010];
int dist[100010];

vpii roads;

void dijkstra(int s) {
  fill_n(dist, N + 1, inf);

  priority_queue<pii, vector<pii>, greater<pii>> pq;

  dist[s] = 0;

  pq.push(pii(0, s));

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

    int u = p.scd;

    if (dist[u] < p.fst)
      continue;

    for (edge e : G2[u]) {
      if (dist[e.to] > dist[u] + e.cost) {
        dist[e.to] = dist[u] + e.cost;
        pq.push(pii(dist[e.to], e.to));
      }
    }
  }
}

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

  rep(i, K) {
    int C;
    cin >> C;
    --C;
    zombie.PB(C);
  }

  rep(i, M) {
    int A, B;
    cin >> A >> B;
    --A, --B;

    roads.push_back(pii(A, B));

    G1[A].PB(B);
    G1[B].PB(A);
  }

  // ??±????????????BFS??§?¨???¶
  fill_n(zombie_dist, N + 1, inf);

  queue<int> q;

  for (int num : zombie) {
    zombie_dist[num] = 0;
    is_zombie[num] = true;
    q.push(num);

    while (q.size()) {
      int u = q.front();
      q.pop();

      if (zombie_dist[u] >= S)
        continue;

      for (int v : G1[u]) {
        if (zombie_dist[v] > zombie_dist[u] + 1) {
          zombie_dist[v] = zombie_dist[u] + 1;
          is_danger[v] = true;
          q.push(v);
        }
      }
    }
  }

  // ????????????????±?????????°??????????§????
  for (pii road : roads) {
    G2[road.fst].PB(edge(
        road.scd,
        road.scd == N - 1
            ? 0
            : (is_zombie[road.scd] ? inf : (is_danger[road.scd] ? Q : P))));
    G2[road.scd].PB(edge(
        road.fst,
        road.fst == N - 1
            ? 0
            : (is_zombie[road.fst] ? inf : (is_danger[road.fst] ? Q : P))));
  }

  dijkstra(0);

  cout << dist[N - 1] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> vector<T> &sort(vector<T> &a) {
  sort(all(a));
  return a;
}

const int inf = 3e18;
const ll linf = 3e18;
const double eps = 1e-9;

int N, M, K, S;
int P, Q;

struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};
typedef vector<edge> edges;

vi zombie;

vi G1[100010];
edges G2[100010];

int zombie_dist[100010];
bool is_zombie[100010];
bool is_danger[100010];
int dist[100010];

vpii roads;

void dijkstra(int s) {
  fill_n(dist, N + 1, inf);

  priority_queue<pii, vector<pii>, greater<pii>> pq;

  dist[s] = 0;

  pq.push(pii(0, s));

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

    int u = p.scd;

    if (dist[u] < p.fst)
      continue;

    for (edge e : G2[u]) {
      if (dist[e.to] >= dist[u] + e.cost) {
        dist[e.to] = dist[u] + e.cost;
        pq.push(pii(dist[e.to], e.to));
      }
    }
  }
}

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

  rep(i, K) {
    int C;
    cin >> C;
    --C;
    zombie.PB(C);
  }

  rep(i, M) {
    int A, B;
    cin >> A >> B;
    --A, --B;

    roads.push_back(pii(A, B));

    G1[A].PB(B);
    G1[B].PB(A);
  }

  // ??±????????????BFS??§?¨???¶
  fill_n(zombie_dist, N + 1, inf);

  queue<int> q;

  for (int num : zombie) {
    zombie_dist[num] = 0;
    is_zombie[num] = true;
    q.push(num);

    while (q.size()) {
      int u = q.front();
      q.pop();

      if (zombie_dist[u] >= S)
        continue;

      for (int v : G1[u]) {
        if (zombie_dist[v] >= zombie_dist[u] + 1) {
          zombie_dist[v] = zombie_dist[u] + 1;
          is_danger[v] = true;
          q.push(v);
        }
      }
    }
  }

  // ????????????????±?????????°??????????§????
  for (pii road : roads) {
    G2[road.fst].PB(edge(
        road.scd,
        road.scd == N - 1
            ? 0
            : (is_zombie[road.scd] ? inf : (is_danger[road.scd] ? Q : P))));
    G2[road.scd].PB(edge(
        road.fst,
        road.fst == N - 1
            ? 0
            : (is_zombie[road.fst] ? inf : (is_danger[road.fst] ? Q : P))));
  }

  dijkstra(0);

  cout << dist[N - 1] << endl;
}