Problem p00546 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

vector<PII> G[100010];
queue<PII> que; // que{Verticle, Distance}
priority_queue<PII, vector<PII>, greater<PII>> pq;
int main(void) {
  int N, M, K, S;
  int P, Q;
  cin >> N >> M >> K >> S;
  cin >> P >> Q;
  int isDanger[100010] = {};
  REP(i, K) {
    int C;
    cin >> C;
    C--;
    que.push({C, 0});
    isDanger[C] = 2;
  }
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back({0, b});
    G[b].push_back({0, a});
  }
  while (que.size()) {
    PII p = que.front();
    que.pop();
    if (p.second > S)
      continue;
    if (isDanger[p.first] == 1)
      continue;
    if (isDanger[p.first] == 0)
      isDanger[p.first] = 1;
    REP(i, G[p.first].size()) {
      que.push({G[p.first][i].second, p.second + 1});
    }
  }
  REP(i, N) {
    REP(j, G[i].size()) {
      if (isDanger[G[i][j].second])
        G[i][j].first = Q;
      else
        G[i][j].first = P;
    }
  }

  ll d[100010];
  fill(d, d + N, INF_LL);
  d[0] = 0;
  pq.push({0, 0});
  while (pq.size()) {
    PII p = pq.top();
    pq.pop();
    if (d[p.second] < p.first)
      continue;
    if (isDanger[p.second] == 2)
      continue;

    REP(i, G[p.second].size()) {
      int v = G[p.second][i].second;
      int e = G[p.second][i].first;

      if (d[v] > d[p.second] + e) {
        d[v] = d[p.second] + e;
        pq.push({d[v], v});
      }
    }
  }
  if (isDanger[N - 1])
    cout << d[N - 1] - Q << endl;
  else
    cout << d[N - 1] - P << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main(void) {
  int N, M, K, S;
  int P, Q;
  cin >> N >> M >> K >> S;
  cin >> P >> Q;
  vector<PII> G[100010];
  int isDanger[100010] = {};
  queue<PII> que; // que{Verticle, Distance}
  REP(i, K) {
    int C;
    cin >> C;
    C--;
    que.push({C, 0});
    isDanger[C] = 2;
  }
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back({0, b});
    G[b].push_back({0, a});
  }
  while (que.size()) {
    PII p = que.front();
    que.pop();
    if (p.second > S)
      continue;
    if (isDanger[p.first] == 0)
      isDanger[p.first] = 1;
    REP(i, G[p.first].size()) {
      que.push({G[p.first][i].second, p.second + 1});
    }
  }
  REP(i, N) {
    REP(j, G[i].size()) {
      if (isDanger[G[i][j].second])
        G[i][j].first = Q;
      else
        G[i][j].first = P;
    }
  }

  ll d[100010];
  fill(d, d + N, INF_LL);
  d[0] = 0;
  priority_queue<PII, vector<PII>, greater<PII>> pq;
  pq.push({0, 0});
  while (pq.size()) {
    PII p = pq.top();
    pq.pop();
    if (d[p.second] < p.first)
      continue;
    if (isDanger[p.second] == 2)
      continue;

    REP(i, G[p.second].size()) {
      int v = G[p.second][i].second;
      int e = G[p.second][i].first;

      if (d[v] > d[p.second] + e) {
        d[v] = d[p.second] + e;
        pq.push({d[v], v});
      }
    }
  }
  if (isDanger[N - 1])
    cout << d[N - 1] - Q << endl;
  else
    cout << d[N - 1] - P << endl;
}