HDU - 3932 Groundhog Build Home【爬山算法】

Groundhogs are good at digging holes, their home is a hole, usually a group of groundhogs will find a more suitable area for their activities and build their home at this area .xiaomi has grown up, can no longer live with its parents.so it needs to build its own home.xiaomi like to visit other family so much, at each visit it always start from the point of his own home.Xiaomi will visit all of the groundhogs’ home in this area(it will chose the linear distance between two homes).To save energy,xiaomi would like you to help it find where its home built,so that the longest distance between xiaomi’s home and the other groundhog’s home is minimum.

Input
The input consists of many test cases,ending of eof.Each test case begins with a line containing three integers X, Y, N separated by space.The numbers satisfy conditions: 1 <= X,Y <=10000, 1 <= N<= 1000. Groundhogs acivity at a rectangular area ,and X, Y is the two side of this rectangle, The number N stands for the number of holes.Then exactly N lines follow, each containing two integer numbers xi and yi (0 <= xi <= X, 0 <= yi <= Y) indicating the coordinates of one home.

Output
Print exactly two lines for each test case.The first line is the coordinate of xiaomi’s home which we help to find. The second line is he longest distance between xiaomi’s home and the other groundhog’s home.The output round to the nearest number with exactly one digit after the decimal point (0.05 rounds up to 0.1).

Sample Input
1000 50 1
10 10
1000 50 4
0 0
1 0
0 1
1 1

Sample Output
(10.0,10.0).
0.0
(0.5,0.5).
0.7

** 题目大意:给定一堆点,找到一个点的位置使这个点到所有点中的最大距离最小**

#include<bits/stdc++.h>
using namespace std;
#define maxn 100010
typedef long long ll;
struct point{
    double x,y;
} p[maxn],pp;
int n; double ans=1e10,X,Y;
double dis(point a,point b){
    return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int get(point x){
    double res=-1;
    int k;
    for(int i=1;i<=n;i++){
        double m=dis(p[i],x);
        if(m>res) res=m,k=i;
    }
    ans=min(ans,dis(x,p[k]));
    return k;
}
void hc(){
    double T=1.0,eps=1e-8;
    while(T>eps){
        int k=get(pp);
        pp.x=pp.x+(p[k].x-pp.x)*T;
        pp.y=pp.y+(p[k].y-pp.y)*T;
        T*=0.999;
    }
}
int  main(){
    double X,Y;
    while(cin>>X>>Y>>n){
        for(int i=1;i<=n;i++) cin>>p[i].x>>p[i].y;
        pp.x=X/2.0,pp.y=Y/2.0;
        ans=1e10;
        hc();
        printf("(%.1f,%.1f).\n",pp.x,pp.y);
        printf("%.1f\n",sqrt(ans));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值