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

博客介绍了HDU 3932比赛题目,该题目涉及计算几何中的最小圆覆盖问题。内容讨论了如何找到一个点,使得从这个点到给定点集合中所有其他点的最长距离最小,并给出了问题的解决方案和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

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

题目题意:题目给我们一个最大的范围(是一个矩形)的俩条边(我也不知这个有撒用,反正我没用大笑),然后给了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;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值