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

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef unsigned long long int ull;
#define pb push_back
#define INF 1000000000
#define fi first
#define sec second
int dist1[100100], dist2[100100];
int c[40100], d[40100];
bitset<1024> dp[100100];
struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};
vector<edge> g[100100];
int comp(int x, int y) { return dist1[x] < dist1[y]; }
void dijkstra(int s, int t) {
  for (int i = 0; i < 100100; i++)
    dist1[i] = INF, dist2[i] = INF;
  dist1[s] = 0, dist2[t] = 0;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, s));
  while (!q.empty()) {
    P a = q.top();
    q.pop();
    int v = a.sec;
    for (int i = 0; i < g[v].size(); i++) {
      edge e = g[v][i];
      if (dist1[e.to] > dist1[v] + e.cost) {
        dist1[e.to] = dist1[v] + e.cost;
        q.push(P(dist1[e.to], e.to));
      }
    }
  }
  q.push(P(0, t));
  while (!q.empty()) {
    P a = q.top();
    q.pop();
    int v = a.sec;
    for (int i = 0; i < g[v].size(); i++) {
      edge e = g[v][i];
      if (dist2[e.to] > dist2[v] + e.cost) {
        dist2[e.to] = dist2[v] + e.cost;
        q.push(P(dist2[e.to], e.to));
      }
    }
  }
  return;
}
int S, R, Q;
int a, b;
int main() {
  scanf("%d %d", &S, &R);
  for (int i = 0; i < R; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    u--;
    v--;
    g[u].pb(edge(v, w));
    g[v].pb(edge(u, w));
  }
  scanf("%d %d %d", &a, &b, &Q);
  a--;
  b--;
  dijkstra(a, b);
  /*for(int i=0;i<S;i++)
  {
      cout << dist1[i] << ' ' << dist2[i] << endl;
  }*/
  vector<int> tp;
  for (int i = 0; i < S; i++)
    tp.pb(i);
  sort(tp.begin(), tp.end(), comp);
  // for(int i=0;i<S;i++)cout << tp[i] << ' ';
  for (int i = 0; i < Q; i += 1024) {
    for (int j = 0; j < 100100; j++)
      dp[j] = bitset<1024>(0);
    for (int j = 0; j < min(1024, Q - i); j++) {
      scanf("%d %d", &c[j], &d[j]);
      c[j]--;
      d[j]--;
      dp[c[j]].set(j);
    }
    for (int j = 0; j < S; j++) {
      int f = tp[j];
      if (dist1[f] + dist2[f] != dist1[b])
        continue;
      for (int k = 0; k < g[f].size(); k++) {
        edge e = g[f][k];
        if (dist1[e.to] + dist2[e.to] != dist1[b])
          continue;
        if (dist1[e.to] != dist1[f] + e.cost)
          continue;
        dp[e.to] |= dp[f];
      }
    }
    for (int j = 0; j < min(1024, Q - i); j++) {
      if (dp[d[j]][j])
        printf("Yes\\n");
      else
        printf("No\\n");
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef unsigned long long int ull;
#define pb push_back
#define INF 1000000000
#define fi first
#define sec second
int dist1[100100], dist2[100100];
int c[40100], d[40100];
bitset<40000> dp[100100];
struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};
vector<edge> g[100100];
int comp(int x, int y) { return dist1[x] < dist1[y]; }
void dijkstra(int s, int t) {
  for (int i = 0; i < 100100; i++)
    dist1[i] = INF, dist2[i] = INF;
  dist1[s] = 0, dist2[t] = 0;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, s));
  while (!q.empty()) {
    P a = q.top();
    q.pop();
    int v = a.sec;
    for (int i = 0; i < g[v].size(); i++) {
      edge e = g[v][i];
      if (dist1[e.to] > dist1[v] + e.cost) {
        dist1[e.to] = dist1[v] + e.cost;
        q.push(P(dist1[e.to], e.to));
      }
    }
  }
  q.push(P(0, t));
  while (!q.empty()) {
    P a = q.top();
    q.pop();
    int v = a.sec;
    for (int i = 0; i < g[v].size(); i++) {
      edge e = g[v][i];
      if (dist2[e.to] > dist2[v] + e.cost) {
        dist2[e.to] = dist2[v] + e.cost;
        q.push(P(dist2[e.to], e.to));
      }
    }
  }
  return;
}
int S, R, Q;
int a, b;
int main() {
  scanf("%d %d", &S, &R);
  for (int i = 0; i < R; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    u--;
    v--;
    g[u].pb(edge(v, w));
    g[v].pb(edge(u, w));
  }
  scanf("%d %d %d", &a, &b, &Q);
  a--;
  b--;
  dijkstra(a, b);
  /*for(int i=0;i<S;i++)
  {
      cout << dist1[i] << ' ' << dist2[i] << endl;
  }*/
  vector<int> tp;
  for (int i = 0; i < S; i++)
    tp.pb(i);
  sort(tp.begin(), tp.end(), comp);
  // for(int i=0;i<S;i++)cout << tp[i] << ' ';
  for (int i = 0; i < Q; i++) {
    scanf("%d %d", &c[i], &d[i]);
    c[i]--;
    d[i]--;
    dp[c[i]].set(i);
  }
  for (int j = 0; j < S; j++) {
    int f = tp[j];
    if (dist1[f] + dist2[f] != dist1[b])
      continue;
    for (int k = 0; k < g[f].size(); k++) {
      edge e = g[f][k];
      if (dist1[e.to] + dist2[e.to] != dist1[b])
        continue;
      if (dist1[e.to] != dist1[f] + e.cost)
        continue;
      dp[e.to] |= dp[f];
    }
  }
  for (int i = 0; i < Q; i++) {
    if (dp[d[i]][i])
      printf("Yes\\n");
    else
      printf("No\\n");
  }
  return 0;
}