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

#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int INF = 500000000;
int n, r;
vector<int> et[1500];
vector<int> ec[1500];
bool flag[1500];

int dijkstra(int s, int maxDist) {
  int i, dist[1500];
  typedef pair<int, int> P;
  priority_queue<P, vector<P>, greater<P>> que;

  for (i = 0; i < n; i++)
    dist[i] = INF;
  que.push(P(0, s));

  while (!que.empty()) {
    P now = que.top();
    que.pop();
    int cst = now.first;
    int v = now.second;

    if (dist[v] <= cst)
      continue;
    dist[v] = cst;

    for (i = 0; i < et[v].size(); i++) {
      que.push(P(cst + ec[v][i], et[v][i]));
    }
  }

  //?????§?????¢????±???????
  int ret = 0;
  for (i = 0; i < n; i++)
    ret = max(ret, dist[i]);
  if (maxDist != ret)
    return ret;

  //??????????????¢?´¢?????????????????¨????????????
  queue<int> qu;
  bool visit[1500] = {false};

  for (i = 0; i < n; i++)
    if (dist[i] == maxDist)
      qu.push(i);
  while (!qu.empty()) {
    int v = qu.front();
    qu.pop();
    flag[v] = true;

    for (i = 0; i < et[v].size(); i++) {
      if (dist[et[v][i]] + ec[v][i] == dist[v] && !visit[et[v][i]]) {
        qu.push(et[v][i]);
        visit[et[v][i]] = true;
      }
    }
  }
  return ret;
}

int main() {
  int i;

  cin >> n >> r;
  for (i = 0; i < r; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    s--;
    t--;
    et[s].push_back(t);
    et[t].push_back(s);
    ec[s].push_back(d);
    ec[t].push_back(d);
  }

  int maxDist = -1;
  for (i = 0; i < n; i++) {
    maxDist = max(maxDist, dijkstra(i, INF));
  }

  for (i = 0; i < n; i++) {
    dijkstra(i, maxDist);
  }

  vector<int> ans;
  for (i = 0; i < n; i++) {
    if (!flag[i])
      ans.push_back(i + 1);
  }

  cout << ans.size() << endl;
  for (i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int INF = 500000000;
int n, r;
vector<int> et[1500];
vector<int> ec[1500];
bool flag[1500];

int dijkstra(int s, int maxDist) {
  int i, dist[1500];
  typedef pair<int, int> P;
  priority_queue<P, vector<P>, greater<P>> que;

  for (i = 0; i < n; i++)
    dist[i] = INF;
  que.push(P(0, s));

  while (!que.empty()) {
    P now = que.top();
    que.pop();
    int cst = now.first;
    int v = now.second;

    if (dist[v] <= cst)
      continue;
    dist[v] = cst;

    for (i = 0; i < et[v].size(); i++) {
      que.push(P(cst + ec[v][i], et[v][i]));
    }
  }

  //?????§?????¢????±???????
  int ret = 0;
  for (i = 0; i < n; i++)
    ret = max(ret, dist[i]);
  if (maxDist != ret)
    return ret;

  //??????????????¢?´¢?????????????????¨????????????
  queue<int> qu;
  bool visit[1500] = {false};

  for (i = 0; i < n; i++)
    if (dist[i] == maxDist)
      qu.push(i);
  while (!qu.empty()) {
    int v = qu.front();
    qu.pop();
    flag[v] = true;
    visit[v] = true;

    for (i = 0; i < et[v].size(); i++) {
      if (dist[et[v][i]] + ec[v][i] == dist[v] && !visit[et[v][i]]) {
        qu.push(et[v][i]);
      }
    }
  }
  return ret;
}

int main() {
  int i;

  cin >> n >> r;
  for (i = 0; i < r; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    s--;
    t--;
    et[s].push_back(t);
    et[t].push_back(s);
    ec[s].push_back(d);
    ec[t].push_back(d);
  }

  int maxDist = -1;
  for (i = 0; i < n; i++) {
    maxDist = max(maxDist, dijkstra(i, INF));
  }

  for (i = 0; i < n; i++) {
    dijkstra(i, maxDist);
  }

  vector<int> ans;
  for (i = 0; i < n; i++) {
    if (!flag[i])
      ans.push_back(i + 1);
  }

  cout << ans.size() << endl;
  for (i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}