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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <vector>
using namespace std;

int n, m, k, s, p, q;
vector<int> v[100001];
int da[100001];
typedef pair<long long int, int> P;
long long int di[100001];
long long int INF = 1e17;

void bfs() {
  queue<P> q;
  q.push(P(0, -1));
  da[0] = -1;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    for (int i = 0; i < v[p.first].size(); ++i) {
      if (p.second + 1 < da[v[p.first][i]]) {
        da[v[p.first][i]] = p.second + 1;
        if (p.second + 1 < s)
          q.push(P(v[p.first][i], p.second + 1));
      }
    }
  }
}

int d(int x) {
  if (da[x] == 0)
    return INF;
  else if (da[x] <= s)
    return q;
  else
    return p;
}

void dijkstra() {
  fill(di, di + n + 1, INF);
  di[1] = 0;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, 1));

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    long long int c = p.first;
    int ve = p.second;
    if (c > di[ve])
      continue;
    for (int i = 0; i < v[ve].size(); ++i) {
      if (di[v[ve][i]] > di[ve] + d(v[ve][i])) {
        di[v[ve][i]] = di[ve] + d(v[ve][i]);
        q.push(P(di[v[ve][i]], v[ve][i]));
      }
    }
  }
}

int main() {
  scanf("%d %d %d %d %d %d", &n, &m, &k, &s, &p, &q);
  fill(da, da + n + 1, 100100100);
  for (int i = 0; i < k; ++i) {
    int t;
    scanf("%d", &t);
    v[0].push_back(t);
    v[t].push_back(0);
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    v[a].push_back(b);
    v[b].push_back(a);
  }

  bfs();
  /*
          for(int i=1; i<n+1; ++i){
                  printf("%d\\n", da[i]);
          }
  */
  dijkstra();
  /*
          for(int i=0; i<n; ++i){
                  printf("%lld\\n", di[i+1]);
          }
  */
  printf("%lld\\n", di[n] - d(n));
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

int n, m, k, s;
int p, q;
vector<int> c;
vector<int> e[200000];

int d[100000];
long long int cost[100000];
int inf = 1e8;
long long int INF = 1e12;
bool used[100000];

typedef pair<int, int> P;
queue<P> que;

void danger() {
  fill(d, d + n, inf);
  for (int i = 0; i < k; i++) {
    d[c[i]] = 0;
    que.push(P(c[i], 0));
    while (que.size()) {
      P t = que.front();
      que.pop();
      for (int i = 0; i < e[t.first].size(); i++) {
        if (t.second + 1 > s)
          break;
        if (d[e[t.first][i]] == 0)
          continue;
        if (d[e[t.first][i]] > t.second + 1) {
          d[e[t.first][i]] = t.second + 1;
          que.push(P(e[t.first][i], t.second + 1));
        }
        if (t.second < s - 1)
          que.push(P(e[t.first][i], t.second + 1));
      }
    }
  }
}

void dijkstra() {
  priority_queue<P, vector<P>, greater<P>> pq;
  fill(cost, cost + n, INF);
  cost[0] = 0;
  pq.push(P(0, 0));

  while (!pq.empty()) {
    P pa = pq.top();
    pq.pop();
    int v = pa.second;
    if (cost[v] < pa.first)
      continue;
    for (int i = 0; i < e[v].size(); i++) {
      long long int co = INF;
      if (d[e[v][i]] != 0)
        co = q;
      if (d[e[v][i]] == inf)
        co = p;
      if (cost[e[v][i]] > cost[v] + co) {
        cost[e[v][i]] = cost[v] + co;
        pq.push(P(cost[e[v][i]], e[v][i]));
      }
    }
  }
}

int main() {
  scanf("%d %d %d %d %d %d", &n, &m, &k, &s, &p, &q);
  for (int i = 0; i < k; i++) {
    int t;
    scanf("%d", &t);
    c.push_back(t - 1);
  }
  for (int i = 0; i < m; i++) {
    int t1, t2;
    scanf("%d %d", &t1, &t2);
    e[t1 - 1].push_back(t2 - 1);
    e[t2 - 1].push_back(t1 - 1);
  }

  danger();

  dijkstra();
  /*
          printf("\\n");
          for(int i=0; i<n; i++){
                  printf("%d\\n", d[i]);
          }
          printf("\\n");
  */
  int x = q;
  if (d[n - 1] == inf)
    x = p;
  printf("%lld\\n", cost[n - 1] - x);
  return 0;
}