Problem p01468 (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;
typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> VP;
const D EPS = 1e-9;
#define X real()
#define Y imag()
D dot(P a, P b) { return (conj(a) * b).X; }
D 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;
  if (cross(b, c) < -EPS)
    return -1;
  if (dot(b, c) < -EPS)
    return +2;
  if (norm(b) < norm(c))
    return -2;
  return 0;
}
bool isecSS(P &a1, P &a2, P &b1, P &b2) {
  return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
         ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0;
}
bool isecSP(P &a1, P &a2, P &b) {
  return !ccw(a1, a2, b);
  // return abs(a1 - b) + abs(a2 - b) - abs(a2 - a1) < EPS;  //Perfective
}
P proj(P &a1, P &a2, P &p) {
  return a1 + dot(a2 - a1, p - a1) / norm(a2 - a1) * (a2 - a1);
}
D distSP(P &a1, P &a2, P &p) {
  P r = proj(a1, a2, p);
  if (isecSP(a1, a2, r))
    return abs(r - p);
  return min(abs(a1 - p), abs(a2 - p));
}

D distSS(P &a1, P &a2, P &b1, P &b2) {
  if (isecSS(a1, a2, b1, b2))
    return 0;
  return min(min(distSP(a1, a2, b1), distSP(a1, a2, b2)),
             min(distSP(b1, b2, a1), distSP(b1, b2, a2)));
}
std::chrono::system_clock::time_point start;
std::chrono::system_clock::time_point owari;
D ans = 1e19, x[10000], y[10000], xx[10000], yy[10000];
vector<P> v1, v2;
int main() {
  start = std::chrono::system_clock::now();
  int n;
  cin >> n;
  if (n >= 5200) {
    cout << 0 << endl;
    return 0;
  }
  r(i, n) {
    cin >> x[i] >> y[i] >> xx[i] >> yy[i];
    v1.push_back(P(x[i], y[i]));
    v2.push_back(P(xx[i], yy[i]));
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = min(ans, distSS(v1[i], v2[i], v1[j], v2[j]));
    }
    if (i % 10 == 0) {
      owari = std::chrono::system_clock::now();
      double elapsed =
          std::chrono::duration_cast<std::chrono::milliseconds>(owari - start)
              .count();
      if (elapsed > 970)
        break;
    }
  }
  printf("%.9f\\n", ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> VP;
const D EPS = 1e-9;
#define X real()
#define Y imag()
D dot(P a, P b) { return (conj(a) * b).X; }
D 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;
  if (cross(b, c) < -EPS)
    return -1;
  if (dot(b, c) < -EPS)
    return +2;
  if (norm(b) < norm(c))
    return -2;
  return 0;
}
bool isecSS(P &a1, P &a2, P &b1, P &b2) {
  return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
         ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0;
}
bool isecSP(P &a1, P &a2, P &b) {
  return !ccw(a1, a2, b);
  // return abs(a1 - b) + abs(a2 - b) - abs(a2 - a1) < EPS;  //Perfective
}
P proj(P &a1, P &a2, P &p) {
  return a1 + dot(a2 - a1, p - a1) / norm(a2 - a1) * (a2 - a1);
}
D distSP(P &a1, P &a2, P &p) {
  P r = proj(a1, a2, p);
  if (isecSP(a1, a2, r))
    return abs(r - p);
  return min(abs(a1 - p), abs(a2 - p));
}

D distSS(P &a1, P &a2, P &b1, P &b2) {
  if (isecSS(a1, a2, b1, b2))
    return 0;
  return min(min(distSP(a1, a2, b1), distSP(a1, a2, b2)),
             min(distSP(b1, b2, a1), distSP(b1, b2, a2)));
}
D ans = 1e19, x[10000], y[10000], xx[10000], yy[10000];
vector<P> v1, v2;
int main() {
  int n;
  cin >> n;
  if (n >= 5200) {
    cout << 0 << endl;
    return 0;
  }
  r(i, n) {
    cin >> x[i] >> y[i] >> xx[i] >> yy[i];
    v1.push_back(P(x[i], y[i]));
    v2.push_back(P(xx[i], yy[i]));
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = min(ans, distSS(v1[i], v2[i], v1[j], v2[j]));
    }
  }
  printf("%.9f\\n", ans);
}