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

#include <bits/stdc++.h>

#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x < b; ++x)
#define DEBUG(X)                                                               \\
  { cout << #X << " = " << X << endl; }
#define PR(A, n)                                                               \\
  {                                                                            \\
    cout << #A << " = ";                                                       \\
    FOR(_, 1, n) cout << A[_] << " ";                                          \\
    cout << endl;                                                              \\
  }
#define PR0(A, n)                                                              \\
  {                                                                            \\
    cout << #A << " = ";                                                       \\
    REP(_, 0, n) cout << A[_] << " ";                                          \\
    cout << endl;                                                              \\
  }

using namespace std;

typedef long long LL;
typedef pair<int, int> II;

const int N = 1500 + 10;
const int INF = 0x3f3f3f3f;

int n, m;
int d[N][N];

bool ban[N], fre[N];

II V[2 * N];

vector<II> adj[N];
priority_queue<II> pq;

void Dijkstra(int *d, int s) {
  d[s] = 0;
  pq.push(II(-d[s], s));
  while (pq.size()) {
    int u = pq.top().second, w = -pq.top().first;
    pq.pop();
    if (w > d[u])
      continue;
    REP(k, 0, adj[u].size()) {
      int v = adj[u][k].first, w = adj[u][k].second;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        pq.push(II(-d[v], v));
      }
    }
  }
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // LOCAL
  scanf("%d%d", &n, &m);
  FOR(i, 1, m) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    adj[u].push_back(II(v, w));
    adj[v].push_back(II(u, w));
    // cout << u << " " << v << " " << w << endl;
  }
  memset(d, INF, sizeof d);
  FOR(i, 1, n) { Dijkstra(d[i], i); }
  int maxDist = -INF;
  FOR(i, 1, n)
  FOR(j, 1, n) maxDist = max(maxDist, d[i][j]);
  int VSize = 0;
  FOR(i, 1, n) ban[i] = true;
  FOR(i, 1, n)
  FOR(j, 1, n) if (d[i][j] == maxDist) {
    V[++VSize] = II(i, j);
    ban[i] = false;
    ban[j] = false;
  }
  FOR(i, 1, n) if (ban[i] == true) {
    FOR(j, 1, VSize) {
      int x = V[j].first, y = V[j].second;
      if (d[x][i] + d[i][y] == maxDist)
        ban[i] = false;
    }
  }
  int sz = 0;
  FOR(i, 1, n) if (ban[i])++ sz;
  printf("%d\\n", sz);
  FOR(i, 1, n) if (ban[i]) printf("%d\\n", i);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x < b; ++x)
#define DEBUG(X)                                                               \\
  { cout << #X << " = " << X << endl; }
#define PR(A, n)                                                               \\
  {                                                                            \\
    cout << #A << " = ";                                                       \\
    FOR(_, 1, n) cout << A[_] << " ";                                          \\
    cout << endl;                                                              \\
  }
#define PR0(A, n)                                                              \\
  {                                                                            \\
    cout << #A << " = ";                                                       \\
    REP(_, 0, n) cout << A[_] << " ";                                          \\
    cout << endl;                                                              \\
  }

using namespace std;

typedef long long LL;
typedef pair<int, int> II;

const int N = 1500 + 10;
const int INF = 0x3f3f3f3f;

int n, m;
int d[N][N];

bool ban[N], fre[N];

II V[N];

vector<II> adj[N];

void Dijkstra(int *d, int s) {
  priority_queue<II> pq;
  d[s] = 0;
  pq.push(II(d[s], s));
  while (pq.size()) {
    int u = pq.top().second, w = pq.top().first;
    pq.pop();
    if (w > d[u])
      continue;
    REP(k, 0, adj[u].size()) {
      int v = adj[u][k].first, w = adj[u][k].second;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        pq.push(II(d[v], v));
      }
    }
  }
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // LOCAL
  scanf("%d%d", &n, &m);
  FOR(i, 1, m) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    adj[u].push_back(II(v, w));
    adj[v].push_back(II(u, w));
    // cout << u << " " << v << " " << w << endl;
  }
  FOR(i, 1, n) {
    memset(d[i], INF, sizeof d[i]);
    Dijkstra(d[i], i);
  }
  int maxDist = -INF;
  FOR(i, 1, n)
  FOR(j, 1, n) maxDist = max(maxDist, d[i][j]);
  int VSize = 0;
  FOR(i, 1, n) ban[i] = true;
  FOR(i, 1, n)
  FOR(j, 1, n) if (d[i][j] == maxDist) {
    V[++VSize] = II(i, j);
    ban[i] = false;
    ban[j] = false;
  }
  FOR(i, 1, n) if (ban[i] == true) {
    FOR(j, 1, VSize) {
      int x = V[j].first, y = V[j].second;
      if (d[x][i] + d[i][y] == maxDist)
        ban[i] = false;
    }
  }
  int sz = 0;
  FOR(i, 1, n) if (ban[i])++ sz;
  printf("%d\\n", sz);
  FOR(i, 1, n) if (ban[i]) printf("%d\\n", i);
  return 0;
}