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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

const double EPS = 1e-10;
const double INF = 1e12;
#define EQ(n, m) (abs((n) - (m)) < EPS)
#define X real()
#define Y imag()

typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2> {
  L(const P &a, const P &b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};
double dot(P a, P b) { return (conj(a) * b).X; }
double cross(P a, P b) { return (conj(a) * b).Y; }
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return +1; // ccw
  if (cross(b, c) < -EPS)
    return -1; // cw
  if (dot(b, c) < -EPS)
    return +2; // c-a-b
  if (abs(c) - abs(b) > EPS)
    return -2; // a-b-c
  return 0;    // a-c-b
}

bool intersectSP(const L &s, const P &p) {
  return abs(cross(s[0] - p, s[1] - p)) < EPS && dot(s[0] - p, s[1] - p) < EPS;
}
bool intersectSS(const L &a, const L &b) {
  return (ccw(a[0], a[1], b[0]) * ccw(a[0], a[1], b[1]) <= 0) &&
         (ccw(b[0], b[1], a[0]) * ccw(b[0], b[1], a[1]) <= 0);
}
double distanceLP(const L &l, const P &p) {
  return abs(cross(l[1] - l[0], p - l[0])) / abs(l[1] - l[0]);
}
double distanceSP(const L &s, const P &p) {
  if (dot(s[1] - s[0], p - s[0]) < EPS)
    return abs(p - s[0]);
  if (dot(s[0] - s[1], p - s[1]) < EPS)
    return abs(p - s[1]);
  return distanceLP(s, p);
}
double distanceSS(const L &s, const L &t) {
  if (intersectSS(s, t))
    return 0;
  return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),
             min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}

const int limit = 101 * 101 / 2;

int main() {
  int n;
  cin >> n;
  vector<L> line(n);
  for (int i = 0; i < n; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    line[i] = L(P(x1, y1), P(x2, y2));
  }
  if (n > limit) {
    cout << 0 << endl;
    return 0;
  }

  double ans = INF;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = min(ans, distanceSS(line[i], line[j]));
    }
  }
  cout << fixed;
  cout << setprecision(6);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

const double EPS = 1e-10;
const double INF = 1e12;
#define EQ(n, m) (abs((n) - (m)) < EPS)
#define X real()
#define Y imag()

typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2> {
  L(const P &a, const P &b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};
double dot(P a, P b) { return (conj(a) * b).X; }
double cross(P a, P b) { return (conj(a) * b).Y; }
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return +1; // ccw
  if (cross(b, c) < -EPS)
    return -1; // cw
  if (dot(b, c) < -EPS)
    return +2; // c-a-b
  if (abs(c) - abs(b) > EPS)
    return -2; // a-b-c
  return 0;    // a-c-b
}

bool intersectSP(const L &s, const P &p) {
  return abs(cross(s[0] - p, s[1] - p)) < EPS && dot(s[0] - p, s[1] - p) < EPS;
}
bool intersectSS(const L &a, const L &b) {
  return (ccw(a[0], a[1], b[0]) * ccw(a[0], a[1], b[1]) <= 0) &&
         (ccw(b[0], b[1], a[0]) * ccw(b[0], b[1], a[1]) <= 0);
}
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));
}
double distanceSS(const L &s, const L &t) {
  if (intersectSS(s, t))
    return 0;
  return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),
             min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}

const int limit = 101 * 101 / 2;

int main() {
  int n;
  cin >> n;
  vector<L> line(n);
  for (int i = 0; i < n; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    line[i] = L(P(x1, y1), P(x2, y2));
  }
  if (n > limit) {
    cout << 0 << endl;
    return 0;
  }

  double ans = INF;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = min(ans, distanceSS(line[i], line[j]));
    }
  }
  cout << fixed;
  cout << setprecision(6);
  cout << ans << endl;
  return 0;
}