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

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct edge {
  int to, cost;
};

int n, r;
vector<edge> g[1501];
int d[1501][1501];
int mp[1501];
int mmp[1501][1501];

inline void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d[s], d[s] + n + 1, 1145141919);
  d[s][s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[s][v] < p.first)
      continue;
    rep(i, 0, g[v].size()) {
      if (d[s][g[v][i].to] > d[s][v] + g[v][i].cost) {
        d[s][g[v][i].to] = d[s][v] + g[v][i].cost;
        que.push(P(d[s][g[v][i].to], g[v][i].to));
      }
    }
  }
}

inline void solve(int i, int j) {
  stack<int> st;
  st.push(j);
  while (!st.empty()) {
    int tp = st.top();
    st.pop();
    mmp[i][tp] = 1;
    rep(k, 0, g[tp].size()) {
      if (d[i][tp] == d[i][g[tp][k].to] + g[tp][k].cost &&
          !mmp[i][g[tp][k].to]) {
        st.push(g[tp][k].to);
      }
    }
  }
}

int main() {
  cin >> n >> r;
  rep(i, 0, r) {
    int a, b, c;
    cin >> a >> b >> c;
    edge e;
    e.to = b, e.cost = c;
    g[a].pb(e);
    e.to = a;
    g[b].pb(e);
  }
  rep(i, 1, n + 1) { dijkstra(i); }
  int mas = 0;
  rep(i, 1, n + 1) {
    rep(j, 1, n + 1) { mas = max(mas, d[i][j]); }
  }
  rep(i, 1, n + 1) {
    rep(j, i + 1, n + 1) {
      if (mas == d[i][j]) {
        mp[i] = 1;
        solve(i, j);
      }
    }
  }
  rep(i, 0, n + 1) {
    rep(j, 0, n + 1) {
      if (mmp[i][j])
        mp[j] = 1;
    }
  }
  int count = 0;
  rep(i, 1, n + 1) {
    if (!mp[i])
      count++;
  }
  cout << count << endl;
  rep(i, 1, n + 1) {
    if (!mp[i])
      cout << i << endl;
  }
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct edge {
  int to, cost;
};

int n, r;
vector<edge> g[1501];
int d[1501][1501];
int mp[1501];

inline void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d[s], d[s] + n + 1, 1145141919);
  d[s][s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[s][v] < p.first)
      continue;
    rep(i, 0, g[v].size()) {
      if (d[s][g[v][i].to] > d[s][v] + g[v][i].cost) {
        d[s][g[v][i].to] = d[s][v] + g[v][i].cost;
        que.push(P(d[s][g[v][i].to], g[v][i].to));
      }
    }
  }
}

inline void solve(int i, int j) {
  stack<int> st;
  st.push(j);
  int mmp[1501] = {};
  while (!st.empty()) {
    int tp = st.top();
    st.pop();
    mmp[tp] = 1;
    rep(k, 0, g[tp].size()) {
      if (d[i][tp] == d[i][g[tp][k].to] + g[tp][k].cost && !mmp[g[tp][k].to]) {
        st.push(g[tp][k].to);
      }
    }
  }
  rep(i, 1, n + 1) {
    if (mmp[i])
      mp[i] = 1;
  }
}

int main() {
  cin >> n >> r;
  rep(i, 0, r) {
    int a, b, c;
    cin >> a >> b >> c;
    edge e;
    e.to = b, e.cost = c;
    g[a].pb(e);
    e.to = a;
    g[b].pb(e);
  }
  rep(i, 1, n + 1) { dijkstra(i); }
  int mas = 0;
  rep(i, 1, n + 1) {
    rep(j, 1, n + 1) { mas = max(mas, d[i][j]); }
  }
  rep(i, 1, n + 1) {
    rep(j, i + 1, n + 1) {
      if (mas == d[i][j]) {
        mp[i] = 1;
        solve(i, j);
      }
    }
  }
  int count = 0;
  rep(i, 1, n + 1) {
    if (!mp[i])
      count++;
  }
  cout << count << endl;
  rep(i, 1, n + 1) {
    if (!mp[i])
      cout << i << endl;
  }
}