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

#include <bits/stdc++.h>
using namespace std;
int dist[2000];
set<int> p[2000];
vector<pair<int, int>> vec[2000];
int n, m;
bool used2[2000];
vector<int> R[2000][2000];
int dst[2000][2000];
vector<int> shor[2000];
void dijkstra(int s) {
  for (int i = 0; i < 2000; i++) {
    dist[i] = 999999999;
    p[i].clear();
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      Q;
  Q.push(make_pair(0, s));
  dist[s] = 0;
  while (!Q.empty()) {
    int a1 = Q.top().first, a2 = Q.top().second;
    Q.pop();
    for (int i = 0; i < (int)vec[a2].size(); i++) {
      int to = vec[a2][i].first, cost = vec[a2][i].second;
      if (dist[to] > a1 + cost) {
        p[to].clear();
        p[to].insert(a2);
        dist[to] = a1 + cost;
        Q.push(make_pair(dist[to], to));
      }
      if (dist[to] == a1 + cost && p[to].count(a2) == 0) {
        p[to].insert(a2);
      }
    }
  }
  for (int i = 0; i < 2000; i++) {
    dst[s][i] = dist[i];
    for (auto a3 : p[i]) {
      R[s][i].push_back(a3);
    }
  }
}
void solve(int s) {
  queue<int> Q;
  bool used[2000];
  for (int i = 0; i < 2000; i++)
    used[i] = false;
  for (int i = 0; i < (int)shor[s].size(); i++) {
    Q.push(shor[s][i]);
    used[shor[s][i]] = true;
  }
  while (!Q.empty()) {
    int a1 = Q.front();
    Q.pop();

    for (int i = 0; i < R[s][a1].size(); i++) {
      int a2 = R[s][a1][i];
      if (used[a2] == false) {
        used[a2] = true;
        Q.push(a2);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (used[i] == true)
      used2[i] = true;
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    vec[a].push_back(make_pair(b, c));
    vec[b].push_back(make_pair(a, c));
  }
  for (int i = 1; i <= n; i++)
    dijkstra(i);
  int maxn = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      maxn = max(maxn, dst[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (dst[i][j] == maxn)
        shor[i].push_back(j);
    }
  }
  for (int i = 1; i <= n; i++)
    solve(i);
  int num = 0;
  for (int i = 1; i <= n; i++) {
    if (used2[i] == false)
      num++;
  }
  cout << num << endl;
  for (int i = 1; i <= n; i++) {
    if (used2[i] == false)
      cout << i << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dist[2000];
set<int> p[2000];
vector<pair<int, int>> vec[2000];
int n, m;
bool used2[2000];
set<int> R[2000][2000];
int dst[2000][2000];
vector<int> shor[2000];
void dijkstra(int s) {
  for (int i = 0; i < 2000; i++) {
    dist[i] = 999999999;
    p[i].clear();
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      Q;
  Q.push(make_pair(0, s));
  dist[s] = 0;
  while (!Q.empty()) {
    int a1 = Q.top().first, a2 = Q.top().second;
    Q.pop();
    for (int i = 0; i < (int)vec[a2].size(); i++) {
      int to = vec[a2][i].first, cost = vec[a2][i].second;
      if (dist[to] > a1 + cost) {
        p[to].clear();
        p[to].insert(a2);
        dist[to] = a1 + cost;
        Q.push(make_pair(dist[to], to));
      }
      if (dist[to] == a1 + cost && p[to].count(a2) == 0) {
        p[to].insert(a2);
      }
    }
  }
  for (int i = 0; i < 2000; i++) {
    dst[s][i] = dist[i];
    R[s][i] = p[i];
  }
}
void solve(int s) {
  queue<int> Q;
  bool used[2000];
  for (int i = 0; i < 2000; i++)
    used[i] = false;
  for (int i = 0; i < (int)shor[s].size(); i++) {
    Q.push(shor[s][i]);
    used[shor[s][i]] = true;
  }
  while (!Q.empty()) {
    int a1 = Q.front();
    Q.pop();

    for (auto a2 : R[s][a1]) {
      if (used[a2] == false) {
        used[a2] = true;
        Q.push(a2);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (used[i] == true)
      used2[i] = true;
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    vec[a].push_back(make_pair(b, c));
    vec[b].push_back(make_pair(a, c));
  }
  for (int i = 1; i <= n; i++)
    dijkstra(i);
  int maxn = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      maxn = max(maxn, dst[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (dst[i][j] == maxn)
        shor[i].push_back(j);
    }
  }
  for (int i = 1; i <= n; i++)
    solve(i);
  int num = 0;
  for (int i = 1; i <= n; i++) {
    if (used2[i] == false)
      num++;
  }
  cout << num << endl;
  for (int i = 1; i <= n; i++) {
    if (used2[i] == false)
      cout << i << endl;
  }
  return 0;
}