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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double EPS = 1e-8, INF = 1e12, PI = acos(-1.0);
typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
inline double cross(const P &a, const P &b) { return imag(conj(a) * b); }
inline double dot(const P &a, const P &b) { return real(conj(a) * b); }
struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};
typedef vector<P> G;
inline int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return +1; // counter clockwise
  if (cross(b, c) < -EPS)
    return -1; // clockwise
  if (dot(b, c) < EPS)
    return +2; // c--a--b on line
  if (norm(b) + EPS < norm(c))
    return -2; // a--b--c on line
  return 0;
}
bool intersectSP(const L &s, const P &p) {
  return ccw(p, s[0], s[1]) == 2;
  // return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}
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));
}

int n, m, x, y, p[5001], q[5001];
bool use[5001];
double dist[5001], dd[5001][5001];

int main() {
  G g;
  cin >> n >> m >> x >> y;
  g.pb(P(x, y));
  rep(i, n) {
    cin >> p[i] >> q[i];
    g.pb(P(p[i], q[i]));
  }
  n++;
  rep(i, n) rep(j, i) dd[i][j] = dd[j][i] = abs(g[i] - g[j]);

  double ans = 0;
  rep(i, n) dist[i] = dd[0][i];
  use[0] = 1;

  rep(it, m) {
    double mn = INF;
    int mni;
    rep(i, n) if (!use[i]) {
      if (dist[i] + EPS < mn) {
        mn = dist[i], mni = i;
      }
    }
    double mn2 = INF;
    int mni2;
    rep(i, n) if (use[i]) {
      double d = dd[mni][i];
      if (d + EPS < mn2) {
        mn2 = d, mni2 = i;
      }
    }

    use[mni] = 1;
    rep(i, n) if (!use[i]) {
      double d = distanceSP(L(g[mni], g[mni2]), g[i]);
      dist[i] = min(dist[i], d);
    }
    ans += dd[mni][mni2];
    /*
    cerr<<"taberu: "<<mni<<" from: "<<mni2<<endl;
    dbg(mn);*/
  }
  printf("%.9f\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double EPS = 1e-8, INF = 1e12, PI = acos(-1.0);
typedef complex<long double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
inline double cross(const P &a, const P &b) { return imag(conj(a) * b); }
inline double dot(const P &a, const P &b) { return real(conj(a) * b); }
struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};
typedef vector<P> G;
inline int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return +1; // counter clockwise
  if (cross(b, c) < -EPS)
    return -1; // clockwise
  if (dot(b, c) < EPS)
    return +2; // c--a--b on line
  if (norm(b) + EPS < norm(c))
    return -2; // a--b--c on line
  return 0;
}
bool intersectSP(const L &s, const P &p) {
  return ccw(p, s[0], s[1]) == 2;
  // return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}
P projection(const L &l, const P &p) {
  long 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));
}

int n, m, x, y, p[5000], q[5000];
bool use[5000];
double dist[5000], dd[5000][5000];

int main() {
  G g;
  cin >> n >> m >> x >> y;
  g.pb(P(x, y));
  rep(i, n) {
    cin >> p[i] >> q[i];
    g.pb(P(p[i], q[i]));
  }
  n++;
  rep(i, n) rep(j, i) dd[i][j] = dd[j][i] = abs(g[i] - g[j]);

  double ans = 0;
  rep(i, n) dist[i] = dd[0][i];
  use[0] = 1;

  rep(it, m) {
    double mn = INF;
    int mni;
    rep(i, n) if (!use[i]) {
      if (dist[i] + EPS < mn) {
        mn = dist[i], mni = i;
      }
    }
    double mn2 = INF;
    int mni2;
    rep(i, n) if (use[i]) {
      double d = dd[mni][i];
      if (d + EPS < mn2) {
        mn2 = d, mni2 = i;
      }
    }

    use[mni] = 1;
    rep(i, n) if (!use[i]) {
      double d = distanceSP(L(g[mni], g[mni2]), g[i]);
      dist[i] = min(dist[i], d);
    }
    ans += dd[mni][mni2];
    /*
    cerr<<"taberu: "<<mni<<" from: "<<mni2<<endl;
    dbg(mn);*/
  }
  printf("%.9f\\n", ans);

  return 0;
}