HDU 3007

基本小圆覆盖模板题

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int X,Y; int n;
const double eps=0.00000001;
struct point{
    double x,y;
}p[550];

struct Circle{
    point cent;
    double r;
}cir;

double dist(point x, point y){
    double a=x.x-y.x;
    double b=x.y-y.y;
    return sqrt(a*a+b*b);
}

double triangleArea(point t1, point t2, point t3){
    point p1,p2;
    p1.x=t2.x-t1.x; p1.y=t2.y-t1.y;
    p2.x=t3.x-t1.x; p2.y=t3.y-t1.y;
    return fabs(p1.x*p2.y-p1.y*p2.x)/2;
}

Circle triangleCircle(point t1, point t2, point t3){
    Circle tmp;
    double a=dist(t1,t2);
    double b=dist(t2,t3);
    double c=dist(t3,t1);
    tmp.r=a*b*c/triangleArea(t1,t2,t3)/4;
    double xa,ya,xb,yb,xc,yc;
    double c1,c2;
    xa=t1.x; ya= t1.y;
    xb=t2.x; yb= t2.y;
    xc=t3.x; yc= t3.y;
    c1=(xa*xa+ya*ya-xb*xb-yb*yb)/2;
    c2=(xa*xa+ya*ya-xc*xc-yc*yc)/2;
    tmp.cent.x=(c1*(ya-yc)-c2*(ya-yb))/((xa-xb)*(ya-yc)-(xa-xc)*(ya-yb));
    tmp.cent.y=(c1*(xa-xc)-c2*(xa-xb))/((ya-yb)*(xa-xc)-(ya-yc)*(xa-xb));
    return tmp;
}

void slove(){
    random_shuffle(p,p+n);
    cir.cent=p[0];
    cir.r=0;
    for(int i=1;i<n;i++){
        if(dist(cir.cent,p[i])>cir.r){
            cir.cent=p[i]; cir.r=0;
            for(int j=0;j<i;j++){
                if(dist(cir.cent,p[j])>cir.r){
                    cir.cent.x=(p[i].x+p[j].x)/2;
                    cir.cent.y=(p[i].y+p[j].y)/2;
                    cir.r=dist(p[i],p[j])/2;
                    for(int k=0;k<j;k++){
                        if(dist(cir.cent,p[k])>cir.r){
                            cir=triangleCircle(p[i],p[j],p[k]);
                        }
                    }
                }
            }
        }
    }
}

int main (){
    while(scanf("%d",&n),n){
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        slove();
        printf("%0.2lf %0.2lf ",cir.cent.x,cir.cent.y);
        printf("%0.2lf\n",cir.r);
    }
    return 0;
}

  

HDU 3007

时间: 2024-08-01 17:44:07

HDU 3007的相关文章

hdu 3007 Buried memory 最远点对

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007 Each person had do something foolish along with his or her growth.But,when he or she did this that time,they could not predict that this thing is a mistake and they will want this thing would rather

最小圆覆盖 hdu 3007

今天学习了一下最小圆覆盖, 看了一下午都没看懂, 晚上慢慢的摸索这代码,接合着别人的讲解, 画着图跟着代码一步一步的走着,竟然有些理解了. 最小圆覆盖: 给定n个点, 求出半径最小的圆可以把这些点全部包围, 可以在圆的边界上 下面是我的个人理解. 如果不对, 还请路过大牛指出 先找一个点, 让圆心等于这个点的坐标, 半径等于0, 显然这个对的, 接着找下一个点, 如果只有两个点的话, 那么最小的圆一定是以他们为直径做圆, 接着找第三个点, 如果第三个点在园内或者在边界上, 那么不用更新当前的最小

HDU 3007 Buried memory 最小圆覆盖

题目大意:没看.反正就是求最小圆覆盖. 思路:一个神奇的算法--随机增量法.可以证明,这个算法可以在O(n)的时间复杂度内求出最小圆覆盖.虽然好像能卡掉的样子,但是加上一句random_shuffle就卡不掉了. 具体的过程是这样的: 在全局记录一个圆,表示目前的最小圆覆盖.从头开始扫描.遇到第一个不在当前最小圆覆盖内的点的时候: 将这个点与当前最小圆覆盖的圆心为直径做一个圆,作为当前的最小圆覆盖.从头开始扫描.遇到第一个不在当前最小圆覆盖的点的时候: 将刚才的两个点和当前点做三角形,将这个三角

ZOJ 1450 HDU 3007 (最小圆覆盖)

首先这个圆边上必有至少两点,打乱数组,然后利用枚举,不断重新定义圆,找出最小的圆 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int N = 100005; const double eps = 1e-8; int n; struct Point { double x, y; Point()

hdu 3007【最小圆覆盖-随机增量法】

#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; const int N=505; int n; double r; struct dian { double x,y; dian(double X=0,double Y=0) { x=X,y=Y; } dian operator + (const dian &a) c

HDU 3007 模拟退火算法

Buried memory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4067    Accepted Submission(s): 2171 Problem Description Each person had do something foolish along with his or her growth.But,when

计算几何学习8

由于poj炸了 而题单上有很多poj的题 就先开始第二部分了 学习了两个固定算法 最小圆覆盖和平面上最近点对 平面上最近点对采用的是分治的思想 把一个x有序的序列分成A,B左右两部分 当得到A内最近点对距离,B类最近点对距离后 先更新大序列的答案ans A,B间最近点对的产生 显然在x坐标距离mid点不超过ans的区间内产生 我们把这段区间拿出来,对y排序 再逐对更新 值得注意的地方 1)为了确保速度 可以在第二次对y排序的时候采取对标号排序的方式 2)在枚举点对更新的时候注意在y差值 >= a

计算几何题目分类

转载 一.基础题目 1.1 有固定算法的题目 A, 最近点对问题最近点对问题的算法基于扫描线算法.ZOJ 2107    Quoit Design    典型最近点对问题POJ    3714    Raid    变种最近点对问题 B,最小包围圆最小包围圆的算法是一种增量算法,期望是O(n).ZOJ    1450    Minimal Circle  HDU    3007    Buried memory C,旋转卡壳POJ 3608    Bridge Across Islands   

hdu 3006 The Number of set(思维+壮压DP)

The Number of set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1056    Accepted Submission(s): 655 Problem Description Given you n sets.All positive integers in sets are not less than 1 and