最小圆覆盖模板

 1 #include<bits/stdc++.h>
 2 #define ll long long 
 3 #define rep(i,a,b) for(int i=a;i<=b;i++)
 4 #define rrp(i,a,b) for(int i=b;i>=a;i--)
 5 #define dbg(x) cout<<#x<<"=="<<x<<endl 
 6 #define eps 1e-8
 7 using namespace std;
 8 const int maxn=5e5+5;
 9 int n;
10 double R,r;
11 struct Point{
12     double x,y;
13 }p[maxn],ans;
14 int sgn(double x){
15     if(fabs(x)<eps) return 0;
16     else return x<0?-1:1;
17 }
18 double Distance(Point A,Point B){
19     return hypot(A.x-B.x,A.y-B.y);
20 }
21 Point circle_center(const Point a,const Point b,const Point c){
22     Point center;
23     double a1=b.x-a.x,b1=b.y-a.y,c1=(a1*a1+b1*b1)/2;
24     double a2=c.x-a.x,b2=c.y-a.y,c2=(a2*a2+b2*b2)/2;
25     double d=a1*b2-a2*b1;
26     center.x=a.x+(c1*b2-c2*b1)/d;
27     center.y=a.y+(a1*c2-a2*c1)/d;
28     return center;
29 }
30 void min_cover_circle(Point *p,int n,Point &c,double &r){
31     random_shuffle(p+1,p+1+n);
32     c=p[0];r=0;
33     for(int i=1;i<=n;i++){
34         if(sgn(Distance(p[i],c)-r)>0){
35             c=p[i];r=0;
36             for(int j=0;j<i;j++){
37                 if(sgn(Distance(p[j],c)-r)>0){
38                     c.x=(p[i].x+p[j].x)/2;
39                     c.y=(p[i].y+p[j].y)/2;
40                     r=Distance(p[j],c);
41                     for(int k=0;k<j;k++){
42                         if(sgn(Distance(p[k],c)-r)>0){
43                             c=circle_center(p[i],p[j],p[k]);
44                             r=Distance(p[i],c);
45                         }
46                     }
47                 }
48             }
49         }
50     }
51     
52 }
53 int main()
54 {   
55     freopen("robots.in","r",stdin);
56     int t;
57     scanf("%d",&t);
58     while(t--){
59         scanf("%d%d%d",&n,&R,&r);
60         p[0]={0,0};
61         rep(i,1,n){
62             scanf("%lf%lf",&p[i].x,&p[i].y);
63             p[i].x=p[i-1].x+p[i].x; 
64             p[i].y=p[i-1].y+p[i].y;
65         }
66         min_cover_circle(p,n,ans,r);
67         cout<<fixed<<setprecision(10)<<-ans.x<<" "<<-ans.y<<endl;
68     }
69     return 0; 
70 }

 

posted @ 2021-04-14 23:32  JamZF  阅读(42)  评论(0编辑  收藏  举报