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

#include <bits/stdc++.h>

#define fto(i, x, y) for (int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for (int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for (int i = (x); i <= (y); i += a)
#define ftoit(it, var)                                                         \\
  for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)

#define ii pair<int, int>
#define mp make_pair
#define FF first
#define SS second

#define vi vector<int>
#define vii vector<ii>
#define ll long long
#define vll vector<ll>

#define eb emplace_back
#define ef emplace_front
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()

#define oo 1000000007
#define OO 1000000000000000007LL

using namespace std;

#define Pro "tmp"
#define maxN 1507

int D[maxN];
vii adj[maxN];

int f[maxN][maxN];
int n, m;

bool ans[maxN];

void dijkstra(int u) {
  fto(i, 1, n) D[i] = oo;
  D[u] = 0;

  set<ii> st;
  st.insert(mp(0, u));

  while (!st.empty()) {
    int u = (*st.begin()).SS;
    st.erase(st.begin());

    fto(i, 0, (int)adj[u].size() - 1) {
      int v = adj[u][i].FF;
      int w = adj[u][i].SS;

      if (D[v] > D[u] + w) {
        st.erase(mp(D[v], v));
        D[v] = D[u] + w;
        st.insert(mp(D[v], v));
      }
    }
  }

  return;
}

int main() {
  //    #ifndef ONLINE_JUDGE
  //        freopen(Pro".inp", "r", stdin);
  //        freopen(Pro".out", "w", stdout);
  //    #endif // ONLINE_JUDGE

  scanf("%d%d", &n, &m);
  fto(i, 1, m) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    adj[u].eb(mp(v, w));
    adj[v].eb(mp(u, w));
  }

  int maxAns = 0;
  vii trace;

  fto(i, 1, n) {
    dijkstra(i);
    fto(j, 1, n) {
      f[i][j] = D[j];
      if (maxAns < f[i][j]) {
        maxAns = f[i][j];
        trace.clear();
        trace.eb(i, j);
      } else if (maxAns == f[i][j])
        trace.eb(i, j);
    }
  }

  fto(w, 1, n) {
    fto(i, 0, (int)trace.size() - 1) {
      int u = trace[i].FF;
      int v = trace[i].SS;
      if (f[u][w] + f[w][v] == maxAns) {
        ans[w] = 1;
        break;
      }
    }
  }

  int num = 0;
  fto(i, 1, n) num += !ans[i];

  printf("%d\\n", num);
  fto(i, 1, n) if (!ans[i]) printf("%d\\n", i);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define fto(i, x, y) for (int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for (int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for (int i = (x); i <= (y); i += a)
#define ftoit(it, var)                                                         \\
  for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)

#define ii pair<int, int>
#define mp make_pair
#define FF first
#define SS second

#define vi vector<int>
#define vii vector<ii>
#define ll long long
#define vll vector<ll>

#define eb emplace_back
#define ef emplace_front
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()

#define oo 1000000007
#define OO 1000000000000000007LL

using namespace std;

#define Pro "tmp"
#define maxN 1507

int D[maxN];
vii adj[maxN];

int f[maxN][maxN];
int n, m;

bool ans[maxN];

void dijkstra(int u) {
  fto(i, 1, n) D[i] = oo;
  D[u] = 0;

  set<ii> st;
  st.insert(mp(0, u));

  while (!st.empty()) {
    int u = (*st.begin()).SS;
    st.erase(st.begin());

    fto(i, 0, (int)adj[u].size() - 1) {
      int v = adj[u][i].FF;
      int w = adj[u][i].SS;

      if (D[v] > D[u] + w) {
        st.erase(mp(D[v], v));
        D[v] = D[u] + w;
        st.insert(mp(D[v], v));
      }
    }
  }

  return;
}

int main() {
  //    #ifndef ONLINE_JUDGE
  //        freopen(Pro".inp", "r", stdin);
  //        freopen(Pro".out", "w", stdout);
  //    #endif // ONLINE_JUDGE

  scanf("%d%d", &n, &m);
  fto(i, 1, m) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    adj[u].eb(mp(v, w));
    adj[v].eb(mp(u, w));
  }

  int maxAns = 0;
  fto(i, 1, n) {
    dijkstra(i);
    fto(j, 1, n) {
      f[i][j] = D[j];
      maxAns = max(maxAns, f[i][j]);
    }
  }

  //    fto (i, 1, n) {
  //        fto (j, 1, n) cout << f[i][j] << " ";
  //        puts("");
  //    }

  set<int> st;
  fto(i, 1, n) st.insert(i);

  fto(i, 1, n) {
    fto(j, i, n) {
      if (f[i][j] != maxAns)
        continue;
      fto(k, 1, n) {
        if (f[i][k] + f[k][j] == maxAns)
          ans[k] = 1;
      }
    }
  }

  int num = 0;
  fto(i, 1, n) num += !ans[i];

  printf("%d\\n", num);
  fto(i, 1, n) if (!ans[i]) printf("%d\\n", i);

  return 0;
}