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

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

typedef pair<int, int> P;

#define rep(i, x) for (int i = 0; i < x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define pb push_back
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define sor(vec) sort(vec.begin(), vec.end())
#define rev(vec) reverse(vec.begin(), vec.end())
#define pque(x) priority_queue<x>
#define rpque(x) priority_queue<x, vector<x>, greater<x>>
#define fr first
#define sc second

const int INF = 1000000000;

int main() {
  int s, r, u, v, w, a, b, q;
  static vector<P> G[100010];

  scanf("%d%d", &s, &r);
  rep(i, r) {
    scanf("%d%d%d", &u, &v, &w);
    G[u].pb(P(v, w));
    G[v].pb(P(u, w));
  }
  scanf("%d%d%d", &a, &b, &q);

  static bool used[100010];
  static int d[100010];
  rpque(P) que;
  static vector<int> prev[100010];

  rep(i, 100010) used[i] = false;
  rep(i, 100010) d[i] = INF;
  d[a] = 0;
  que.push(P(0, a));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (!used[p.sc]) {
      rep(i, G[p.sc].size()) {
        P ed = G[p.sc][i];
        if (d[ed.fr] > d[p.sc] + ed.sc) {
          d[ed.fr] = d[p.sc] + ed.sc;
          prev[ed.fr].clear();
          prev[ed.fr].pb(p.sc);
          que.push(P(d[ed.fr], ed.fr));
        } else if (d[ed.fr] == d[p.sc] + ed.sc) {
          prev[ed.fr].pb(p.sc);
        }
      }
      used[p.sc] = true;
    }
  }

  /*rep1(i,s){
          printf("%d:",i);
          rep(j,prev[i].size()){
                  printf("%d ",prev[i][j]);
          }
          printf("\\n");
  }*/

  rep(i, 100010) used[i] = false;
  queue<int> que0;
  static vector<int> nG[100010];
  que0.push(b);
  used[b] = true;

  while (!que0.empty()) {
    int x = que0.front();
    que0.pop();
    rep(i, prev[x].size()) {
      int p = prev[x][i];
      nG[p].pb(x);
      if (!used[p]) {
        que0.push(p);
        used[p] = true;
      }
    }
  }

  /*rep1(i,s){
          printf("%d:",i);
          rep(j,nG[i].size()){
                  printf("%d ",nG[i][j]);
          }
          printf("\\n");
  }*/

  static bool used0[100010];
  rep(i, 100010) used0[i] = used[i];

  rep(ppp, q) {
    int c, e;
    scanf("%d%d", &c, &e);

    if (!used0[e]) {
      printf("No\\n");
      continue;
    }

    rep(i, 100010) used[i] = false;
    stack<int> sta;
    sta.push(c);
    used[c] = true;

    bool ret = false;
    while (!sta.empty()) {
      int x = sta.top();
      sta.pop();
      if (x == e) {
        ret = true;
        break;
      }
      if (d[x] < d[e]) {
        rrep(i, nG[x].size()) {
          int nex = nG[x][i];
          if (!used[nex]) {
            sta.push(nex);
            used[nex] = true;
          }
        }
      }
    }

    if (ret)
      printf("Yes\\n");
    else
      printf("No\\n");
  }
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> P;

#define rep(i, x) for (int i = 0; i < x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define pb push_back
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define sor(vec) sort(vec.begin(), vec.end())
#define rev(vec) reverse(vec.begin(), vec.end())
#define pque(x) priority_queue<x>
#define rpque(x) priority_queue<x, vector<x>, greater<x>>
#define fr first
#define sc second

const int INF = 1000000000;

int main() {
  int s, r, u, v, w, a, b, q;
  static vector<P> G[100010];

  scanf("%d%d", &s, &r);
  rep(i, r) {
    scanf("%d%d%d", &u, &v, &w);
    G[u].pb(P(v, w));
    G[v].pb(P(u, w));
  }
  scanf("%d%d%d", &a, &b, &q);

  static bool used[100010];
  static int d[100010];
  rpque(P) que;
  static vector<int> prev[100010];

  rep(i, 100010) used[i] = false;
  rep(i, 100010) d[i] = INF;
  d[a] = 0;
  que.push(P(0, a));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (!used[p.sc]) {
      rep(i, G[p.sc].size()) {
        P ed = G[p.sc][i];
        if (d[ed.fr] > d[p.sc] + ed.sc) {
          d[ed.fr] = d[p.sc] + ed.sc;
          prev[ed.fr].clear();
          prev[ed.fr].pb(p.sc);
          que.push(P(d[ed.fr], ed.fr));
        } else if (d[ed.fr] == d[p.sc] + ed.sc) {
          prev[ed.fr].pb(p.sc);
        }
      }
      used[p.sc] = true;
    }
  }

  /*rep1(i,s){
          printf("%d:",i);
          rep(j,prev[i].size()){
                  printf("%d ",prev[i][j]);
          }
          printf("\\n");
  }*/

  rep(i, 100010) used[i] = false;
  queue<int> que0;
  static vector<int> nG[100010];
  que0.push(b);
  used[b] = true;

  while (!que0.empty()) {
    int x = que0.front();
    que0.pop();
    rep(i, prev[x].size()) {
      int p = prev[x][i];
      nG[p].pb(x);
      if (!used[p]) {
        que0.push(p);
        used[p] = true;
      }
    }
  }

  /*rep1(i,s){
          printf("%d:",i);
          rep(j,nG[i].size()){
                  printf("%d ",nG[i][j]);
          }
          printf("\\n");
  }*/

  static bool used0[100010];
  rep(i, 100010) used0[i] = used[i];

  rep(ppp, q) {
    int c, e;
    scanf("%d%d", &c, &e);

    if (!used0[e]) {
      printf("No\\n");
      continue;
    }

    rep(i, 100010) used[i] = false;
    stack<int> sta;
    sta.push(c);
    used[c] = true;

    bool ret = false;
    while (!sta.empty()) {
      int x = sta.top();
      sta.pop();
      if (x == e) {
        ret = true;
        break;
      }
      rrep(i, nG[x].size()) {
        int nex = nG[x][i];
        if (!used[nex]) {
          sta.push(nex);
          used[nex] = true;
        }
      }
    }

    if (ret)
      printf("Yes\\n");
    else
      printf("No\\n");
  }
}