您的位置:首页 > 其它

bzoj 1337 最小圆覆盖

2015-06-03 15:36 309 查看
/**************************************************************
Problem: 1337
User: idy002
Language: C++
Result: Accepted
Time:4 ms
Memory:2372 kb
****************************************************************/

#include <cstdio>
#include <cmath>
#include <algorithm>
#define line(a,b) ((b)-(a))
#define eps 1e-10
#define N 100010
using namespace std;

int sg( double x ) { return (x>-eps)-(x<eps); }
struct Vector {
double x, y;
Vector( double x=0, double y=0 ):x(x),y(y){}
Vector operator+( const Vector &b ) const { return Vector(x+b.x,y+b.y); }
Vector operator-( const Vector &b ) const { return Vector(x-b.x,y-b.y); }
Vector operator*( double b ) const { return Vector(x*b,y*b); }
Vector operator/( double b ) const { return Vector(x/b,y/b); }
double operator^( const Vector &b ) const { return x*b.y-y*b.x; }
double len() { return sqrt(x*x+y*y); }
Vector nor() { return Vector(-y,x); }
};
typedef Vector Point;
Point inter( Point p, Vector u, Point q, Vector v ) {
return p+u*((line(p,q)^v)/(u^v));
}
struct Circle {
Point o;
double r;
Circle(){}
Circle( Point a ):o(a),r(0){}
Circle( Point a, Point b ) {
o = (a+b)/2;
r = (a-b).len()/2;
}
Circle( Point a, Point b, Point c ) {   //  ab^bc != 0
Point p=(a+b)/2, q=(b+c)/2;
Vector u=(a-b).nor(), v=(b-c).nor();
o = inter(p,u,q,v);
r = (o-a).len();
}
bool contain( Point a ) {
return sg( (a-o).len() - r ) <= 0;
}
};

int n;
Point pts
;

int main() {
scanf( "%d", &n );
for( int i=1; i<=n; i++ ) {
double x, y;
scanf( "%lf%lf", &x, &y );
pts[i] = Point(x,y);
}
random_shuffle( pts+1, pts+1+n );
Circle c = Circle(pts[1]);
for( int i=2; i<=n; i++ ) {
if( c.contain(pts[i]) ) continue;
c = Circle(pts[i]);
for( int j=1; j<i; j++ ) {
if( c.contain(pts[j]) ) continue;
c = Circle(pts[i],pts[j]);
for( int k=1; k<j; k++ ) {
if( c.contain(pts[k]) ) continue;
c = Circle(pts[i],pts[j],pts[k]);
}
}
}
printf( "%.3lf\n", c.r );
}


View Code

题解见bzoj 1336
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: