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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M;
vector<P> G[1500];
int D[1500][1500];
void dijkstra(int s) {
  int *dist = D[s];
  priority_queue<P, vector<P>, greater<P>> q;
  dist[s] = 0;
  q.push(P(0, s));
  while (!q.empty()) {
    int x = q.top()._2, r = q.top()._1;
    q.pop();
    if (dist[x] < r)
      continue;

    for (P p : G[x]) {
      int t = p._1, l = p._2;
      if (dist[t] > r + l) {
        dist[t] = r + l;
        q.push(P(r + l, t));
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  rep(i, M) {
    int s, t, d;
    cin >> s >> t >> d;
    s--, t--;
    G[s].pb(P(t, d));
    G[t].pb(P(s, d));
  }
  rep(i, N) rep(j, N) D[i][j] = INF;
  rep(i, N) dijkstra(i);
  int R = 0;
  rep(i, N) rep(j, N) R = max(R, D[i][j]);

  vector<int> ans;
  rep(i, N) {
    vector<int> rs;
    map<int, vector<int>> vs;
    rep(j, N) rs.pb(D[i][j]), vs[D[i][j]].pb(j);
    sort(all(rs));
    uniq(rs);
    // for (int x:rs)cout<<x<<",";cout<<" i="<<i<<"\\n";
    bool ok = true;
    for (int x : rs) {
      if (binary_search(all(rs), R - x)) {
        for (int s : vs[x]) {
          for (int t : vs[R - x]) {
            if (D[s][t] == R) {
              ok = false;
              break;
            }
          }
        }
      }
    }
    if (ok)
      ans.pb(i);
  }
  cout << ans.size() << "\\n";
  for (int x : ans)
    cout << x + 1 << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M;
vector<P> G[1500];
int D[1500][1500];
void dijkstra(int s) {
  int *dist = D[s];
  priority_queue<P, vector<P>, greater<P>> q;
  dist[s] = 0;
  q.push(P(0, s));
  while (!q.empty()) {
    int x = q.top()._2, r = q.top()._1;
    q.pop();
    if (dist[x] < r)
      continue;

    for (P p : G[x]) {
      int t = p._1, l = p._2;
      if (dist[t] > r + l) {
        dist[t] = r + l;
        q.push(P(r + l, t));
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  rep(i, M) {
    int s, t, d;
    cin >> s >> t >> d;
    s--, t--;
    G[s].pb(P(t, d));
    G[t].pb(P(s, d));
  }
  rep(i, N) rep(j, N) D[i][j] = INF;
  rep(i, N) dijkstra(i);
  int R = 0;
  rep(i, N) rep(j, N) R = max(R, D[i][j]);

  vector<int> ans;
  rep(i, N) {
    vector<int> rs;
    map<int, vector<int>> vs;
    rep(j, N) rs.pb(D[i][j]), vs[D[i][j]].pb(j);
    sort(all(rs));
    // for (int x:rs)cout<<x<<",";cout<<" i="<<i<<"\\n";
    bool ok = true;
    for (int x : rs) {
      if (binary_search(all(rs), R - x)) {
        for (int s : vs[x]) {
          for (int t : vs[R - x]) {
            if (D[s][t] == R) {
              ok = false;
              break;
            }
          }
        }
      }
    }
    if (ok)
      ans.pb(i);
  }
  cout << ans.size() << "\\n";
  for (int x : ans)
    cout << x + 1 << "\\n";
  return 0;
}