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

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int INF = 1000000000;
typedef long long int lli;
typedef pair<int, int> P;

const int MAX_V = 5001;
int cost[MAX_V][MAX_V];
int d[MAX_V];
bool used[MAX_V];
int shortest[MAX_V];

int abs(int x) {
  if (x < 0)
    return -x;
  else
    return x;
}

int main() {
  int V, E, K;
  cin >> V >> E >> K;
  for (int i = 1; i <= V; ++i) {
    for (int j = 1; j <= V; ++j) {
      if (i == j) {
        cost[i][j] = 0;
      } else {
        cost[i][j] = INF;
      }
    }
  }
  for (int i = 0; i < E; ++i) {
    int a, b, l;
    cin >> a >> b >> l;
    cost[a][b] = cost[b][a] = l;
  }
  fill(d, d + V + 1, INF);
  for (int i = 0; i < K; ++i) {
    int tmp;
    cin >> tmp;
    d[tmp] = 0;
  }
  fill(shortest, shortest + V + 1, INF);
  fill(used, used + V + 1, false);
  while (true) {
    int v = -1;
    for (int u = 1; u <= V; ++u) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }

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

    for (int u = 1; u <= V; ++u) {
      d[u] = min(d[u], d[v] + cost[v][u]);
      if (shortest[u] > d[u])
        shortest[u] = d[u];
    }
  }

  int ans = -1;
  for (int i = 1; i <= V; ++i) {
    for (int j = 1; j <= V; ++j) {
      if (cost[i][j] != INF) {
        int tmp = abs((shortest[j] + shortest[i] + cost[i][j] + 1) / 2);
        ans = max(ans, tmp);
      }
    }
  }
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int INF = 1000000000;
typedef long long int lli;
typedef pair<int, int> P;

const int MAX_V = 5001;
int cost[MAX_V][MAX_V];
int d[MAX_V];
bool used[MAX_V];
vector<int> mall;
int shortest[MAX_V];

int abs(int x) {
  if (x < 0)
    return -x;
  else
    return x;
}

int main() {
  int V, E, K;
  cin >> V >> E >> K;
  for (int i = 1; i <= V; ++i) {
    for (int j = 1; j <= V; ++j) {
      if (i == j) {
        cost[i][j] = 0;
      } else {
        cost[i][j] = INF;
      }
    }
  }
  for (int i = 0; i < E; ++i) {
    int a, b, l;
    cin >> a >> b >> l;
    cost[a][b] = l;
    cost[b][a] = l;
  }
  for (int i = 0; i < K; ++i) {
    int tmp;
    cin >> tmp;
    mall.push_back(tmp);
  }
  fill(shortest, shortest + V + 1, INF);
  for (int i = 0; i < K; ++i) {
    fill(d, d + V + 1, INF);
    fill(used, used + V + 1, false);
    d[mall[i]] = 0;
    while (true) {
      int v = -1;
      for (int u = 1; u <= V; ++u) {
        if (!used[u] && (v == -1 || d[u] < d[v]))
          v = u;
      }

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

      for (int u = 1; u <= V; ++u) {
        d[u] = min(d[u], d[v] + cost[v][u]);
        if (shortest[u] > d[u])
          shortest[u] = d[u];
      }
    }
  }

  int ans = -1;
  for (int i = 1; i <= V; ++i) {
    for (int j = 1; j <= V; ++j) {
      if (cost[i][j] != INF) {
        int tmp = abs((shortest[j] + shortest[i] + cost[i][j] + 1) / 2);
        ans = max(ans, tmp);
      }
    }
  }
  cout << ans << "\\n";
  return 0;
}