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

/*input
7 11
1 2 2
1 3 1
2 4 2
2 5 2
2 6 1
3 4 1
3 5 1
3 6 1
4 7 2
5 7 2
6 7 1
*/
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\\n'
#define fi first
#define se second
#define mp make_pair
#define int long long
#define N 1505
#define bit(x, y) ((x >> y) & 1LL)

int n, m;
vector<vector<pair<int, int>>> a(N);
int dis[N][N];

bool inqueue[N];
queue<int> q;
int mxans = 0;
int mx[N];

void SPFA(int root) {
  dis[root][root] = 0;
  q.push(root);
  inqueue[root] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    inqueue[u] = false;
    for (auto v : a[u]) {
      if (dis[root][v.fi] > dis[root][u] + v.se) {
        dis[root][v.fi] = dis[root][u] + v.se;
        if (!inqueue[v.fi]) {
          inqueue[v.fi] = true;
          q.push(v.fi);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++)
    mxans = max(mxans, dis[root][i]);
}

bool visited[N];
bool mark[N];
void dfs(int u, int root) {
  if (visited[u])
    return;
  visited[u] = true;
  mx[u] = dis[root][u];
  for (auto v : a[u]) {
    if (dis[root][v.fi] == dis[root][u] + v.se) {
      dfs(v.fi, root);
      mx[u] = max(mx[u], mx[v.fi]);
    }
  }
  if (mx[u] == mxans)
    mark[u] = 1;
}

vector<int> order;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef in1code
  freopen("1test.inp", "r", stdin);
#endif
  memset(dis, 127, sizeof(dis));
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    a[u].push_back(mp(v, c));
    a[v].push_back(mp(u, c));
  }
  for (int i = 1; i <= n; i++)
    SPFA(i);
  for (int i = 1; i <= n; i++) {
    memset(visited, 0, sizeof(visited));
    dfs(i, i);
  }
  for (int i = 1; i <= n; i++) {
    if (!mark[i])
      order.push_back(i);
  }
  cout << order.size() << endl;
  for (auto it : order)
    cout << it << endl;
}


Error: TLE

Fixed submission:

/*input
7 11
1 2 2
1 3 1
2 4 2
2 5 2
2 6 1
3 4 1
3 5 1
3 6 1
4 7 2
5 7 2
6 7 1
*/
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\\n'
#define fi first
#define se second
#define mp make_pair
#define int long long
#define N 1505
#define bit(x, y) ((x >> y) & 1LL)

int n, m;
vector<vector<pair<int, int>>> a(N);
int dis[N][N];

bool inqueue[N];
queue<int> q;
int mx = 0;

void SPFA(int root) {
  dis[root][root] = 0;
  q.push(root);
  inqueue[root] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    inqueue[u] = false;
    for (auto v : a[u]) {
      if (dis[root][v.fi] > dis[root][u] + v.se) {
        dis[root][v.fi] = dis[root][u] + v.se;
        if (!inqueue[v.fi]) {
          inqueue[v.fi] = true;
          q.push(v.fi);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++)
    mx = max(mx, dis[root][i]);
}

bool mark[N];
vector<int> order;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef in1code
  freopen("1test.inp", "r", stdin);
#endif
  memset(dis, 127, sizeof(dis));
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    a[u].push_back(mp(v, c));
    a[v].push_back(mp(u, c));
  }
  for (int i = 1; i <= n; i++)
    SPFA(i);
  for (int u = 1; u <= n; u++) {
    for (int v = u; v <= n; v++) {
      if (dis[u][v] != mx)
        continue;
      for (int i = 1; i <= n; i++) {
        if (dis[u][i] + dis[i][v] == mx) {
          mark[i] = true;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!mark[i])
      order.push_back(i);
  }
  cout << order.size() << endl;
  for (auto it : order)
    cout << it << endl;
}