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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

static const double INF = 1e20;
static const double EPS = 1e-7;

typedef complex<double> P;

inline double dot(P a, P b) { return real(conj(a) * b); }

struct L : public vector<P> {
  L(P a, P b) {
    push_back(a);
    push_back(b);
  }
};

inline bool intersectSP(const L &s, const P &p) {
  return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS;
}

inline P projection(const L &l, const P &p) {
  double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
  return l[0] + t * (l[0] - l[1]);
}

inline double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r))
    return norm(r - p);
  return min(norm(s[0] - p), norm(s[1] - p));
}

inline P in() {
  double x, y;
  cin >> x >> y;
  return P(x, y);
}

signed main() {
  int N, M;
  cin >> N >> M;
  vector<P> p(N + 1);
  p[0] = in();
  rep2(i, 1, N + 1) { p[i] = in(); }
  vector<bool> is_kyoten(N + 1);
  is_kyoten[0] = true;
  rep(i, N) is_kyoten[i + 1] = false;

  vector<double> d_points(N + 1);
  vector<double> d_seg(N + 1);

  rep(i, N + 1) {
    d_points[i] = 0;
    d_seg[i] = 0;
  }

  double ans = 0;

  double mi = INF;
  int id_mi = 0;
  rep2(i, 1, N + 1) {
    d_points[i] = norm(p[i] - p[0]);
    if (d_points[i] < mi - EPS) {
      mi = d_points[i];
      id_mi = i;
    }
  }

  ans += abs(p[id_mi] - p[0]);

  is_kyoten[id_mi] = true;
  L l = L(p[0], p[id_mi]);
  rep2(i, 1, N + 1) { d_seg[i] = distanceSP(l, p[i]); }

  rep(t, M - 1) {
    // ?¶????????????????????????¢???
    double mi = INF;
    int id_food = 0;
    rep2(i, 1, N + 1) {
      if (!is_kyoten[i] && d_seg[i] < mi - EPS) {
        mi = d_seg[i];
        id_food = i;
      }
    }

    mi = INF;
    int id_kyoten = -1;
    rep(i, N + 1) {
      if (is_kyoten[i]) {
        double dist = norm(p[i] - p[id_food]);
        if (dist < mi - EPS) {
          mi = dist;
          id_kyoten = i;
        }
      }
    }

    ans += abs(p[id_kyoten] - p[id_food]);

    L l = L(p[id_kyoten], p[id_food]);
    rep(i, N + 1) { d_seg[i] = min(d_seg[i], distanceSP(l, p[i])); }

    is_kyoten[id_food] = true;
  }

  printf("%.10f\\n", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

static const double INF = 1e10;
static const double EPS = 1e-7;

typedef complex<double> P;

double dot(P a, P b) { return real(conj(a) * b); }

struct L : public vector<P> {
  L(P a, P b) {
    push_back(a);
    push_back(b);
  }
};

bool intersectSP(const L &s, const P &p) {
  return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS;
}

P projection(const L &l, const P &p) {
  double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
  return l[0] + t * (l[0] - l[1]);
}

double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r))
    return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

P in() {
  double x, y;
  cin >> x >> y;
  return P(x, y);
}

signed main() {
  int N, M;
  cin >> N >> M;
  vector<P> p(N + 1);
  p[0] = in();
  rep2(i, 1, N + 1) { p[i] = in(); }
  vector<bool> is_kyoten(N + 1);
  is_kyoten[0] = true;
  rep(i, N) is_kyoten[i + 1] = false;

  vector<double> d_points(N + 1);
  vector<double> d_seg(N + 1);

  rep(i, N + 1) {
    d_points[i] = 0;
    d_seg[i] = 0;
  }

  double ans = 0;

  double mi = INF;
  int id_mi = 0;
  rep2(i, 1, N + 1) {
    d_points[i] = abs(p[i] - p[0]);
    if (d_points[i] < mi - EPS) {
      mi = d_points[i];
      id_mi = i;
    }
  }

  ans += abs(p[id_mi] - p[0]);

  is_kyoten[id_mi] = true;
  L l = L(p[0], p[id_mi]);
  rep2(i, 1, N + 1) { d_seg[i] = distanceSP(l, p[i]); }

  rep(t, M - 1) {
    // ?¶????????????????????????¢???
    double mi = INF;
    int id_food = 0;
    rep2(i, 1, N + 1) {
      if (!is_kyoten[i] && d_seg[i] < mi - EPS) {
        mi = d_seg[i];
        id_food = i;
      }
    }

    mi = INF;
    int id_kyoten = -1;
    rep(i, N + 1) {
      if (is_kyoten[i]) {
        double dist = abs(p[i] - p[id_food]);
        if (dist < mi - EPS) {
          mi = dist;
          id_kyoten = i;
        }
      }
    }

    ans += abs(p[id_kyoten] - p[id_food]);

    L l = L(p[id_kyoten], p[id_food]);
    rep(i, N + 1) { d_seg[i] = min(d_seg[i], distanceSP(l, p[i])); }

    is_kyoten[id_food] = true;
  }

  printf("%.10f\\n", ans);
}