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

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

typedef pair<int, int> P;
const int inf = 1e9 + 9;

int cost[3000][3000];
// bool used[3000];

struct edge {
  edge() {}
  edge(int t, int c) {
    to = t;
    cost = c;
  }
  int to, cost;
};

vector<edge> G[3000];
int d[3000];
bool f[3000][3000];
vector<int> pre[3000];

int n, m, k;

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> q;
  d[s] = 0;
  q.push(P(0, s));

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;

    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] == d[v] + e.cost) {
        pre[e.to].push_back(v);
      }
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        q.push(P(d[e.to], e.to));
        pre[e.to].clear();
        pre[e.to].push_back(v);
      }
    }
  }
}

/*
void dijkstra(int s)
{
  memset(used, false, sizeof(used));
  d[s] = 0;

  while (true){
    int v = -1;

    for (int u = 0; u < n; u++)
      if (!used[u] && (v == -1 || d[u] < d[v])) v = u;

    if (v == -1) break;
    used[v] = true;

    for (int u = 0; u < n; u++){
      if (d[u] == d[v] + cost[v][u]){
        pre[u].push_back(v);
      }
      if (d[u] > d[v] + cost[v][u]){
        d[u] = d[v] + cost[v][u];
        pre[u].clear();
        pre[u].push_back(v);
      }
    }
  }
}
*/

void dfs(int v) {
  for (int i = 0; i < pre[v].size(); i++) {
    int u = pre[v][i];
    if (!f[u][v])
      continue;
    f[v][u] = f[u][v] = false;
    dfs(u);
  }
}

int main() {
  for (int i = 0; i < 3000; i++)
    for (int j = 0; j < 3000; j++)
      cost[i][j] = inf;

  cin >> n >> m >> k;
  for (int i = 0; i < m; i++) {
    int a, b, l;
    cin >> a >> b >> l;
    a--;
    b--;
    cost[a][b] = cost[b][a] = l;
    G[a].push_back(edge(b, l));
    G[b].push_back(edge(a, l));
    f[a][b] = f[b][a] = true;
  }

  fill(d, d + n, inf);
  for (int i = 0; i < k; i++) {
    int s;
    cin >> s;
    dijkstra(s - 1);
  }

  for (int i = 0; i < n; i++) {
    dfs(i);
  }

  /*
  for (int i = 0; i < n; i++){
    cout << d[i] << (i + 1 == n ? "\\n" : " ");
  }

  for (int i = 0; i < n; i++){
    for (int j = 0; j < n; j++){
      cout << (f[i][j] ? "t" : "f") << (j + 1 == n ? "\\n" : " ");
    }
  }
  */

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, d[i]);
  }

  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (f[i][j]) {
        ans = max(ans, max(d[i], d[j]) +
                           (int)round((cost[i][j] - abs(d[i] - d[j])) / 2.0));
      }
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

const int inf = 1e9 + 9;

int cost[3000][3000];
int d[3000];
bool used[3000];
bool f[3000][3000];
vector<int> pre[3000];

int n, m, k;

void dijkstra(int s) {
  memset(used, false, sizeof(used));
  d[s] = 0;

  while (true) {
    int v = -1;

    for (int u = 0; u < n; u++)
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;

    if (v == -1)
      break;
    used[v] = true;

    for (int u = 0; u < n; u++) {
      if (d[u] == d[v] + cost[v][u]) {
        pre[u].push_back(v);
      }
      if (d[u] > d[v] + cost[v][u]) {
        d[u] = d[v] + cost[v][u];
        pre[u].clear();
        pre[u].push_back(v);
      }
    }
  }
}

void dfs(int v) {
  for (int i = 0; i < pre[v].size(); i++) {
    int u = pre[v][i];
    if (!f[u][v])
      continue;
    f[v][u] = f[u][v] = false;
    dfs(u);
  }
}

int main() {
  for (int i = 0; i < 3000; i++)
    for (int j = 0; j < 3000; j++)
      cost[i][j] = inf;

  cin >> n >> m >> k;
  for (int i = 0; i < m; i++) {
    int a, b, l;
    cin >> a >> b >> l;
    a--;
    b--;
    cost[a][b] = cost[b][a] = l;
    f[a][b] = f[b][a] = true;
  }

  fill(d, d + n, inf);
  for (int i = 0; i < k; i++) {
    int s;
    cin >> s;
    dijkstra(s - 1);
  }

  for (int i = 0; i < n; i++) {
    dfs(i);
  }

  /*
  for (int i = 0; i < n; i++){
    cout << d[i] << (i + 1 == n ? "\\n" : " ");
  }

  for (int i = 0; i < n; i++){
    for (int j = 0; j < n; j++){
      cout << (f[i][j] ? "t" : "f") << (j + 1 == n ? "\\n" : " ");
    }
  }
  */

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, d[i]);
  }

  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (f[i][j]) {
        ans = max(ans, max(d[i], d[j]) +
                           (int)round((cost[i][j] - abs(d[i] - d[j])) / 2.0));
      }
    }
  }

  cout << ans << endl;
}