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

#include <iostream>
#include <limits>
#include <queue>
#include <vector>
using namespace std;
struct edge {
  int to, cost;
  edge(int to_, int cost_) : to(to_), cost(cost_){};
};
bool operator<(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }
template <class Type>
std::vector<Type> shortest_path(std::vector<std::vector<edge>> &G, int s) {
  std::priority_queue<edge> que;
  que.push(edge(s, 0));
  std::vector<Type> ret(G.size(), std::numeric_limits<Type>::max());
  ret[s] = 0;
  while (!que.empty()) {
    edge u = que.top();
    que.pop();
    for (edge e : G[u.to]) {
      if (ret[e.to] > ret[u.to] + e.cost) {
        ret[e.to] = ret[u.to] + e.cost;
        que.push(edge(e.to, -ret[e.to]));
      }
    }
  }
  return ret;
}
std::vector<vector<int>>
shortest_path_tree_all(std::vector<std::vector<edge>> &G, int s) {
  std::priority_queue<edge> que;
  que.push(edge(s, 0));
  std::vector<long long> ret(G.size(), std::numeric_limits<long long>::max());
  ret[s] = 0;
  vector<vector<int>> res(G.size());
  while (!que.empty()) {
    edge u = que.top();
    que.pop();
    for (edge e : G[u.to]) {
      if (ret[e.to] > ret[u.to] + e.cost) {
        ret[e.to] = ret[u.to] + e.cost;
        res[e.to].clear();
        que.push(edge(e.to, -ret[e.to]));
      }
      if (ret[e.to] == ret[u.to] + e.cost) {
        res[e.to].push_back(u.to);
      }
    }
  }
  return res;
}
int n, m, a, b, c;
int main() {
  cin >> n >> m;
  vector<vector<edge>> G(n);
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    a--, b--;
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }
  vector<vector<int>> d(n);
  for (int i = 0; i < n; i++)
    d[i] = shortest_path<int>(G, i);
  int x = -1;
  vector<vector<int>> p;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (d[i][j] > x)
        x = d[i][j], p.clear();
      if (d[i][j] == x)
        p.push_back({i, j});
    }
  }
  vector<vector<int>> gp(n);
  for (vector<int> i : p) {
    gp[i[0]].push_back(i[1]);
    gp[i[1]].push_back(i[0]);
  }
  vector<bool> ret(n, false);
  for (int t = 0; t < n; t++) {
    vector<vector<int>> f = shortest_path_tree_all(G, t);
    queue<int> que;
    vector<bool> vis(n, false);
    for (int t2 : gp[t]) {
      que.push(t2);
      vis[t2] = true;
      ret[t2] = true;
    }
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      for (int i : f[u]) {
        if (!vis[i]) {
          vis[i] = true;
          ret[i] = true;
          que.push(i);
        }
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!ret[i])
      cnt++;
  }
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    if (!ret[i])
      cout << i + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits>
#include <queue>
#include <vector>
using namespace std;
struct edge {
  int to, cost;
  edge(int to_, int cost_) : to(to_), cost(cost_){};
};
bool operator<(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }
template <class Type>
std::vector<Type> shortest_path(std::vector<std::vector<edge>> &G, int s) {
  std::priority_queue<edge> que;
  que.push(edge(s, 0));
  std::vector<Type> ret(G.size(), std::numeric_limits<Type>::max());
  ret[s] = 0;
  while (!que.empty()) {
    edge u = que.top();
    que.pop();
    for (edge e : G[u.to]) {
      if (ret[e.to] > ret[u.to] + e.cost) {
        ret[e.to] = ret[u.to] + e.cost;
        que.push(edge(e.to, -ret[e.to]));
      }
    }
  }
  return ret;
}
std::vector<vector<int>>
shortest_path_tree_all(std::vector<std::vector<edge>> &G, int s) {
  std::priority_queue<edge> que;
  que.push(edge(s, 0));
  std::vector<long long> ret(G.size(), std::numeric_limits<long long>::max());
  ret[s] = 0;
  vector<vector<int>> res(G.size());
  while (!que.empty()) {
    edge u = que.top();
    que.pop();
    for (edge e : G[u.to]) {
      if (ret[e.to] > ret[u.to] + e.cost) {
        ret[e.to] = ret[u.to] + e.cost;
        res[e.to].clear();
        que.push(edge(e.to, -ret[e.to]));
      }
      if (ret[e.to] == ret[u.to] + e.cost) {
        res[e.to].push_back(u.to);
      }
    }
  }
  return res;
}
int n, m, a, b, c;
int main() {
  cin >> n >> m;
  vector<vector<edge>> G(n);
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    a--, b--;
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }
  vector<vector<int>> d(n);
  for (int i = 0; i < n; i++)
    d[i] = shortest_path<int>(G, i);
  int x = -1;
  vector<vector<int>> p;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (d[i][j] > x)
        x = d[i][j], p.clear();
      if (d[i][j] == x)
        p.push_back({i, j});
    }
  }
  vector<bool> ret(n, false);
  for (vector<int> g : p) {
    vector<vector<int>> f = shortest_path_tree_all(G, g[0]);
    queue<int> que;
    que.push(g[1]);
    vector<bool> vis(n, false);
    vis[g[1]] = true;
    ret[g[1]] = true;
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      for (int i : f[u]) {
        if (!vis[i]) {
          vis[i] = true;
          ret[i] = true;
          que.push(i);
        }
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!ret[i])
      cnt++;
  }
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    if (!ret[i])
      cout << i + 1 << endl;
  }
  return 0;
}