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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define INF 1500001
int n, r, d[1500][1500], qt[1500], ans = 0;
vector<P> e[1500];
vector<int> p[1500][1500];
int dfs(int a, int b) {
  if (b == a)
    return 0;
  for (int i = 0; i < p[a][b].size(); i++) {
    if (qt[p[a][b][i]] == 0)
      ans++;
    qt[p[a][b][i]] = 1;
    dfs(a, p[a][b][i]);
  }
  return 0;
}
int main(void) {
  cin >> n >> r;
  for (int i = 0; i < r; i++) {
    int s, t, c;
    cin >> s >> t >> c;
    s--;
    t--;
    e[s].push_back(P(t, c));
    e[t].push_back(P(s, c));
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      d[i][j] = INF;
    }
  }
  int mc = 0;
  for (int i = 0; i < n; i++) {
    priority_queue<P, vector<P>, greater<P>> k;
    d[i][i] = 0;
    k.push(P(0, i));
    while (!k.empty()) {
      int v = k.top().second, co = k.top().first;
      k.pop();
      if (d[i][v] != co)
        continue;
      for (int j = 0; j < e[v].size(); j++) {
        if (d[i][e[v][j].first] == d[i][v] + e[v][j].second)
          p[i][e[v][j].first].push_back(v);
        else if (d[i][e[v][j].first] > d[i][v] + e[v][j].second) {
          p[i][e[v][j].first].clear();
          p[i][e[v][j].first].push_back(v);
          d[i][e[v][j].first] = d[i][v] + e[v][j].second;
          k.push(P(d[i][e[v][j].first], e[v][j].first));
        }
      }
    }
    for (int j = 0; j < n; j++) {
      if (mc < d[i][j]) {
        mc = d[i][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (d[i][j] == mc) {
        dfs(i, j);
      }
    }
  }
  cout << n - ans << endl;
  for (int i = 0; i < n; i++) {
    if (qt[i] == 0)
      cout << i + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define INF 1500001
int n, r, d[1500][1500], qt[1500];
vector<P> e[1500];
int main(void) {
  cin >> n >> r;
  for (int i = 0; i < r; i++) {
    int s, t, c;
    cin >> s >> t >> c;
    s--;
    t--;
    e[s].push_back(P(t, c));
    e[t].push_back(P(s, c));
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      d[i][j] = INF;
    }
  }
  P mv(0, 0);
  int mc = 0;
  for (int i = 0; i < n; i++) {
    priority_queue<P, vector<P>, greater<P>> k;
    d[i][i] = 0;
    k.push(P(0, i));
    while (!k.empty()) {
      int v = k.top().second, co = k.top().first;
      k.pop();
      if (d[i][v] != co)
        continue;
      for (int j = 0; j < e[v].size(); j++) {
        if (d[i][e[v][j].first] > d[i][v] + e[v][j].second) {
          d[i][e[v][j].first] = d[i][v] + e[v][j].second;
          k.push(P(d[i][e[v][j].first], e[v][j].first));
        }
      }
    }
    for (int j = 0; j < n; j++) {
      if (mc < d[i][j]) {
        mc = d[i][j];
        mv = P(i, j);
      }
    }
  }
  vector<P> si;
  si.push_back(mv);
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (d[i][j] == mc && mv != P(i, j))
        si.push_back(P(i, j));
    }
  }

  int ans = 0;
  for (int i = 0; i < si.size(); i++) {
    int s = si[i].first, t = si[i].second;
    for (int j = 0; j < n; j++) {
      if (d[s][j] + d[j][t] == mc && qt[j] == 0) {
        qt[j] = 1;
        ans++;
      }
    }
  }
  cout << n - ans << endl;
  for (int i = 0; i < n; i++) {
    if (qt[i] == 0)
      cout << i + 1 << endl;
  }
}