HDU - 3007 Buried memory

传送门

最小圆覆盖模板。

//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
const int N=507;
typedef long long LL;
typedef double db;
using namespace std;
const db eps=1e-10;
int n; db r;

template<typename T>void read(T &x)  {
    char ch=getchar(); x=0; T f=1;
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-') f=-1,ch=getchar();
    for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}

struct pt {
    db x,y;
    pt(){}
    pt(db x,db y):x(x),y(y){}
}p[N],c;

pt operator + (pt A,pt B) { return pt(A.x+B.x,A.y+B.y); }
pt operator - (pt A,pt B) { return pt(A.x-B.x,A.y-B.y); }
db dot(pt A,pt B) { return A.x*B.x+A.y*B.y; }
db length(pt A) { return sqrt(dot(A,A)); }
int dcmp(db x) { if(fabs(x)<eps) return 0; return x>0?1:-1; }

pt circle_center(pt a,pt b,pt c) {
    pt res;
    db a1=b.x-a.x,a2=c.x-a.x;
    db b1=b.y-a.y,b2=c.y-a.y;
    db c1=(a1*a1+b1*b1)/2.0;
    db c2=(a2*a2+b2*b2)/2.0;
    db d=a1*b2-a2*b1;
    res.x=a.x+(b2*c1-b1*c2)/d;
    res.y=a.y+(a1*c2-a2*c1)/d;
    return res;
}

void min_circle_cover(pt p[],int n,pt &c,db &r) {
    random_shuffle(p+1,p+n+1);
    c=p[0]; r=0;
    for(int i=2;i<=n;i++) {
        if(dcmp(length(p[i]-c)-r)>0) {
            c=p[i]; r=0;
            for(int j=1;j<i;j++) {
                if(dcmp(length(p[j]-c)-r)>0) {
                    c.x=(p[i].x+p[j].x)/2;
                    c.y=(p[i].y+p[j].y)/2;
                    r=length(p[i]-c); 
                    for(int k=1;k<j;k++) {
                        if(dcmp(length(p[k]-c)-r)>0) {
                            c=circle_center(p[i],p[j],p[k]);
                            r=length(p[k]-c);
                        } 
                    }
                }
            }
        }
    }
}

int main() {
    srand(998244353);
    for(;;) {
        read(n);
        if(!n) break;
        for(int i=1;i<=n;i++) 
            scanf("%lf %lf",&p[i].x,&p[i].y);
        min_circle_cover(p,n,c,r);
        printf("%.2lf %.2lf %.2lf\n",c.x,c.y,r);
    }
    return 0;
}
View Code

 

posted @ 2018-02-24 16:42  啊宸  阅读(166)  评论(0编辑  收藏  举报