题目描述:
给出n个点,然后求最小覆盖掉它们的圆的坐标及半径
题目分析:
裸的最小圆覆盖问题,当练手题目了,但为啥在凸包的分类里(差评)
使用随机增量法,期望 O(n)
题目链接:
代码实现:
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <cstdlib>
const int maxm=1e6+100;
const double eps=1e-9;
struct Point{
double x,y;
int tim;
};
int n;
Point a[maxm],o;
double r;
double distance(Point p1,Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
void makecir(Point p1,Point p2,Point p3)//三点确定一个圆
{
double x1=2*(p2.x-p1.x),y1=2*(p2.y-p1.y);
double c1=p2.x*p2.x+p2.y*p2.y-p1.x*p1.x-p1.y*p1.y;
double x2=2*(p3.x-p1.x),y2=2*(p3.y-p1.y);
double c2=p3.x*p3.x+p3.y*p3.y-p1.x*p1.x-p1.y*p1.y;
o.x=(y1*c2-y2*c1)/(y1*x2-y2*x1);
o.y=(x2*c1-x1*c2)/(y1*x2-y2*x1);
r=distance(o,p1);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
std::random_shuffle(a+1,a+n+1);
o=a[1],r=0;
for(int i=2;i<=n;i++)
if(distance(o,a[i])>r+eps)
{
o=a[i],r=0;
for(int j=1;j<=i-1;j++)
if(distance(o,a[j])>r+eps)
{
o.x=(a[i].x+a[j].x)/2;
o.y=(a[i].y+a[j].y)/2;
r=distance(o,a[j]);
for(int k=1;k<=j-1;k++)
if(distance(o,a[k])>r+eps)
makecir(a[i],a[j],a[k]);
}
}
printf("%.2lf %.2lf %.2lf\n",o.x,o.y,r);
return 0;
}