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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF (1 << 29)

typedef pair<int, int> pii;

int d[3000][3000]; // ???i, j????????¢
int minCost[3000]; // ???i????????§???????????°??¢???????????§??????????????¢
bool shop[3000];   // ???i????????§???????????°??¢??????????????????
vector<pii> g[3000]; // to, cost
int n, m, k;

int main() {
  cin >> n >> m >> k;

  fill(d[0], d[n], INF);
  fill(minCost, minCost + n, INF);

  REP(i, m) {
    int a, b, l;
    scanf("%d %d %d", &a, &b, &l);
    a--;
    b--;
    g[a].push_back(pii(b, l));
    g[b].push_back(pii(a, l));
    d[a][b] = d[b][a] = l;
  }

  REP(i, k) {
    int pos;
    scanf("%d", &pos);
    pos--;
    shop[pos] = true;
  }

  REP(i, n) {
    priority_queue<pii, vector<pii>, greater<pii>> pq; // cost, pos
    pq.push(pii(0, i));

    vector<int> dist(n, INF);
    dist[i] = 0;
    while (!pq.empty()) {
      pii now = pq.top();
      pq.pop();

      int ncost = now.first;
      int npos = now.second;

      if (dist[npos] < ncost)
        continue;

      if (shop[npos]) {
        minCost[i] = dist[npos];
        break;
      }

      REP(j, g[npos].size()) {
        int to = g[npos][j].first;
        int cost = ncost + g[npos][j].second;
        if (dist[to] > cost) {
          dist[to] = cost;
          pq.push(pii(cost, to));
        }
      }
    }
  }

  int ans = 0;
  REP(i, n) FOR(j, i + 1, n) {
    if (d[i][j] == INF)
      continue;
    ans = max(ans, (minCost[i] + minCost[j] + d[i][j] + 1) / 2);
  }
  cout << ans << endl;

  return 0;
}

// 1:20

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF (1 << 29)

typedef pair<int, int> pii;

int d[3000][3000]; // ???i, j????????¢
int minCost[3000]; // ???i????????§???????????°??¢???????????§??????????????¢
bool shop[3000];   // ???i????????§???????????°??¢??????????????????
vector<pii> g[3000]; // to, cost
int n, m, k;

int main() {
  cin >> n >> m >> k;

  fill(d[0], d[n], INF);
  fill(minCost, minCost + n, INF);

  REP(i, m) {
    int a, b, l;
    scanf("%d %d %d", &a, &b, &l);
    a--;
    b--;
    g[a].push_back(pii(b, l));
    g[b].push_back(pii(a, l));
    d[a][b] = d[b][a] = l;
  }

  REP(i, k) {
    int pos;
    scanf("%d", &pos);
    pos--;
    shop[pos] = true;
  }

  REP(i, n) {
    priority_queue<pii, vector<pii>, greater<pii>> pq; // cost, pos
    pq.push(pii(0, i));

    vector<int> dist(n, INF);
    while (!pq.empty()) {
      pii now = pq.top();
      pq.pop();

      int ncost = now.first;
      int npos = now.second;

      if (dist[npos] <= ncost)
        continue;
      dist[npos] = ncost;
      if (shop[npos]) {
        minCost[i] = dist[npos];
        break;
      }

      REP(j, g[npos].size())
      pq.push(pii(ncost + g[npos][j].second, g[npos][j].first));
    }
  }

  int ans = 0;
  REP(i, n) FOR(j, i + 1, n) {
    if (d[i][j] == INF)
      continue;
    ans = max(ans, (minCost[i] + minCost[j] + d[i][j] + 1) / 2);
  }
  cout << ans << endl;

  return 0;
}