模拟:随机增量法

BZOJ1336随机增量法解决最小圆覆盖

给出N个点,让你画一个最小的包含所有点的圆

假设圆O是前i-1个点得最小覆盖圆,加入第i个点,如果在圆内或边上则什么也不做。否,新得到的最小覆盖圆肯定经过第i个点

然后以第i个点为基础(半径为0),重复以上过程依次加入第j个点,若第j个点在圆外,则最小覆盖圆必经过第j个点。

重复以上步骤(因为最多需要三个点来确定这个最小覆盖圆,所以重复三次)

遍历完所有点之后,所得到的圆就是覆盖所有点得最小圆。

就喜欢这种题干特别容易明白的题了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cstdlib>
 6 #define eps 1e-8
 7 using namespace std;
 8 struct point{double x,y;}p[100005],o;
 9 int n;
10 double r;
11 inline double sqr(double x){return x*x;}
12 inline double dis(point a,point b)
13 {return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));}
14 inline bool cmp(double a,double b)
15 {return fabs(a-b)<eps;}
16 point geto(point a,point b,point c)
17 {
18       double a1,a2,b1,b2,c1,c2;
19       point ans;
20       a1=2*(b.x-a.x),b1=2*(b.y-a.y),c1=sqr(b.x)-sqr(a.x)+sqr(b.y)-sqr(a.y);
21       a2=2*(c.x-a.x),b2=2*(c.y-a.y),c2=sqr(c.x)-sqr(a.x)+sqr(c.y)-sqr(a.y);
22       if(cmp(a1,0)) 
23       { 
24            ans.y=c1/b1; 
25            ans.x=(c2-ans.y*b2)/a2; 
26       } 
27       else if(cmp(b1,0)) 
28       { 
29            ans.x=c1/a1; 
30            ans.y=(c2-ans.x*a2)/b2; 
31       } 
32       else
33       { 
34           ans.x=(c2*b1-c1*b2)/(a2*b1-a1*b2); 
35           ans.y=(c2*a1-c1*a2)/(b2*a1-b1*a2); 
36       } 
37       return ans; 
38 }
39 int main()
40 {
41     scanf("%d",&n);
42     for(int i=1;i<=n;i++)
43         scanf("%lf%lf",&p[i].x,&p[i].y);
44     for(int i=1;i<=n;i++)
45         swap(p[rand()%n+1],p[rand()%n+1]);
46     o=p[1];
47     for(int i=1;i<=n;i++)
48     {
49         if(dis(o,p[i])<r||cmp(dis(o,p[i]),r))continue;
50         o.x=(p[i].x+p[1].x)/2;o.y=(p[i].y+p[1].y)/2;r=dis(p[i],p[1])/2;
51         for(int j=2;j<i;j++)
52         {
53             if(dis(o,p[j])<r||cmp(dis(o,p[j]),r))continue;
54             o.x=(p[i].x+p[j].x)/2;o.y=(p[i].y+p[j].y)/2;r=dis(p[i],p[j])/2;
55             for(int k=1;k<j;k++)
56             {
57                 if(dis(o,p[k])<r||cmp(dis(o,p[k]),r))continue;
58                 o=geto(p[i],p[j],p[k]);
59                 r=dis(o,p[i]);
60             }
61         }
62     }
63     printf("%.10lf
%.10lf %.10lf",r,o.x,o.y);
64     //system("pause");
65     return 0;
66 }
原文地址:https://www.cnblogs.com/aininot260/p/9635757.html