码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 1337 最小圆覆盖 随机增量法

时间:2015-04-17 18:21:00      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:bzoj   bzoj1337   随机增量法   

题目大意:求最小圆覆盖

我又写了一遍233

尼玛上一遍居然忘记random_shuffle了= =

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define M 100100
#define EPS 1e-7
using namespace std;
struct Point{
	double x,y;
	Point() {}
	Point(double _,double __):
		x(_),y(__) {}
	friend istream& operator >> (istream &_,Point &p)
	{
		scanf("%lf%lf",&p.x,&p.y);
		return _;
	}
	friend Point operator + (const Point &p1,const Point &p2)
	{
		return Point(p1.x+p2.x,p1.y+p2.y);
	}
	friend Point operator - (const Point &p1,const Point &p2)
	{
		return Point(p1.x-p2.x,p1.y-p2.y);
	}
	friend double operator * (const Point &p1,const Point &p2)
	{
		return p1.x*p2.y-p1.y*p2.x;
	}
	friend Point operator * (const Point &p1,double rate)
	{
		return Point(p1.x*rate,p1.y*rate);
	}
	friend double Distance(const Point &p1,const Point &p2)
	{
		return sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) ) ;
	}
	friend Point Rotate(const Point &p)
	{
		return Point(-p.y,p.x);
	}
}points[M];
struct Line{
	Point p,v;
	Line() {}
	Line(const Point &_,const Point &__):
		p(_),v(__) {}
	friend Point Get_Intersection(const Line &l1,const Line &l2)
	{
		Point u=l1.p-l2.p;
		double temp=(l2.v*u)/(l1.v*l2.v);
		return l1.p+l1.v*temp;
	}
};
struct Circle{
	Point o;
	double r;
	Circle() {}
	Circle(const Point &_,double __):
		o(_),r(__) {}
	bool In_Circle(const Point &p)
	{
		return Distance(p,o) < r + EPS ;
	}
}ans;
int n;
int main()
{
	int i,j,k;
	srand(19980402);
	cin>>n;
	for(i=1;i<=n;i++)
		cin>>points[i];
	random_shuffle(points+1,points+n+1);
	for(i=1;i<=n;i++)
	{
		if(!ans.In_Circle(points[i]))
		{
			ans=Circle(points[i],0);
			for(j=1;j<i;j++)
				if(!ans.In_Circle(points[j]))
				{
					ans=Circle((points[i]+points[j])*0.5,Distance(points[i],points[j])*0.5);
					for(k=1;k<j;k++)
						if(!ans.In_Circle(points[k]))
						{
							Line l1=Line((points[i]+points[j])*0.5,Rotate(points[i]-points[j]));
							Line l2=Line((points[i]+points[k])*0.5,Rotate(points[i]-points[k]));
							Point O=Get_Intersection(l1,l2);
							ans=Circle(O,Distance(points[i],O));
						}
				}
		}
	}
	cout<<fixed<<setprecision(3)<<ans.r<<endl;
	return 0;
}


BZOJ 1337 最小圆覆盖 随机增量法

标签:bzoj   bzoj1337   随机增量法   

原文地址:http://blog.csdn.net/popoqqq/article/details/45098399

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!