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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 25;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const vi emp;

class Graph {
private:
  int V;
  vvp List;
  void bfs(vi a, int s, vb &b, vb used) {
    queue<P> que;
    for (int i = 0; i < a.size(); i++) {
      que.push(P(a[i], 0));
      b[a[i]] = used[a[i]] = 1;
    }
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      if (p.second == s)
        continue;
      for (int i = 0; i < List[p.first].size(); i++) {
        int v = List[p.first][i].first;
        if (!used[v]) {
          b[v] = used[v] = 1;
          if (p.second < s - 1)
            que.push(P(v, p.second + 1));
        }
      }
    }
  }
  ll DIJ(int s, int t) {
    priority_queue<pair<ll, ll>> que;
    vector<ll> d(V, INF);
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      pair<ll, ll> p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < -p.first)
        continue;
      for (int i = 0; i < List[v].size(); i++) {
        ll F = List[v][i].first, S = List[v][i].second;
        if (d[F] > d[v] + S) {
          d[F] = d[v] + S;
          que.push(pair<ll, ll>(-d[F], F));
        }
      }
    }
    return d[t];
  }

public:
  Graph(int v) {
    V = v;
    List = vvp(v);
  }
  void add_edge(int s, int t, int c) { List[s].push_back(P(t, c)); }
  ll solve(vp e, int s, int p, int q, vi a) {
    vb b(V), used(V);
    bfs(a, s, b, used);
    Graph sg(V);
    set<int> Set;
    for (int i = 0; i < a.size(); i++)
      Set.insert(a[i]);
    for (int i = 0; i < e.size(); i++) {
      vi st(2);
      st[0] = e[i].first;
      st[1] = e[i].second;
      if (Set.find(st[0]) != Set.end() || Set.find(st[1]) != Set.end())
        continue;
      for (int j = 0; j < 2; j++) {
        int S = st[j], T = st[(j + 1) % 2];
        if (T == 0 || T == V - 1)
          sg.add_edge(S, T, 0);
        else if (b[T])
          sg.add_edge(S, T, q);
        else
          sg.add_edge(S, T, p);
      }
    }
    return sg.DIJ(0, V - 1);
  }
};

int n, m, k, s, p, q;
vi a;
vp e;

int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m >> k >> s >> p >> q;
  a = vi(k);
  e = vp(m);
  Graph g(n);
  for (int i = 0; i < k; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < m; i++) {
    int S, T;
    cin >> S >> T;
    g.add_edge(S - 1, T - 1, 0);
    g.add_edge(T - 1, S - 1, 0);
    e[i] = P(S - 1, T - 1);
  }
  cout << g.solve(e, s, p, q, a) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 25;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const vi emp;

class Graph {
private:
  int V;
  vvp List;
  void bfs(vi a, int s, vb &b, vb used) {
    queue<P> que;
    for (int i = 0; i < a.size(); i++) {
      que.push(P(a[i], 0));
      b[a[i]] = used[a[i]] = 1;
    }
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      if (p.second == s)
        continue;
      for (int i = 0; i < List[p.first].size(); i++) {
        int v = List[p.first][i].first;
        if (!used[v]) {
          b[v] = used[v] = 1;
          if (p.second < s - 1)
            que.push(P(v, p.second + 1));
        }
      }
    }
  }
  ll DIJ(int s, int t) {
    priority_queue<pair<ll, ll>> que;
    vector<ll> d(V, INF);
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      pair<ll, ll> p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (int i = 0; i < List[v].size(); i++) {
        pair<ll, ll> q = List[v][i];
        ll F = q.first, S = q.second;
        if (d[F] > d[v] + S) {
          d[F] = d[v] + S;
          que.push(pair<ll, ll>(d[F], F));
        }
      }
    }
    return d[t];
  }

public:
  Graph(int v) {
    V = v;
    List = vvp(v);
  }
  void add_edge(int s, int t, int c) { List[s].push_back(P(t, c)); }
  ll solve(vp e, int s, int p, int q, vi a) {
    vb b(V), used(V);
    bfs(a, s, b, used);
    Graph sg(V);
    set<int> Set;
    for (int i = 0; i < a.size(); i++)
      Set.insert(a[i]);
    for (int i = 0; i < e.size(); i++) {
      vi st(2);
      st[0] = e[i].first;
      st[1] = e[i].second;
      if (Set.find(st[0]) != Set.end() || Set.find(st[1]) != Set.end())
        continue;
      for (int j = 0; j < 2; j++) {
        int S = st[j], T = st[(j + 1) % 2];
        if (T == 0 || T == V - 1)
          sg.add_edge(S, T, 0);
        else if (b[T])
          sg.add_edge(S, T, q);
        else
          sg.add_edge(S, T, p);
      }
    }
    /*		for(int i=0;i<V;i++) cout<<b[i]<<' ';
                    cout<<endl;*/
    return sg.DIJ(0, V - 1);
  }
};

int n, m, k, s, p, q;
vi a;
vp e;

int main() {
  cin >> n >> m >> k >> s >> p >> q;
  a = vi(k);
  e = vp(m);
  Graph g(n);
  for (int i = 0; i < k; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < m; i++) {
    int S, T;
    cin >> S >> T;
    g.add_edge(S - 1, T - 1, 0);
    g.add_edge(T - 1, S - 1, 0);
    e[i] = P(S - 1, T - 1);
  }
  cout << g.solve(e, s, p, q, a) << endl;
}