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

#include <bits/stdc++.h>

using namespace std;

#define EPS (1e-5)
#define equals(a, b) (fabs(a - b) < EPS)

struct Point {
  double x, y;

  Point() {}
  Point(double x, double y) : x(x), y(y) {}

  Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }

  Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); }

  Point operator*(const double &k) const { return Point(x * k, y * k); }
};

typedef Point Vector;

double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }

double cross(const Point &a, const Point &b) { return a.x * b.y - b.x * a.y; }

double norm(const Point &p) { return dot(p, p); }

double abs(const Point &p) { return sqrt(norm(p)); }

struct Line {
  Point s, t;
  Line() {}
  Line(Point s, Point t) : s(s), t(t) {}
};

Line getPerpendicularBisector(const Point &a, const Point &b) {
  double cx = (a.x + b.x) / 2.0;
  double cy = (a.y + b.y) / 2.0;
  Point p = Point(cx + (a.y - b.y), cy + (b.x - a.x));
  return Line(Point(cx, cy), p);
}

Point crosspointLL(const Line &a, const Line &b) {
  Vector va = a.t - a.s, vb = b.t - b.s;
  double d = cross(vb, va);
  if (abs(d) < EPS)
    return b.s;
  return a.s + va * cross(vb, b.t - a.s) * (1.0 / d);
}

int main() {
  Point p[3];
  int N;
  cin >> N;
  while (N--) {
    for (int i = 0; i < 3; i++) {
      cin >> p[i].x >> p[i].y;
    }
    Line l[2];
    for (int i = 0; i < 2; i++) {
      l[i] = getPerpendicularBisector(p[i], p[i + 1]);
    }
    Point c = crosspointLL(l[0], l[1]);
    double r = abs(c - p[0]);
    printf("%.3f %.3f %.3f\\n", c.x, c.y, r);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define EPS (1e-5)
#define equals(a, b) (fabs(a - b) < EPS)

struct Point {
  double x, y;

  Point() {}
  Point(double x, double y) : x(x), y(y) {}

  Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }

  Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); }

  Point operator*(const double &k) const { return Point(x * k, y * k); }
};

double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }

double norm(const Point &p) { return dot(p, p); }

double abs(const Point &p) { return sqrt(norm(p)); }

typedef vector<Point> Polygon;

struct Circle {
  Point p;
  double r;
  Circle(Point p, double r) : p(p), r(r) {}
};

bool check(const Polygon &pg, const Point &p) {
  double r = abs(pg[0] - p);
  for (int i = 1; i < 3; i++) {
    if (!equals(r, abs(pg[i] - p)))
      return 0;
  }
  return 1;
}

Circle smallest_enclosing_circle(const Polygon &pg) {
  Point p(0, 0);
  int N = 3;
  for (int i = 0; i < (int)pg.size(); i++) {
    p = p + pg[i];
  }
  p = p * (1. / pg.size());
  double d = 0.5;
  while (!check(pg, p)) {
    for (int i = 0; i < 150; i++) {
      int s = 0;
      for (int j = 0; j < N; j++) {
        if (abs(pg[s] - p) - abs(pg[j] - p) < -EPS) {
          s = j;
        }
      }
      p = p + (pg[s] - p) * d;
    }
    d *= 0.5;
  }
  double r = abs(p - pg[0]);
  return Circle(p, r);
}

int main() {
  Polygon pg(3);
  int N;
  cin >> N;
  while (N--) {
    for (int i = 0; i < 3; i++) {
      cin >> pg[i].x >> pg[i].y;
    }
    auto c = smallest_enclosing_circle(pg);
    printf("%.3f %.3f %.3f\\n", c.p.x, c.p.y, c.r);
  }
  return 0;
}