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

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>
#define fi first
#define sec second
#define INF 100000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int n, m, k;
int cost[3000][3000];
int cnt[3000];
vector<P> e[3000];
bool shop[3000];
int ans;

void dijkstra(int s) {
  rep(i, n) cost[s][i] = INF;
  if (!shop[s])
    return;
  cost[s][s] = 0;
  queue<P> que;
  que.push(P(s, 0));
  while (true) {
    if (que.size() == 0)
      break;
    P p = que.front();
    que.pop();
    if (p.sec > cost[s][p.fi])
      continue;
    rep(i, e[p.fi].size()) {
      if (shop[e[p.fi][i].fi])
        continue;
      if (cost[s][e[p.fi][i].fi] > cost[s][p.fi] + e[p.fi][i].sec) {
        cost[s][e[p.fi][i].fi] = cost[s][p.fi] + e[p.fi][i].sec;
        que.push(P(e[p.fi][i].fi, cost[s][e[p.fi][i].fi]));
      }
    }
  }
}

int Abs(int x) { return x > 0 ? x : -x; }

int main() {
  scanf("%d%d%d", &n, &m, &k);
  rep(i, m) {
    int a, b, l;
    scanf("%d%d%d", &a, &b, &l);
    a--;
    b--;
    e[a].push_back(P(b, l));
    e[b].push_back(P(a, l));
  }
  rep(i, k) {
    int s;
    scanf("%d", &s);
    s--;
    shop[s] = true;
  }
  rep(i, n) dijkstra(i);
  rep(i, n) cnt[i] = INF;
  rep(i, n) {
    rep(j, n) { cnt[i] = min(cnt[i], cost[j][i]); }
  }
  rep(i, n) ans = max(ans, cnt[i]);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < e[i].size(); j++) {
      int f = e[i][j].fi, s = e[i][j].sec;
      ans = max(ans, max(cnt[i], cnt[f]) + (s - Abs(cnt[i] - cnt[f]) + 1) / 2);
    }
  }
  printf("%d\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>
#define fi first
#define sec second
#define INF 100000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int n, m, k;
int cost[3000][3000];
int cnt[3000];
vector<P> e[3000];
bool shop[3000];
int ans;

void dijkstra(int s) {
  rep(i, n) cost[s][i] = INF;
  if (!shop[s])
    return;
  cost[s][s] = 0;
  queue<P> que;
  que.push(P(s, 0));
  while (true) {
    if (que.size() == 0)
      break;
    P p = que.front();
    que.pop();
    if (p.sec > cost[s][p.fi])
      continue;
    rep(i, e[p.fi].size()) {
      if (cost[s][e[p.fi][i].fi] > cost[s][p.fi] + e[p.fi][i].sec) {
        cost[s][e[p.fi][i].fi] = cost[s][p.fi] + e[p.fi][i].sec;
        que.push(P(e[p.fi][i].fi, cost[s][e[p.fi][i].fi]));
      }
    }
  }
}

int Abs(int x) { return x > 0 ? x : -x; }

int main() {
  scanf("%d%d%d", &n, &m, &k);
  rep(i, m) {
    int a, b, l;
    scanf("%d%d%d", &a, &b, &l);
    a--;
    b--;
    e[a].push_back(P(b, l));
    e[b].push_back(P(a, l));
  }
  rep(i, k) {
    int s;
    scanf("%d", &s);
    s--;
    shop[s] = true;
  }
  rep(i, n) dijkstra(i);
  rep(i, n) cnt[i] = INF;
  rep(i, n) {
    rep(j, n) { cnt[i] = min(cnt[i], cost[j][i]); }
  }
  rep(i, n) ans = max(ans, cnt[i]);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < e[i].size(); j++) {
      int f = e[i][j].fi, s = e[i][j].sec;
      ans = max(ans, max(cnt[i], cnt[f]) + (s - Abs(cnt[i] - cnt[f]) + 1) / 2);
    }
  }
  printf("%d\\n", ans);
}