bzoj 1337 最小圆覆盖

 1 /**************************************************************
 2     Problem: 1337
 3     User: idy002
 4     Language: C++
 5     Result: Accepted
 6     Time:4 ms
 7     Memory:2372 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <cmath>
12 #include <algorithm>
13 #define line(a,b) ((b)-(a))
14 #define eps 1e-10
15 #define N 100010
16 using namespace std;
17  
18 int sg( double x ) { return (x>-eps)-(x<eps); }
19 struct Vector {
20     double x, y;
21     Vector( double x=0, double y=0 ):x(x),y(y){}
22     Vector operator+( const Vector &b ) const { return Vector(x+b.x,y+b.y); }
23     Vector operator-( const Vector &b ) const { return Vector(x-b.x,y-b.y); }
24     Vector operator*( double b ) const { return Vector(x*b,y*b); }
25     Vector operator/( double b ) const { return Vector(x/b,y/b); }
26     double operator^( const Vector &b ) const { return x*b.y-y*b.x; }
27     double len() { return sqrt(x*x+y*y); }
28     Vector nor() { return Vector(-y,x); }
29 };
30 typedef Vector Point;
31 Point inter( Point p, Vector u, Point q, Vector v ) {
32     return p+u*((line(p,q)^v)/(u^v));
33 }
34 struct Circle {
35     Point o;
36     double r;
37     Circle(){}
38     Circle( Point a ):o(a),r(0){}
39     Circle( Point a, Point b ) {
40         o = (a+b)/2;
41         r = (a-b).len()/2;
42     }
43     Circle( Point a, Point b, Point c ) {   //  ab^bc != 0
44         Point p=(a+b)/2, q=(b+c)/2;
45         Vector u=(a-b).nor(), v=(b-c).nor();
46         o = inter(p,u,q,v);
47         r = (o-a).len();
48     }
49     bool contain( Point a ) {
50         return sg( (a-o).len() - r ) <= 0;
51     }
52 };
53  
54 int n;
55 Point pts[N];
56  
57 int main() {
58     scanf( "%d", &n );
59     for( int i=1; i<=n; i++ ) {
60         double x, y;
61         scanf( "%lf%lf", &x, &y );
62         pts[i] = Point(x,y);
63     }
64     random_shuffle( pts+1, pts+1+n );
65     Circle c = Circle(pts[1]);
66     for( int i=2; i<=n; i++ ) {
67         if( c.contain(pts[i]) ) continue;
68         c = Circle(pts[i]);
69         for( int j=1; j<i; j++ ) {
70             if( c.contain(pts[j]) ) continue;
71             c = Circle(pts[i],pts[j]);
72             for( int k=1; k<j; k++ ) {
73                 if( c.contain(pts[k]) ) continue;
74                 c = Circle(pts[i],pts[j],pts[k]);
75             }
76         }
77     }
78     printf( "%.3lf\n", c.r );
79 }
View Code

 

题解见bzoj 1336

posted @ 2015-06-03 15:36  idy002  阅读(162)  评论(0编辑  收藏  举报