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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 4;

template <class T> class EdgeBase {
public:
  int to;
  T cost;
  EdgeBase(){};
  EdgeBase(int to0, T cost0) {
    to = to0;
    cost = cost0;
  }
};
typedef EdgeBase<int> Edge;

template <class T>
void shortestPath(const vector<vector<EdgeBase<T>>> &edges, int start,
                  vector<T> &dist) {
  dist.assign(edges.size(), INF);
  dist[start] = 0;
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
  q.push(make_pair(0, start));

  while (!q.empty()) {
    pair<T, int> p = q.top();
    q.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    for (unsigned i = 0; i < edges[v].size(); ++i) {
      EdgeBase<T> e = edges[v][i];
      if (dist[v] + e.cost < dist[e.to]) {
        dist[e.to] = dist[v] + e.cost;
        q.push(make_pair(dist[e.to], e.to));
      }
    }
  }
}

template <class T>
void shortestPath(const vector<vector<EdgeBase<T>>> &edges,
                  vector<vector<int>> &dist) {
  dist.resize(edges.size());
  for (unsigned i = 0; i < edges.size(); ++i)
    shortestPath(edges, i, dist[i]);
}

int main() {
  int n, r;
  cin >> n >> r;
  vector<vector<Edge>> edges(n);
  for (int i = 0; i < r; ++i) {
    int s, t, d;
    cin >> s >> t >> d;
    --s;
    --t;
    edges[s].push_back(Edge(t, d));
    edges[t].push_back(Edge(s, d));
  }

  vector<vector<int>> dist;
  shortestPath(edges, dist);
  int maxDist = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (dist[i][j] < INF)
        maxDist = max(maxDist, dist[i][j]);
    }
  }

  vector<bool> check(n, true);
  for (int i = 0; i < n; ++i) {
    queue<int> q;
    vector<bool> used(n, false);
    for (int j = 0; j < n; ++j) {
      if (dist[i][j] == maxDist) {
        q.push(j);
        check[j] = false;
        used[j] = true;
      }
    }
    while (!q.empty()) {
      int a = q.front();
      q.pop();
      for (const Edge &e : edges[a]) {
        if (!used[e.to] && dist[i][e.to] + e.cost == dist[i][a]) {
          q.push(e.to);
          check[e.to] = false;
          used[e.to] = true;
        }
      }
    }
  }

  vector<int> ans;
  for (int i = 0; i < n; ++i) {
    if (check[i])
      ans.push_back(i + 1);
  }
  cout << ans.size() << endl;
  for (int a : ans)
    cout << a << endl;

  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 4;

template <class T> class EdgeBase {
public:
  int to;
  T cost;
  EdgeBase(){};
  EdgeBase(int to0, T cost0) {
    to = to0;
    cost = cost0;
  }
};
typedef EdgeBase<int> Edge;

template <class T>
void shortestPath(const vector<vector<EdgeBase<T>>> &edges, int start,
                  vector<T> &dist) {
  dist.assign(edges.size(), INF);
  dist[start] = 0;
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
  q.push(make_pair(0, start));

  while (!q.empty()) {
    pair<T, int> p = q.top();
    q.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    for (unsigned i = 0; i < edges[v].size(); ++i) {
      EdgeBase<T> e = edges[v][i];
      if (dist[v] + e.cost < dist[e.to]) {
        dist[e.to] = dist[v] + e.cost;
        q.push(make_pair(dist[e.to], e.to));
      }
    }
  }
}

template <class T>
void shortestPath(const vector<vector<EdgeBase<T>>> &edges,
                  vector<vector<int>> &dist) {
  dist.resize(edges.size());
  for (unsigned i = 0; i < edges.size(); ++i)
    shortestPath(edges, i, dist[i]);
}

int main() {
  int n, r;
  cin >> n >> r;
  vector<vector<Edge>> edges(n);
  for (int i = 0; i < r; ++i) {
    int s, t, d;
    cin >> s >> t >> d;
    --s;
    --t;
    edges[s].push_back(Edge(t, d));
    edges[t].push_back(Edge(s, d));
  }

  vector<vector<int>> dist;
  shortestPath(edges, dist);
  int maxDist = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (dist[i][j] < INF)
        maxDist = max(maxDist, dist[i][j]);
    }
  }

  vector<bool> check(n, true);
  for (int i = 0; i < n; ++i) {
    queue<int> q;
    for (int j = 0; j < n; ++j) {
      if (dist[i][j] == maxDist) {
        q.push(j);
        check[j] = false;
      }
    }
    while (!q.empty()) {
      int a = q.front();
      q.pop();
      for (const Edge &e : edges[a]) {
        if (dist[i][e.to] + e.cost == dist[i][a]) {
          q.push(e.to);
          check[e.to] = false;
        }
      }
    }
  }

  vector<int> ans;
  for (int i = 0; i < n; ++i) {
    if (check[i])
      ans.push_back(i + 1);
  }
  cout << ans.size() << endl;
  for (int a : ans)
    cout << a << endl;

  return 0;
}