HDU 3932(计算几何+最小圆覆盖)

来源:互联网 发布:java 判断是不是汉字 编辑:程序博客网 时间:2024/04/17 02:59

问题描述:

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 110 101000 50 40 01 00 11 1
Sample Output

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

题目题意:题目给我们一个最大的范围(是一个矩形)的俩条边(我也不知这个有撒用,反正我没用大笑),然后给了N个点的坐标,让我们求一个点使得到达这N个点的最大距离最小,输出点的坐标和距离。

题目分析:这个题目我们可以抽象成我们要求一个最小的圆能覆盖所有的点,经典的最小圆覆盖问题(居然15ms过了,是不是撞上死耗子了).


代码如下:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;struct  Point{    double x,y;};struct Point a[1005],d;double r;double  get_dis(Point p1,Point  p2)   //两点间距离{    return (sqrt((p1.x-p2.x)*(p1.x -p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));}double get_muti(Point p1, Point p2,Point p0){    return   ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));}void get_o(Point p,Point q,int n){    d.x=(p.x+q.x)/2.0;    d.y=(p.y+q.y)/2.0;    r=get_dis(p,q)/2;    int k;    double c1,c2,t1,t2,t3;    for(k=1;k<=n;k++) {        if(get_dis(d,a[k])<=r)continue;        if(get_muti(p,q,a[k])!=0.0) {            c1=(p.x*p.x+p.y*p.y-q.x*q.x-q.y*q.y)/2.0;            c2=(p.x*p.x+p.y*p.y-a[k].x*a[k].x-a[k].y*a[k].y)/2.0;            d.x=(c1*(p.y-a[k].y)-c2*(p.y-q.y))/((p.x-q.x)*(p.y-a[k].y)-(p.x-a[k].x)*(p.y-q.y));            d.y=(c1*(p.x-a[k].x)-c2*(p.x-q.x))/((p.y-q.y)*(p.x-a[k].x)-(p.y-a[k].y)*(p.x-q.x));            r=get_dis(d,a[k]);        }        else {            t1=get_dis(p,q);            t2=get_dis(q,a[k]);            t3=get_dis(p,a[k]);            if(t1>=t2&&t1>=t3) {                d.x=(p.x+q.x)/2.0;                d.y=(p.y+q.y)/2.0;r=get_dis(p,q)/2.0;            }            else if(t2>=t1&&t2>=t3) {                d.x=(a[k].x+q.x)/2.0;                 d.y=(a[k].y+q.y)/2.0;                 r=get_dis(a[k],q)/2.0;            }            else {                d.x=(a[k].x+p.x)/2.0;                d.y=(a[k].y+p.y)/2.0;                r=get_dis(a[k],p)/2.0;            }        }    }}void solve(Point pi,int n){    d.x=(pi.x+a[1].x)/2.0;    d.y=(pi.y+a[1].y)/2.0;    r=get_dis(pi,a[1])/2.0;    int j;    for(j=2;j<=n;j++){    if(get_dis(d,a[j])<=r)continue;        else            get_o(pi,a[j],j-1);    }}int main(){    double x,y;    int n;    while (scanf("%lf%lf%d",&x,&y,&n)!=EOF) {        for(int i=1;i<=n;i++){            scanf("%lf %lf",&a[i].x,&a[i].y);        }        if(n==1)   { printf("(%.1lf,%.1lf).\n0.0\n",a[1].x,a[1].y);continue;}        r=get_dis(a[1],a[2])/2.0;        d.x=(a[1].x+a[2].x)/2.0;        d.y=(a[1].y+a[2].y)/2.0;        for(int i=3;i<=n;i++){            if(get_dis(d,a[i])<=r)continue;            else            solve(a[i],i-1);        }        printf("(%.1lf,%.1lf).\n%.1lf\n",d.x,d.y,r);    }    return 0;}









原创粉丝点击