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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-6;

/* ??????????????¬ */

#include <complex>

typedef complex<ld> Point;

const ld pi = acos(-1.0);
const ld dtop = pi / 180.;
const ld ptod = 1. / dtop;

namespace std {
bool operator<(const Point &lhs, const Point &rhs) {
  if (lhs.real() < rhs.real() - eps)
    return true;
  if (lhs.real() > rhs.real() + eps)
    return false;
  return lhs.imag() < rhs.imag();
}
} // namespace std

// ????????\\???
Point input_Point() {
  ld x, y;
  cin >> x >> y;
  return Point(x, y);
}

// ????????????????????????
bool eq(const ld a, const ld b) { return (abs(a - b) < eps); }

// ??????
ld dot(const Point &a, const Point &b) { return real(conj(a) * b); }

// ??????
ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); }

// ??´????????????
class Line {
public:
  Point a, b;
  Line() : a(Point(0, 0)), b(Point(0, 0)) {}
  Line(Point a, Point b) : a(a), b(b) {}
  Point operator[](const int _num) const {
    if (_num == 0)
      return a;
    else if (_num == 1)
      return b;
    else {
      assert(false);
      return Point();
    }
  }
};

// ?????????????????????
bool isis_sp(const Line &s, const Point &p) {
  return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
}

// ??????????¶?
Point proj(const Line &l, const Point &p) {
  ld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b);
  return l.a + t * (l.b - l.a);
}

// ????????¨???????????¢
ld dist_sp(const Line &s, const Point &p) {
  Point r = proj(s, p);
  return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}

int main() {

  int N, M, X, Y;
  cin >> N >> M >> X >> Y;
  vector<Line> ls;
  vector<Point> ps(1, Point(X, Y));
  vector<int> ids(1, -1);
  vector<Point> foods;
  for (int i = 0; i < N; ++i) {
    int px, py;
    cin >> px >> py;
    foods.push_back(Point(px, py));
  }
  vector<int> caneats(N, true);
  ld sum = 0;
  vector<ld> diss(N, 1e18);

  for (int i = 0; i < M; ++i) {
    pair<ld, int> next(1e18, 0);
    for (int j = 0; j < foods.size(); ++j) {
      if (caneats[j]) {

        auto f(foods[j]);
        ld dis = diss[j];
        if (ps.size() == 1) {
          auto p(ps.back());
          dis = min(dis, abs(p - f));
        }

        if (!ls.empty()) {
          auto l(ls.back());
          dis = min(dis, dist_sp(l, f));
        }
        diss[j] = dis;
        if (next.first - eps > dis ||
            (abs(next.first - dis) < eps && next.second < j)) {
          next = make_pair(dis, j);
        }
      }
    }
    {
      auto f(foods[next.second]);
      pair<ld, int> connect(1e18, 0);
      for (int j = 0; j < ps.size(); ++j) {
        ld dis = abs(f - ps[j]);
        if (connect.first - eps > dis ||
            (abs(connect.first - dis) < eps && connect.second < ids[j])) {

          connect = make_pair(abs(f - ps[j]), j);
        }
      }
      sum += connect.first;
      Line l{f, ps[connect.second]};
      ls.push_back(l);
      ps.push_back(f);
      ids.push_back(next.second);
      caneats[next.second] = false;
    }
  }
  cout << fixed << setprecision(10) << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

/* ??????????????¬ */

#include <complex>

typedef complex<ld> Point;

const ld pi = acos(-1.0);
const ld dtop = pi / 180.;
const ld ptod = 1. / dtop;

namespace std {
bool operator<(const Point &lhs, const Point &rhs) {
  if (lhs.real() < rhs.real() - eps)
    return true;
  if (lhs.real() > rhs.real() + eps)
    return false;
  return lhs.imag() < rhs.imag();
}
} // namespace std

// ????????\\???
Point input_Point() {
  ld x, y;
  cin >> x >> y;
  return Point(x, y);
}

// ????????????????????????
bool eq(const ld a, const ld b) { return (abs(a - b) < eps); }

// ??????
ld dot(const Point &a, const Point &b) { return real(conj(a) * b); }

// ??????
ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); }

// ??´????????????
class Line {
public:
  Point a, b;
  Line() : a(Point(0, 0)), b(Point(0, 0)) {}
  Line(Point a, Point b) : a(a), b(b) {}
  Point operator[](const int _num) const {
    if (_num == 0)
      return a;
    else if (_num == 1)
      return b;
    else {
      assert(false);
      return Point();
    }
  }
};

// ?????????????????????
bool isis_sp(const Line &s, const Point &p) {
  return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
}

// ??????????¶?
Point proj(const Line &l, const Point &p) {
  ld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b);
  return l.a + t * (l.b - l.a);
}

// ????????¨???????????¢
ld dist_sp(const Line &s, const Point &p) {
  Point r = proj(s, p);
  return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}

int main() {

  int N, M, X, Y;
  cin >> N >> M >> X >> Y;
  vector<Line> ls;
  vector<Point> ps(1, Point(X, Y));
  vector<int> ids(1, -1);
  vector<Point> foods;
  for (int i = 0; i < N; ++i) {
    int px, py;
    cin >> px >> py;
    foods.push_back(Point(px, py));
  }
  vector<int> caneats(N, true);
  ld sum = 0;
  vector<ld> diss(N, 1e18);

  for (int i = 0; i < M; ++i) {
    pair<ld, int> next(1e18, 0);
    for (int j = 0; j < foods.size(); ++j) {
      if (caneats[j]) {

        auto f(foods[j]);
        ld dis = diss[j];
        if (ps.size() == 1) {
          auto p(ps.back());
          dis = min(dis, abs(p - f));
        }

        if (!ls.empty()) {
          auto l(ls.back());
          dis = min(dis, dist_sp(l, f));
        }
        diss[j] = dis;
        if (next.first - eps > dis ||
            (abs(next.first - dis) < eps && next.second < j)) {
          next = make_pair(dis, j);
        }
      }
    }
    {
      auto f(foods[next.second]);
      pair<ld, int> connect(1e18, 0);
      for (int j = 0; j < ps.size(); ++j) {
        ld dis = abs(f - ps[j]);
        if (connect.first - eps > dis ||
            (abs(connect.first - dis) < eps && connect.second < ids[j])) {

          connect = make_pair(abs(f - ps[j]), j);
        }
      }
      sum += connect.first;
      Line l{f, ps[connect.second]};
      ls.push_back(l);
      ps.push_back(f);
      ids.push_back(next.second);
      caneats[next.second] = false;
    }
  }
  cout << fixed << setprecision(10) << sum << endl;
  return 0;
}