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

/*
 *  頭ついてない
 *  まるで食べられたアンパンマンみたい
 */

#include <cmath>
#include <cstdio>
#include <queue>
#include <utility>

using namespace std;

typedef pair<int, int> P;
#define INF 0x40000000

class Edge {
public:
  int from;
  int to;
  int len;
};

class Vertex {
public:
  vector<Edge *> children;
};

Vertex vs[3000];
Edge es[200000];
int n, m, k;
int to_mall[3000];
priority_queue<P, vector<P>, greater<P>> q;
int min_d;
float answer;

int main() {

  scanf(" %d %d %d", &n, &m, &k);
  for (int i = 0; i < m; i++) {
    int a, b, l;

    scanf(" %d %d %d", &a, &b, &l);
    --a;
    --b;
    es[i * 2].from = a;
    es[i * 2].to = b;
    es[i * 2].len = l;
    es[i * 2 + 1].from = b;
    es[i * 2 + 1].to = a;
    es[i * 2 + 1].len = l;
    vs[a].children.push_back(&es[i * 2]);
    vs[b].children.push_back(&es[i * 2 + 1]);
  }

  fill(to_mall, to_mall + n, INF);
  for (int i = 0; i < k; i++) {
    int s;

    scanf("%d", &s);
    --s;
    to_mall[s] = 0;
    q.push(make_pair(s, 0));
  }

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int s = p.first;

    if (to_mall[s] < p.second)
      continue;
    for (int j = 0; j < vs[s].children.size(); j++) {
      Edge *e = vs[s].children[j];
      if (to_mall[e->to] > to_mall[s] + e->len) {
        to_mall[e->to] = to_mall[s] + e->len;
        q.push(make_pair(e->to, to_mall[e->to]));
      }
    }
  }

  answer = 0;
  for (int i = 0; i < m * 2; i++) {
    int a = es[i].from;
    int b = es[i].to;
    int l = es[i].len;
    float d = (to_mall[a] + to_mall[b] + l) / 2.0;

    if (d > answer) {
      answer = d;
    }
  }

  printf("%d\\n", (int)ceil(answer));
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <queue>
#include <utility>

using namespace std;

typedef pair<int, int> P;
#define INF 0x40000000

class Edge {
public:
  int from;
  int to;
  int len;
};

class Vertex {
public:
  vector<Edge *> children;
};

Vertex vs[3000];
Edge es[200000];
int n, m, k;
int malls[3000];
int mall_size;
int direction[3000];
int to_mall[3000];

int main() {
  float max_d;

  scanf(" %d %d %d", &n, &m, &k);
  for (int i = 0; i < m; i++) {
    int a, b, l;

    scanf(" %d %d %d", &a, &b, &l);
    --a;
    --b;
    es[i * 2].from = a;
    es[i * 2].to = b;
    es[i * 2].len = l;
    es[i * 2 + 1].from = b;
    es[i * 2 + 1].to = a;
    es[i * 2 + 1].len = l;
    vs[a].children.push_back(&es[i * 2]);
    vs[b].children.push_back(&es[i * 2 + 1]);
  }

  for (int i = 0; i < k; i++) {
    int s;

    scanf("%d", &s);
    --s;
    malls[mall_size] = s;
    mall_size++;
  }

  for (int i = 0; i < n; i++) {
    int min_d;
    priority_queue<P, vector<P>, greater<P>> q;

    fill(direction, direction + n, INF);

    direction[i] = 0;
    q.push(make_pair(i, 0));
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int s = p.first;

      if (direction[s] < p.second)
        continue;
      for (int j = 0; j < vs[s].children.size(); j++) {
        Edge *e = vs[s].children[j];
        if (direction[e->to] > direction[s] + e->len) {
          direction[e->to] = direction[s] + e->len;
          q.push(make_pair(e->to, direction[e->to]));
        }
      }
    }

    to_mall[i] = -1;
    for (int j = 0; j < mall_size; j++) {
      int t = malls[j];

      if (to_mall[i] == -1 || to_mall[i] > direction[t]) {
        to_mall[i] = direction[t];
      }
    }
  }

  max_d = 0;
  for (int i = 0; i < m * 2; i++) {
    int a = es[i].from;
    int b = es[i].to;
    int l = es[i].len;
    float d = (to_mall[a] + to_mall[b] + l) / 2.0;

    if (d > max_d) {
      max_d = d;
    }
  }

  printf("%d\\n", (int)ceil(max_d));
}