标签:
增量法最小圆覆盖,简单模版
3 1.00 1.00 2.00 2.00 3.00 3.00 0
2.00 2.00 1.41
/* *********************************************** Author :CKboss Created Time :2014年12月29日 星期一 15时15分46秒 File Name :HDOJ3007.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; const int maxn = 888; const double eps=1e-8; struct Point { double x,y; }pt[maxn]; Point operator+(Point A,Point B) { return (Point){A.x+B.x,A.y+B.y}; } Point operator-(Point A,Point B) { return (Point){A.x-B.x,A.y-B.y}; } Point operator*(Point A,double p) { return (Point){A.x*p,A.y*p}; } Point operator/(Point A,double p) { return (Point){A.x/p,A.y/p}; } int n; int dcmp(double x) { if(fabs(x)<eps) return 0; if(x>eps) return 1; else return -1; } double Cross(Point A,Point B) { return A.x*B.y-A.y*B.x; } double Dot(Point a,Point b) { return a.x*b.x+a.y*b.y; } double Length(Point a) { return sqrt(Dot(a,a)); } struct Circle { Point c; double r; }; Circle CircumscribedCircle(Point p1,Point p2,Point p3) { double Bx=p2.x-p1.x,By=p2.y-p1.y; double Cx=p3.x-p1.x,Cy=p3.y-p1.y; double D=2*(Bx*Cy-By*Cx); double cx=(Cy*(Bx*Bx+By*By)-By*(Cx*Cx+Cy*Cy))/D+p1.x; double cy=(Bx*(Cx*Cx+Cy*Cy)-Cx*(Bx*Bx+By*By))/D+p1.y; Circle c; c.c=(Point){cx,cy}; c.r=Length(p1-c.c); return c; } void min_cover_circle(Point p[],int n,Circle& c) { c.c=p[0]; c.r=0; for(int i=1;i<n;i++) { if(dcmp(Length(p[i]-c.c)-c.r)>0) { c.c=p[i]; c.r=0; for(int j=0;j<i;j++) { if(dcmp(Length(p[j]-c.c)-c.r)>0) { c.c=(Point){(p[i].x+p[j].x)/2,(p[i].y+p[j].y)/2}; c.r=Length(p[j]-p[i])/2.; for(int k=0;k<j;k++) { if(dcmp(Length(p[k]-c.c)-c.r)>0) { c=CircumscribedCircle(p[i],p[j],p[k]); } } } } } } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d",&n)!=EOF&&n) { for(int i=0;i<n;i++) scanf("%lf%lf",&pt[i].x,&pt[i].y); Circle c; min_cover_circle(pt,n,c); printf("%.2lf %.2lf %.2lf\n",c.c.x,c.c.y,c.r); } return 0; }
HDOJ 3007 Buried memory 增量法最小圆覆盖
标签:
原文地址:http://blog.csdn.net/ck_boss/article/details/42240017