BZOJ1337: 最小圆覆盖

题目:求n个点的最小圆覆盖。

题解:最小圆覆盖,上模板。复杂度证明可以戳:这里

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 1000000+5
14 #define maxm 200000+5
15 #define eps 1e-6
16 #define ll long long
17 #define ull unsigned long long
18 #define pa pair<int,int>
19 #define for0(i,n) for(int i=0;i<=(n);i++)
20 #define for1(i,n) for(int i=1;i<=(n);i++)
21 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
22 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
23 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
24 #define for5(n,m) for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
25 #define mod 1000000007
26 #define lch k<<1,l,mid
27 #define rch k<<1|1,mid+1,r
28 #define sqr(x) (x)*(x)
29 #define db double
30 using namespace std;
31 inline int read()
32 {
33     int x=0,f=1;char ch=getchar();
34     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
35     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
36     return x*f;
37 }
38 struct point
39 {
40     db x,y;
41     point operator +(point b){return (point){x+b.x,y+b.y};}
42     point operator /(db b){return (point){x/b,y/b};}
43 }a[maxn];
44 int n;
45 db ans;
46 inline db dist(point a,point b){return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));}
47 inline point calc(db a,db b,db c,db d,db e,db f)
48 {
49   return (point){(c*e-f*b)/(a*e-d*b),(a*f-c*d)/(a*e-d*b)};
50 }
51 inline point get(point a,point b,point c)
52 {
53    return calc(b.x-a.x,b.y-a.y,(sqr(b.x)+sqr(b.y)-sqr(a.x)-sqr(a.y))/2.0,
54         c.x-b.x,c.y-b.y,(sqr(c.x)+sqr(c.y)-sqr(b.x)-sqr(b.y))/2.0);
55 }
56 int main()
57 {
58    freopen("input.txt","r",stdin);
59    freopen("output.txt","w",stdout);
60    n=read();
61    for1(i,n)scanf("%lf%lf",&a[i].x,&a[i].y);
62    for1(i,n)swap(a[rand()%n+1],a[rand()%n+1]);
63    ans=0;
64    for1(i,n)if(dist(a[i],a[0])>ans+eps)
65    {
66         a[0]=a[i];ans=0;
67         for1(j,i-1)if(dist(a[j],a[0])>ans+eps)
68         {
69             a[0]=(a[i]+a[j])/2;ans=dist(a[0],a[i]);
70             for1(k,j-1)if(dist(a[k],a[0])>ans+eps)
71             {
72                 a[0]=get(a[i],a[j],a[k]);ans=dist(a[0],a[i]);
73             }
74         }
75    }
76    printf("%.2f %.2f %.2f\n",a[0].x,a[0].y,ans);
77    return 0;
78 }
View Code

我不会说告诉你这题是三倍经验的

posted @ 2015-03-30 22:24 ZYF-ZYF Views(...) Comments(...) Edit 收藏