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

#include <cstdio>
#include <iostream>
#include <vector>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <chrono>
#include <complex>
#include <cstring>
#include <functional>
#include <limits.h>
#include <map>
#include <math.h>
#include <nmmintrin.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <unordered_map>
const double EPS = 1e-8;
#define Scan(a)                                                                \\
  int a;                                                                       \\
  scanf("%d", &a);
#define ScanS(a)                                                               \\
  char a[500];                                                                 \\
  scanf("%s", a);
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1LL)
#define bitset(a, b) (a |= (1LL << b))
#define bitunset(a, b) (a &= ~(1LL << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#ifdef _MSC_VER
#define __builtin_popcount _mm_popcnt_u32
#define __builtin_popcountll _mm_popcnt_u64
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

pii Dir[8] = {{0, 1}, {-1, 0}, {1, 0},  {0, -1},
              {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

pii operator+(pii obj, pii obj2) {
  return MP(obj.first + obj2.first, obj.second + obj2.second);
}
pii operator-(pii obj, pii obj2) {
  return MP(obj.first - obj2.first, obj.second - obj2.second);
}
class Graph {
public:
  int vn;
  int sumcost = 0;
  vector<vector<pii>> g;

  Graph(int n) {
    vn = n;
    g.resize(n);
  }
  virtual void con(int a, int b, int w) = 0;
  int getWeight(int f, int t) {
    auto itr = lower_bound(ALL(g[f]), make_pair(t, INT_MIN));
    if (itr != g[f].end())
      return itr->second;
    return INT_MIN;
  }
  int Costsum() { return sumcost; }
  void scan(int edcount, bool oindexed, bool w) {
    REP(i, edcount) {
      int a, b, c = 1;
      scanf("%d %d", &a, &b);
      if (w)
        scanf("%d", &c);
      con(a - oindexed, b - oindexed, c);
    }
  }
};
class BiDGraph : public Graph { // 無向
public:
  BiDGraph(int n) : Graph(n) {}

  void con(int a, int b, int w = 1) {
    g[a].push_back({b, w});
    g[b].push_back({a, w});
    sumcost++;
  }
};
class DGraph : public Graph { // 有向
public:
  DGraph(int n) : Graph(n) {}
  void con(int a, int b, int w = 1) {
    g[a].push_back({b, w});
    sumcost++;
  }
};

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) { // ｘの入ってる集合と yの入ってる集合を併合
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { // xとyが同じ集合に入っているかどうかを判定
    return root(x) == root(y);
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  bool isroot(int x) { return data[x] < 0; }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  int n, r;
  cin >> n >> r;

  BiDGraph g(n);
  g.scan(r, 1, 1);

  vector<vector<int>> dis(n, vector<int>(n, -1));

  REP(cc, n) {
    vector<bool> al(n);
    priority_queue<pii, vector<pii>, greater<pii>> pq;

    pq.push({0, cc});

    while (pq.size()) {
      auto cur = pq.top();
      pq.pop();
      if (al[cur.second])
        continue;
      dis[cc][cur.second] = cur.first;
      dis[cur.second][cc] = cur.first;
      al[cur.second] = 1;
      for (auto itr : g.g[cur.second]) {
        if (!al[itr.first]) {
          pq.push({cur.first + itr.second, itr.first});
        }
      }
    }
  }

  vector<bool> used(n);
  vector<vector<bool>> al(n, vector<bool>(n));

  int Max = 0;

  REP(i, n)
  REP(j, n) { Max = max(Max, dis[i][j]); }

  REP(i, n) {
    REP(j, n) {
      if (dis[i][j] == Max) {

        queue<int> que;
        que.push(i);
        used[i] = 1;
        used[j] = 1;
        while (que.size()) {
          int cur = que.front();
          que.pop();

          if (cur == j || al[cur][j])
            continue;
          al[cur][j] = 1;
          used[cur] = 1;

          for (auto itr : g.g[cur]) {
            if (dis[itr.first][j] == dis[cur][j] - itr.second &&
                !al[itr.first][j]) {
              que.push(itr.first);
            }
          }
        }
      }
    }
  }

  int sum = 0;
  REP(i, n)
  sum += 1 - used[i];
  cout << sum << endl;
  REP(i, n)
  if (used[i] == 0) {
    printf("%d\\n", i + 1);
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;

using LL = long long;
#define DB(x) cout << #x << '=' << x << endl;

int N, R;
struct edge {
  int to;
  LL cost;
  edge(int to_, LL cost_) : to(to_), cost(cost_) {}
};
const LL INF = 123456789012345;
using P = pair<LL, int>;
void dijkstra(vector<edge> graph_[], LL dist[], int n_, int s_) {
  priority_queue<P, vector<P>, greater<P>> que;
  que.push({0, s_});
  fill(dist, dist + n_, INF);
  dist[s_] = 0;
  while (!que.empty()) {
    auto q = que.top();
    que.pop();
    LL thd = q.first;
    int thv = q.second;
    if (thd > dist[thv])
      continue;
    for (auto ed : graph_[thv]) {
      if (dist[ed.to] > ed.cost + thd) {
        dist[ed.to] = ed.cost + thd;
        que.push({dist[ed.to], ed.to});
      }
    }
  }
}

vector<edge> graph[1500];
LL dist[1500][1500];
int S[3000], T[3000], D[3000];
int used[1500];
int main() {
  cin >> N >> R;
  for (int j = 0; j < R; ++j) {
    int s, t, d;
    cin >> s >> t >> d;
    --s;
    --t;
    S[j] = s;
    T[j] = t;
    D[j] = d;
    graph[s].push_back({t, d});
    graph[t].push_back({s, d});
  }
  map<LL, vector<pair<int, int>>> cos;
  for (int i = 0; i < N; ++i) {
    dijkstra(graph, dist[i], N, i);
    for (int j = 0; j < N; ++j) {
      cos[dist[i][j]].push_back({i, j});
    }
  }
  auto itr = cos.end();
  --itr;
  auto vec = itr->second;
  for (auto p : vec) {
    int start = p.first;
    int finish = p.second;
    for (int mid = 0; mid < N; ++mid) {
      // DB(dist[start][finish]);
      // DB(dist[start][mid]);
      // DB(dist[mid][finish]);
      if (dist[start][finish] == dist[start][mid] + dist[mid][finish]) {
        used[mid] = true;
      }
    }
  }
  vector<int> answer;
  for (int i = 0; i < N; ++i) {
    if (!used[i])
      answer.push_back(i);
  }
  cout << answer.size() << endl;
  for (int out : answer)
    cout << out + 1 << endl;
  return 0;
}