首页 技术 正文
技术 2022年11月6日
0 收藏 727 点赞 414 浏览 3037 个字

Minimal Circle


Time Limit: 5 Seconds      Memory Limit: 32768 KB


You are to write a program to find a circle which covers a set of points and has the minimal area. There will be no more than 100 points in one problem.

Input

The input contains several problems. The first line of each problem is a line containing only one integer N which indicates the number of points to be covered. The next N lines contain N points. Each point is represented by x and y coordinates separated by a space. After the last problem, there will be a line contains only a zero.

Output

For each input problem, you should give a one-line answer which contains three numbers separated by spaces. The first two numbers indicate the x and y coordinates of the result circle, and the third number is the radius of the circle. (use escape sequence %.2f)

Sample Input

2
0.0 0.0
3 0
5
0 0
0 1
1 0
1 1
2 2
0

Sample Output

1.50 0.00 1.50
1.00 1.00 1.41


Source: Asia 1997, Shanghai (Mainland China)


裸题哦

注意求重心的方法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=;
const double INF=1e9;
const double eps=1e-;
const double pi=acos(-);
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}inline int sgn(double x){
if(abs(x)<eps) return ;
else return x<?-:;
}struct Vector{
double x,y;
Vector(double a=,double b=):x(a),y(b){}
bool operator <(const Vector &a)const{
return sgn(x-a.x)<||(sgn(x-a.x)==&&sgn(y-a.y)<);
}
void print(){printf("%lf %lf\n",x,y);}
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==&&sgn(a.y-b.y)==;}
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
double Len(Vector a){return sqrt(Dot(a,a));}
Vector Normal(Vector a){
return Vector(-a.y,a.x);//counterClockwise
};
struct Line{
Point s,t;
Line(){}
Line(Point a,Point b):s(a),t(b){}
};
bool isLSI(Line l1,Line l2){
Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
return sgn(Cross(v,u))!=sgn(Cross(v,w));
}
Point LI(Line a,Line b){
Vector v=a.s-b.s,v1=a.t-a.s,v2=b.t-b.s;
double t=Cross(v2,v)/Cross(v1,v2);
return a.s+v1*t;
}
Point Circumcenter(Point a,Point b,Point c){
Point p=(a+b)/,q=(a+c)/;
Vector v=Normal(b-a),u=Normal(c-a);
if(sgn(Cross(v,u))==){
if(sgn(Len(a-b)+Len(b-c)-Len(a-c))==) return (a+c)/;
if(sgn(Len(a-b)+Len(a-c)-Len(b-c))==) return (b+c)/;
if(sgn(Len(a-c)+Len(b-c)-Len(a-b))==) return (a+b)/;
}
return LI(Line(p,p+v),Line(q,q+u));
}double minCircleCover(Point p[],int n,Point &c){
random_shuffle(p+,p++n);
c=p[];
double r=;
for(int i=;i<=n;i++)
if(sgn(Len(c-p[i])-r)>){
c=p[i],r=;
for(int j=;j<i;j++)
if(sgn(Len(c-p[j])-r)>){
c=(p[i]+p[j])/,r=Len(c-p[i]);
for(int k=;k<j;k++)
if(sgn(Len(c-p[k])-r)>){
c=Circumcenter(p[i],p[j],p[k]);
r=Len(c-p[i]);
}
}
}
return r;
}int n;
Point p[N],c;
int main(int argc, const char * argv[]){
while(true){
n=read();if(n==) break;
for(int i=;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
double r=minCircleCover(p,n,c);
printf("%.2f %.2f %.2f\n",c.x,c.y,r);
}
}

HDU3932好像还有模拟退火做法

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902