Problem p00738 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;

typedef double Num;
typedef complex<Num> Pt2;
typedef vector<Pt2> Polygon2;

struct Line2 : public vector<Pt2> {
  Line2(Pt2 a, Pt2 b) {
    push_back(a);
    push_back(b);
  }
};

const double eps = 1e-10;
Num dot(Pt2 a, Pt2 b) { return (a * conj(b)).real(); }
Num cross(Pt2 a, Pt2 b) { return (conj(a) * b).imag(); }
Pt2 vec(Line2 l) { return l[1] - l[0]; }

int ccw(Pt2 a, Pt2 b, Pt2 c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // ccw
  if (cross(b, c) < 0)
    return -1; // cw
  if (dot(b, c) < 0)
    return +2; // cab
  if (norm(b) < norm(c))
    return -2; // abc
  return 0;    // acb
}

int ccw(Line2 s, Pt2 p) { return ccw(s[0], s[1], p); }

bool intersectSP(Line2 s, Pt2 p) {
  return abs(s[0] - p) + abs(s[1] - p) < abs(s[1] - s[0]) + eps;
}

bool intersectSS(Line2 s, Line2 t) {
  return ccw(s, t[0]) * ccw(s, t[1]) <= 0 && ccw(t, s[0]) * ccw(t, s[1]) <= 0;
}

Pt2 projection(Line2 l, Pt2 p) {
  double t = dot(p - l[0], vec(l)) / norm(vec(l));
  return l[0] + t * (vec(l));
}

Pt2 reflection(Line2 l, Pt2 p) { return p + 2.0 * (projection(l, p) - p); }

Num distanceSP(Line2 s, Pt2 p) {
  Pt2 r = projection(s, p);
  if (intersectSP(s, r))
    return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

Num distanceSS(Line2 s, Line2 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])));
}

bool convex_contains(Polygon2 g, Pt2 p) {
  for (int i = 0; i < g.size(); i++) {
    if (ccw(Line2(g[i], g[(i + 1) % g.size()]), p) == -1)
      return false;
  }
  return true;
}

double dist(Line2 s, Line2 t, double h) {
  double d = distanceSS(s, t);
  if (d <= h)
    return d;
  return (d * d + h * h) / (2 * h);
}

int main() {
  int N;
  while (cin >> N, N) {
    Num x1, y1, x2, y2, h;
    cin >> x1 >> y1 >> x2 >> y2;
    Line2 course(Pt2(x1, y1), Pt2(x2, y2));

    double ans = 1e10;
    while (N--) {
      cin >> x1 >> y1 >> x2 >> y2 >> h;
      Polygon2 g(4);
      g[0] = Pt2(x1, y2);
      g[3] = Pt2(x2, y2);
      g[1] = Pt2(x1, y1);
      g[2] = Pt2(x2, y1);

      if (convex_contains(g, course[0]) || convex_contains(g, course[1])) {
        ans = 0;
      }

      for (int i = 0; i < g.size(); i++) {
        ans = min(ans, dist(course, Line2(g[i], g[(i + 1) % g.size()]), h));
      }

      if (ans == 0)
        break;
    }
    cout.setf(ios::fixed);
    cout.precision(5);
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;

typedef double Num;
typedef complex<Num> Pt2;
typedef vector<Pt2> Polygon2;

struct Line2 : public vector<Pt2> {
  Line2(Pt2 a, Pt2 b) {
    push_back(a);
    push_back(b);
  }
};

const double eps = 1e-10;
Num dot(Pt2 a, Pt2 b) { return (a * conj(b)).real(); }
Num cross(Pt2 a, Pt2 b) { return (conj(a) * b).imag(); }
Pt2 vec(Line2 l) { return l[1] - l[0]; }

int ccw(Pt2 a, Pt2 b, Pt2 c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // ccw
  if (cross(b, c) < 0)
    return -1; // cw
  if (dot(b, c) < 0)
    return +2; // cab
  if (norm(b) < norm(c))
    return -2; // abc
  return 0;    // acb
}

int ccw(Line2 s, Pt2 p) { return ccw(s[0], s[1], p); }

bool intersectSP(Line2 s, Pt2 p) {
  return abs(s[0] - p) + abs(s[1] - p) < abs(s[1] - s[0]) + eps;
}

bool intersectSS(Line2 s, Line2 t) {
  return ccw(s, t[0]) * ccw(s, t[1]) <= 0 && ccw(t, s[0]) * ccw(t, s[1]) <= 0;
}

Pt2 projection(Line2 l, Pt2 p) {
  double t = dot(p - l[0], vec(l)) / norm(vec(l));
  return l[0] + t * (vec(l));
}

Pt2 reflection(Line2 l, Pt2 p) { return p + 2.0 * (projection(l, p) - p); }

Num distanceSP(Line2 s, Pt2 p) {
  Pt2 r = projection(s, p);
  if (intersectSP(s, r))
    return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

Num distanceSS(Line2 s, Line2 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])));
}

bool convex_contains(Polygon2 g, Pt2 p) {
  for (int i = 0; i < g.size(); i++) {
    if (ccw(Line2(g[i], g[(i + 1) % g.size()]), p) == -1)
      return false;
  }
  return true;
}

double dist(Line2 s, Line2 t, double h) {
  double d = distanceSS(s, t);
  if (d <= h)
    return d;
  return (d * d + h * h) / (2 * h);
}

int main() {
  int N;
  while (cin >> N, N) {
    Num x1, y1, x2, y2, h;
    cin >> x1 >> y1 >> x2 >> y2;
    Line2 course(Pt2(x1, y1), Pt2(x2, y2));

    double ans = 1e10;
    while (N--) {
      cin >> x1 >> y1 >> x2 >> y2 >> h;
      Polygon2 g(4);
      g[0] = Pt2(x1, y2);
      g[3] = Pt2(x2, y2);
      g[1] = Pt2(x1, y1);
      g[2] = Pt2(x2, y1);

      if (convex_contains(g, course[0]) || convex_contains(g, course[1])) {
        ans = 0;
      }

      for (int i = 0; i < g.size(); i++) {
        ans = min(ans, dist(course, Line2(g[i], g[(i + 1) % g.size()]), h));
      }
    }
    cout.setf(ios::fixed);
    cout.precision(5);
    cout << ans << endl;
  }
}