HDU 3932(计算几何+最小圆覆盖)
2017-09-04 10:56
351 查看
问题描述:
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
Sample Output
题目题意:题目给我们一个最大的范围(是一个矩形)的俩条边(我也不知这个有撒用,反正我没用
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/899229cfab2c02d614490485cabb781b.gif)
),然后给了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))/
4000
((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;
}
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
题目题意:题目给我们一个最大的范围(是一个矩形)的俩条边(我也不知这个有撒用,反正我没用
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/899229cfab2c02d614490485cabb781b.gif)
),然后给了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))/
4000
((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;
}
相关文章推荐
- hdu-3007(计算几何+最小覆盖圆)
- HDU 4720(计算几何+最小圆覆盖)
- HDU 3007 Buried memory(计算几何の最小圆覆盖,模版题)
- HDU 3007 最小圆覆盖 计算几何
- hdu 4606 Occupy Cities - 计算几何 + 最短路 + 最小路径覆盖
- HDU 4606 Occupy Cities (计算几何+最短路+二分+最小路径覆盖)
- HDU 5251 矩形面积 (计算几何+旋转卡壳求覆盖凸包的最小矩形面积)
- bzoj1336/1337 && hdu3932 最小圆覆盖 计算几何:随机增量法&模拟退火
- hdu 3548 最小三角形周长(计算几何)
- 计算几何--最小圆覆盖
- 【BZOJ2823】【AHOI2012】信号塔 最小圆覆盖 计算几何
- bzoj1185: [HNOI2007]最小矩形覆盖 计算几何 旋转卡壳
- [BZOJ1185][HNOI2007]最小矩形覆盖(计算几何-旋转卡壳)
- HDU 6080 度度熊保护村庄(计算几何+最小环-Floyd)
- hdu 3932 最小覆盖圆 随机算法做法
- hdu 3932,3007 最小圆覆盖 随机增量法
- hdu 3932 Groundhog Build Home (最小圆覆盖)
- hdu4606 Occupy Citie 简单计算几何,最小路径覆盖
- hdu 4606 简单计算几何+floyd+最小路径覆盖
- hdu 3932 最小覆盖圆 随机算法做法