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

#include "climits"
#include "functional"
#include "iostream"
#include "list"
#include "queue"
using namespace std;

const int BORDER = 1000000;

int N, M, K, S;
int price[2];
bool status[100001];
bool dead[100001];
int box;
int s, e;
bool flag[100001];
list<int> edge[100001];
long long int dis[100001];
long long int NOW;

queue<int> Q;
priority_queue<long long int, vector<long long int>, greater<long long int>>
    town;

int main() {
  cin >> N >> M >> K >> S;
  cin >> price[0] >> price[1];
  for (int i = 1; i <= N; i++) {
    flag[i] = true;
    dis[i] = INT_MAX;
  }
  for (int i = 0; i < K; i++) {
    cin >> box;
    dead[box] = true;
    edge[0].push_back(box);
    flag[box] = false;
  }
  for (int i = 0; i < M; i++) {
    cin >> s >> e;
    edge[s].push_back(e);
    edge[e].push_back(s);
  }
  Q.push(0);
  while (!Q.empty()) {
    NOW = Q.front();
    for (auto i = edge[NOW].begin(); i != edge[NOW].end(); ++i) {
      if (dis[NOW] + 1 < dis[*i]) {
        Q.push(*i);
        dis[*i] = dis[NOW] + 1;
        if (dis[*i] <= S + 1)
          status[*i] = true;
      }
    }
    Q.pop();
  }
  for (int i = 2; i <= N; i++)
    dis[i] = LLONG_MAX;
  dis[1] = 0;
  town.push(1);
  while (!town.empty()) {
    NOW = town.top() % BORDER;
    for (auto i = edge[NOW].begin(); i != edge[NOW].end(); ++i) {
      if (!dead[*i]) {
        if (dis[NOW] + price[status[*i]] < dis[*i]) {
          dis[*i] = dis[NOW] + price[status[*i]];
          town.push(dis[*i] * BORDER + *i);
        }
      }
    }
    town.pop();
  }
  cout << dis[N] - price[status[N]] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include "climits"
#include "iostream"
#include "list"
#include "queue"
using namespace std;

int N, M, K, S;
int status[131073];
int price[2];
int dead;
long long int now;
int box, bag;
list<int> to[100001];
list<int>::iterator itr;
long long int dis[131073];
bool flag[131073];
queue<int> Q;
priority_queue<int> town;

long long int RMQ(int left, int right) {
  if (left == right) {
    if (flag[left])
      return dis[left];
    else
      return LLONG_MAX;
  }
  return min(RMQ(left, (left + right) / 2), RMQ((left + right) / 2 + 1, right));
}

int main() {
  cin >> N >> M >> K >> S;
  cin >> price[0] >> price[1];
  for (int i = 1; i <= N; i++) {
    flag[i] = true;
  }
  for (int i = 0; i < K; i++) {
    cin >> box;
    status[box] = -1;
    flag[box] = false;
  }
  for (int i = 0; i < M; i++) {
    cin >> box >> bag;
    to[box].push_back(bag);
    to[bag].push_back(box);
  }
  for (int i = 2; i < N; i++) {
    if (status[i] == 0) {
      Q.push(i);
      for (int j = 1; j <= N; j++) {
        dis[j] = INT_MAX;
      }
      dis[i] = 0;
      while (!Q.empty()) {
        for (itr = to[Q.front()].begin(); itr != to[Q.front()].end(); ++itr) {
          if (dis[Q.front()] + 1 < dis[(*itr)]) {
            dis[(*itr)] = dis[Q.front()] + 1;
            if (dis[(*itr)] > S) {
              while (!Q.empty()) {
                Q.pop();
              }
              break;
            }
            if (status[(*itr)] == -1) {
              status[i] = 1;
              while (!Q.empty()) {
                Q.pop();
              }
              break;
            }
            Q.push((*itr));
          }
        }
        if (Q.empty())
          break;
        Q.pop();
      }
    }
  }
  dis[1] = 1;
  for (int i = 2; i <= N; i++) {
    dis[i] = LLONG_MAX;
  }
  while (1) {
    now = RMQ(1, N) % 200000;
    flag[now] = false;
    if (now == N)
      break;
    for (itr = to[now].begin(); itr != to[now].end(); ++itr) {
      if (flag[(*itr)]) {
        if (dis[now] / 200000 + price[status[(*itr)]] < dis[(*itr)] / 200000) {
          dis[(*itr)] =
              (dis[now] / 200000 + price[status[(*itr)]]) * 200000 + (*itr);
        }
      }
    }
  }
  cout << dis[N] / 200000 - price[status[N]] << "\\n";
  return 0;
}