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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const double EPS = 1.0e-10;

class Point {
public:
  double y, x;
  Point() { y = x = 0.0; }
  Point(double y0, double x0) {
    y = y0;
    x = x0;
  }
  Point operator+(const Point &p) const { return Point(y + p.y, x + p.x); }
  Point operator-(const Point &p) const { return Point(y - p.y, x - p.x); }
  Point operator*(double a) const { return Point(y * a, x * a); }
  Point operator/(double a) const { return Point(y / a, x / a); }
  double length() const { return sqrt(y * y + x * x); }
  double dist2(const Point &p) const {
    return pow(y - p.y, 2) + pow(x - p.x, 2);
  }
  double dot(const Point &p) const {
    return y * p.y + x * p.x; // |a|*|b|*cos慮
  }
  double cross(const Point &p) const {
    return x * p.y - y * p.x; // |a|*|b|*sin慮
  }
};

double segmentPointDist(const Point &a1, const Point &a2, const Point &p) {
  if ((a2 - a1).dot(p - a1) < 0)
    return a1.dist2(p);
  else if ((a1 - a2).dot(p - a2) < 0)
    return a2.dist2(p);
  else
    return pow(abs((a2 - a1).cross(p - a1)), 2) / a1.dist2(a2);
}

double segmentsDist(const Point &a1, const Point &a2, const Point &b1,
                    const Point &b2) {
  if (((a2 - a1).cross(b1 - a1) * (a2 - a1).cross(b2 - a1) < -EPS) &&
      ((b2 - b1).cross(a1 - b1) * (b2 - b1).cross(a2 - b1) < -EPS))
    return 0.0;

  double ret = segmentPointDist(a1, a2, b1);
  ret = min(ret, segmentPointDist(a1, a2, b2));
  ret = min(ret, segmentPointDist(b1, b2, a1));
  ret = min(ret, segmentPointDist(b1, b2, a2));
  return ret;
}

int main() {
  int n;
  cin >> n;
  if (n > 101 * 101 / 2) {
    cout << 0 << endl;
    return 0;
  }

  vector<Point> p1(n), p2(n);
  for (int i = 0; i < n; ++i)
    cin >> p1[i].x >> p1[i].y >> p2[i].x >> p2[i].y;

  double ret = DBL_MAX;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      ret = min(ret, segmentsDist(p1[i], p2[i], p1[j], p2[j]));
    }
  }
  printf("%.10f\\n", sqrt(ret));

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const double EPS = 1.0e-10;

class Point {
public:
  double y, x;
  Point() { y = x = 0.0; }
  Point(double y0, double x0) {
    y = y0;
    x = x0;
  }
  Point operator+(const Point &p) const { return Point(y + p.y, x + p.x); }
  Point operator-(const Point &p) const { return Point(y - p.y, x - p.x); }
  Point operator*(double a) const { return Point(y * a, x * a); }
  Point operator/(double a) const { return Point(y / a, x / a); }
  double length() const { return sqrt(y * y + x * x); }
  double dist(const Point &p) const {
    return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2));
  }
  double dot(const Point &p) const {
    return y * p.y + x * p.x; // |a|*|b|*cos慮
  }
  double cross(const Point &p) const {
    return x * p.y - y * p.x; // |a|*|b|*sin慮
  }
};

double segmentPointDist(const Point &a1, const Point &a2, const Point &p) {
  if ((a2 - a1).dot(p - a1) < 0)
    return a1.dist(p);
  else if ((a1 - a2).dot(p - a2) < 0)
    return a2.dist(p);
  else
    return abs((a2 - a1).cross(p - a1)) / a1.dist(a2);
}

double segmentsDist(const Point &a1, const Point &a2, const Point &b1,
                    const Point &b2) {
  if (((a2 - a1).cross(b1 - a1) * (a2 - a1).cross(b2 - a1) < -EPS) &&
      ((b2 - b1).cross(a1 - b1) * (b2 - b1).cross(a2 - b1) < -EPS))
    return 0.0;

  double ret = segmentPointDist(a1, a2, b1);
  ret = min(ret, segmentPointDist(a1, a2, b2));
  ret = min(ret, segmentPointDist(b1, b2, a1));
  ret = min(ret, segmentPointDist(b1, b2, a2));
  return ret;
}

int main() {
  int n;
  cin >> n;
  if (n > 101 * 101 / 2) {
    printf("0\\n");
    return 0;
  }

  vector<Point> p1(n), p2(n);
  for (int i = 0; i < n; ++i)
    scanf("%lf %lf %lf %lf", &p1[i].x, &p1[i].y, &p2[i].x, &p2[i].y);

  double ret = DBL_MAX;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      ret = min(ret, segmentsDist(p1[i], p2[i], p1[j], p2[j]));
    }
  }
  printf("%.10f\\n", ret);

  return 0;
}