Problem p02805 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
const long long mod = 1000000007;
// const long long mod = 998244353;
double dist(double x1, double y1, double x2, double y2) {
  return sqrt(((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)));
}

double R(double x1, double y1, double x2, double y2, int *xs, int *ys, int N) {
  double cx = (x1 + x2) / 2;
  double cy = (y1 + y2) / 2;
  double r = dist(x1, y1, x2, y2) / 2;
  for (int i = 0; i < N; i++) {
    if (dist(xs[i], ys[i], cx, cy) > r * (1.0000001)) {
      return -1;
    }
  }
  return r;
}
double R(double x1, double y1, double x2, double y2, double x3, double y3,
         int *xs, int *ys, int N) {
  double a = x1 - x2;
  double b = y1 - y2;
  double c = x2 - x3;
  double d = y2 - y3;
  double l1 = (x1 * x1 + y1 * y1);
  double l2 = (x2 * x2 + y2 * y2);
  double l3 = (x3 * x3 + y3 * y3);
  double v1 = l1 - l2;
  double v2 = l2 - l3;
  double q = a * d - c * b;
  if (q == 0)
    return -1;
  double cx = (d * v1 - b * v2) / (2 * q);
  double cy = (-c * v1 + a * v2) / (2 * q);
  double r = sqrt((x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy));
  // printf("%f %f %f \n", cx, cy, r);
  for (int i = 0; i < N; i++) {
    if (dist(xs[i], ys[i], cx, cy) > r * (1.0000001)) {
      return -1;
    }
  }
  return r;
}

int main() {
  int N;
  scanf("%d", &N);
  int *x = new int[N];
  int *y = new int[N];
  for (int i = 0; i < N; i++)
    scanf("%d %d", x + i, y + i);
  double min = 200000;
  if (N == 2) {
    min =
        sqrt((x[0] - x[1]) * (x[0] - x[1]) + (y[0] - y[1]) * (y[0] - y[1])) / 2;
  } else {
    for (int i1 = 0; i1 < N; i1++) {
      for (int i2 = i1 + 1; i2 < N; i2++) {
        for (int i3 = i2 + 1; i3 < N; i3++) {
          double r = R(x[i1], y[i1], x[i2], y[i2], x[i3], y[i3], x, y, N);
          // printf("%f\n", r);
          if (r != -1) {
            min = r < min ? r : min;
          }
        }
        double r = R(x[i1], y[i1], x[i2], y[i2], x, y, N);
        if (r != -1) {
          min = r < min ? r : min;
        }
      }
    }
  }
  printf("%lf", min);
  while (1)
    getchar();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
const long long mod = 1000000007;
// const long long mod = 998244353;
double dist(double x1, double y1, double x2, double y2) {
  return sqrt(((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)));
}

double R(double x1, double y1, double x2, double y2, int *xs, int *ys, int N) {
  double cx = (x1 + x2) / 2;
  double cy = (y1 + y2) / 2;
  double r = dist(x1, y1, x2, y2) / 2;
  for (int i = 0; i < N; i++) {
    if (dist(xs[i], ys[i], cx, cy) > r * (1.0000001)) {
      return -1;
    }
  }
  return r;
}
double R(double x1, double y1, double x2, double y2, double x3, double y3,
         int *xs, int *ys, int N) {
  double a = x1 - x2;
  double b = y1 - y2;
  double c = x2 - x3;
  double d = y2 - y3;
  double l1 = (x1 * x1 + y1 * y1);
  double l2 = (x2 * x2 + y2 * y2);
  double l3 = (x3 * x3 + y3 * y3);
  double v1 = l1 - l2;
  double v2 = l2 - l3;
  double q = a * d - c * b;
  if (q == 0)
    return -1;
  double cx = (d * v1 - b * v2) / (2 * q);
  double cy = (-c * v1 + a * v2) / (2 * q);
  double r = sqrt((x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy));
  // printf("%f %f %f \n", cx, cy, r);
  for (int i = 0; i < N; i++) {
    if (dist(xs[i], ys[i], cx, cy) > r * (1.0000001)) {
      return -1;
    }
  }
  return r;
}

int main() {
  int N;
  scanf("%d", &N);
  int *x = new int[N];
  int *y = new int[N];
  for (int i = 0; i < N; i++)
    scanf("%d %d", x + i, y + i);
  double min = 200000;
  if (N == 2) {
    min =
        sqrt((x[0] - x[1]) * (x[0] - x[1]) + (y[0] - y[1]) * (y[0] - y[1])) / 2;
  } else {
    for (int i1 = 0; i1 < N; i1++) {
      for (int i2 = i1 + 1; i2 < N; i2++) {
        for (int i3 = i2 + 1; i3 < N; i3++) {
          double r = R(x[i1], y[i1], x[i2], y[i2], x[i3], y[i3], x, y, N);
          // printf("%f\n", r);
          if (r != -1) {
            min = r < min ? r : min;
          }
        }
        double r = R(x[i1], y[i1], x[i2], y[i2], x, y, N);
        if (r != -1) {
          min = r < min ? r : min;
        }
      }
    }
  }
  printf("%lf", min);
  return 0;
}