【BZOJ-1336&1337】Alie最小圆覆盖 最小圆覆盖(随机增量法)

时间:2021-08-07 14:26:54

1336: [Balkan2002]Alien最小圆覆盖

Time Limit: 1 Sec  Memory Limit: 162 MBSec  Special Judge
Submit: 1573  Solved: 697
[Submit][Status][Discuss]

Description

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

Input

先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000.0)

Output

输出圆的半径,及圆心的坐标

Sample Input

6
8.0 9.0
4.0 7.5
1.0 2.0
5.1 8.7
9.0 2.0
4.5 1.0

Sample Output

5.00
5.00 5.00

HINT

Source

1337: 最小圆覆盖

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 897  Solved: 437
[Submit][Status][Discuss]

Description

给出平面上N个点,N<=10^5.请求出一个半径最小的圆覆盖住所有的点

Input

第一行给出数字N,现在N行,每行两个实数x,y表示其坐标.

Output

输出最小半径,输出保留三位小数.

Sample Input

4
1 0
0 1
0 -1
-1 0

Sample Output

1.000

HINT

Source

Solution

最小圆覆盖裸题,随机增量法

这道题有个需要注意的地方,输出的时候不要只输出2位小数,可能会WA,可以考虑直接输出

直接把课件黏上来= =

【BZOJ-1336&1337】Alie最小圆覆盖         最小圆覆盖(随机增量法)

【BZOJ-1336&1337】Alie最小圆覆盖         最小圆覆盖(随机增量法)

Code

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#include<string>
#include<bitset>
#include<iomanip>
#define INF 1000000000
#define fi first
#define se second
#define N 100005
#define MP(x,y) make_pair(x,y)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef long double Double; struct Vector
{
double x,y;
Vector(double X=,double Y=) {x=X,y=Y;}
};
typedef Vector Point;
typedef vector<Point> Polygon;
const double eps=1e-;
const double pi=acos(-1.0);
struct Line
{
Point P;
Vector v;
double ang;
Line() {}
Line(Point P,Vector v):P(P),v(v) {ang=atan2(v.y,v.x);}
bool operator<(const Line &L) const {return ang<L.ang;}
};
int dcmp(double x) {if(fabs(x)<eps) return ; else return x<? -:;}
Vector operator + (Vector A,Vector B) {return ((Vector){A.x+B.x,A.y+B.y});}
Vector operator - (Vector A,Vector B) {return ((Vector){A.x-B.x,A.y-B.y});}
Vector operator * (Vector A,double p) {return ((Vector){A.x*p,A.y*p});}
Vector operator / (Vector A,double p) {return ((Vector){A.x/p,A.y/p});}
bool operator < (const Vector& a,const Vector& b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);}
bool operator == (const Vector& a,const Vector& b) {return dcmp(a.x-b.x)==&&dcmp(a.y-b.y)==;}
double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
double Len(Vector A) {return sqrt(Dot(A,A));}
double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
Vector Rotate(Vector A,double rad) {return ((Vector){A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad)});}
Point GLI(Point P,Vector v,Point Q,Vector w) {Vector u=P-Q; double t=Cross(w,u)/Cross(v,w); return P+v*t;}
Point GLI(Line a,Line b) {Vector u=a.P-b.P; double t=Cross(b.v,u)/Cross(a.v,b.v);return a.P+a.v*t;}
Point Center_of_gravity(Point A,Point B,Point C)
{
Point P=(A+B)/,Q=(A+C)/;
Vector v=Rotate(B-A,pi/),w=Rotate(C-A,pi/);
if(dcmp(Len(Cross(v,w)))==)//这是三点一线的情况
{
if(dcmp(Len(A-B)+Len(B-C)-Len(A-C))==)
return (A+C)/;
if(dcmp(Len(A-C)+Len(B-C)-Len(A-B))==)
return (A+B)/;
if(dcmp(Len(A-B)+Len(A-C)-Len(B-C))==)
return (B+C)/;
}
return GLI(P,v,Q,w);
}
double Min_Cover_Circle(Point *p,int n,Point &c)
{
random_shuffle(p,p+n);
c=p[];
double r=;
int i,j,k;
for(i=;i<n;i++)
if(dcmp(Len(c-p[i])-r)>)
{
c=p[i],r=;
for(j=;j<i;j++)
if(dcmp(Len(c-p[j])-r)>)
{
c=(p[i]+p[j])/;
r=Len(c-p[i]);
for(k=;k<j;k++)
if(dcmp(Len(c-p[k])-r)>)
{
c=Center_of_gravity(p[i],p[j],p[k]);
r=Len(c-p[i]);
}
}
}
return r;
}
#define MAXN 100010
Point Po[MAXN];
int main()
{
srand();
int n; scanf("%d",&n);
for (int i=; i<=n; i++)
{
double x,y; scanf("%lf%lf",&x,&y);
Po[i-]=Point(x,y);
}
Point c;
printf("%lf\n",Min_Cover_Circle(Po,n,c));
printf("%lf %lf",c.x,c.y);
return ;
}