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

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define EPS (1e-8)
#define equals(a, b) (fabs((a) - (b)) < EPS)
class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
};
struct seg {
  Point p1, p2;
};
double norm(Point p) { return p.x * p.x + p.y * p.y; }
double abs(Point p) { return sqrt(norm(p)); }
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
double getDistancePP(Point a, Point b) { return abs(a - b); }
double getDistanceLP(seg l, Point p) {
  return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1));
}
double getDistanceSP(seg s, Point p) {
  if (dot(s.p2 - s.p1, p - s.p1) < 0.0)
    return abs(p - s.p1);
  if (dot(s.p1 - s.p2, p - s.p2) < 0.0)
    return abs(p - s.p2);
  return getDistanceLP(s, p);
}
int n, m, c = 1, pre, fro, p, t;
Point a[5001];
seg w;
bool vis[5001];
double d[5001];
double dd = 1000000000, dis = 0;
int main() {
  cin >> n >> m;
  n++;
  r(i, n) cin >> a[i].x >> a[i].y;
  r(i, n) d[i] = abs(a[0] - a[i]);
  vis[0] = 1;
  seg w;
  w.p1 = a[0];
  w.p2 = a[p];
  while (m--) {
    dd = 10000000;
    t = -1;
    c = 0;
    r(i, n) if (!vis[i]) if (t < 0 || d[i] < d[t]) t = i;
    r(i, n) if (vis[i]) if (abs(a[i] - a[t]) < abs(a[c] - a[t])) c = i;
    dis += abs(a[c] - a[t]);
    w.p1 = a[c];
    w.p2 = a[t];
    vis[t] = 1;
    r(i, n) if (!vis[i]) d[i] = min(d[i], getDistanceSP(w, a[i]));
  }
  printf("%.12f\\n", dis);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define EPS (1e-8)
#define equals(a, b) (fabs((a) - (b)) < EPS)
class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
};
struct seg {
  Point p1, p2;
};
double norm(Point p) { return p.x * p.x + p.y * p.y; }
double abs(Point p) { return sqrt(norm(p)); }
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
double getDistancePP(Point a, Point b) { return abs(a - b); }
double getDistanceLP(seg l, Point p) {
  return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1));
}
double getDistanceSP(seg s, Point p) {
  if (dot(s.p2 - s.p1, p - s.p1) < 0.0)
    return abs(p - s.p1);
  if (dot(s.p1 - s.p2, p - s.p2) < 0.0)
    return abs(p - s.p2);
  return getDistanceLP(s, p);
}
int n, m, c = 1, pre, fro, p;
Point a[5001];
bool vis[5001];
double d = 1000000000, dis = 0;
int main() {
  cin >> n >> m >> a[0].x >> a[0].y;
  r(i, n) scanf("%lf%lf", &a[i + 1].x, &a[i + 1].y);
  r(i, n) {
    double t = abs(a[0] - a[i + 1]);
    if (d > t)
      p = i + 1, d = t;
  }
  dis += d;
  vis[0] = 1;
  vis[p] = 1;
  vector<seg> s;
  seg w;
  w.p1 = a[0];
  w.p2 = a[p];
  s.push_back(w);
  while (c != m) {
    c++;
    d = 10000000000;
    r(i, s.size()) r(j, n + 1) if (!vis[j]) {
      double t = getDistanceSP(s[i], a[j]);
      if (t < d)
        d = t, fro = j;
    }
    d = 1000000000;
    r(i, n + 1) if (vis[i]) {
      double t = abs(a[i] - a[fro]);
      if (d > t)
        d = t, pre = i;
    }
    w.p1 = a[pre];
    w.p2 = a[fro];
    s.push_back(w);
    dis += d;
    vis[fro] = 1;
  }
  printf("%.12f\\n", dis);
}