#include<iostream>
#include<cstdio>
#include<cmath>
#include <algorithm>
#define eps 1e-8
using namespace std;
const int MAX=5000;
int n;
double a[MAX][3];
struct Point {
double x,y;
};
int sgn(double x) {
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
double Distance (Point A, Point B) {
return hypot(A.x-B.x,A.y-B.y);
}
Point circle_center(const Point a, const Point b,const Point c) {
Point center;
double a1=b.x-a.x, b1=b.y-a.y, c1=(a1*a1+b1*b1)/2;
double a2=c.x-a.x, b2=c.y-a.y, c2=(a2*a2+b2*b2)/2;
double d=a1*b2-a2*b1;
center.x=a.x+(c1*b2-c2*b1)/d;
center.y=a.y+(a1*c2-a2*c1)/d;
return center;
}
void min_cover_circle(Point* p, int nn, Point& c, double& r) {
random_shuffle(p, p+nn);
c=p[0];
r=0;
for(int i=1; i<nn; i++)
if(sgn(Distance(p[i],c)-r)>0) {
c=p[i];
r=0;
for(int j=0; j<i; j++)
if(sgn(Distance(p[j],c)-r)>0) {
c.x=(p[i].x+p[j].x)/2;
c.y=(p[i].y+p[j].y)/2;
r=Distance(p[j],c);
for(int k=0; k<j; k++)
if(sgn(Distance(p[k],c)-r)>0) {
c=circle_center(p[i],p[j],p[k]);
r=Distance(p[i],c);
}
}
}
}
double f1() {
double ans;
Point p[n];
Point c;
double r;
for(int i=0; i<n; i++) {
p[i].x=a[i][0];
p[i].y=a[i][2];
}
// for(int i=0; i<n; i++) {
// printf("x = %0.10lf, y = %0.10lf\n",p[i].x,p[i].y);
// }
min_cover_circle(p,n,c,r);
ans=r*2;
// printf("r = %0.10lf\n",r);
return ans;
}
double f2() {
double ans;
Point p[n];
Point c;
double r;
for(int i=0; i<n; i++) {
p[i].x=a[i][1];
p[i].y=a[i][2];
}
// for(int i=0; i<n; i++) {
// printf("x = %0.10lf, y = %0.10lf\n",p[i].x,p[i].y);
// }
min_cover_circle(p,n,c,r);
ans=r*2;
return ans;
}
double f3() {
double ans;
Point p[n];
Point c;
double r;
for(int i=0; i<n; i++) {
p[i].x=a[i][0];
p[i].y=a[i][1];
}
// for(int i=0; i<n; i++) {
// printf("x = %0.10lf, y = %0.10lf\n",p[i].x,p[i].y);
// }
min_cover_circle(p,n,c,r);
ans=r*2;
return ans;
}
int main() {
while(~scanf("%d",&n)) {
for(int i=0; i<n; i++) {
for(int j=0; j<3; j++) {
scanf("%lf",&a[i][j]);
}
}
double ans=f1();
double tmp1=f2();
double tmp2=f3();
if(ans>tmp1) {
ans=tmp1;
}
if(ans>tmp2) {
ans=tmp2;
}
printf("%.10lf\n",ans);
}
return 0;
}
最小圆覆盖 ( C++ )
最新推荐文章于 2022-03-14 11:09:22 发布
本文介绍了一种基于随机化和优化的算法,用于在一个三维点集中找到最小覆盖圆。通过`min_cover_circle`函数,计算并更新圆心和半径,以实现对点集的有效覆盖。主要涉及了距离计算、点集操作和圈心计算等技术。
摘要由CSDN通过智能技术生成