您的位置:首页 > 大数据 > 人工智能

hdu 3264 Open-air shopping malls(圆相交面积+二分)

2016-07-27 16:34 591 查看

Open-air shopping malls

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2458 Accepted Submission(s):
906


[align=left]Problem Description[/align]
The city of M is a famous shopping city and its
open-air shopping malls are extremely attractive. During the tourist seasons,
thousands of people crowded into these shopping malls and enjoy the
vary-different shopping.

Unfortunately, the climate has changed little by
little and now rainy days seriously affected the operation of open-air shopping
malls—it’s obvious that nobody will have a good mood when shopping in the rain.
In order to change this situation, the manager of these open-air shopping malls
would like to build a giant umbrella to solve this problem.

These
shopping malls can be considered as different circles. It is guaranteed that
these circles will not intersect with each other and no circles will be
contained in another one. The giant umbrella is also a circle. Due to some
technical reasons, the center of the umbrella must coincide with the center of a
shopping mall. Furthermore, a fine survey shows that for any mall, covering half
of its area is enough for people to seek shelter from the rain, so the task is
to decide the minimum radius of the giant umbrella so that for every shopping
mall, the umbrella can cover at least half area of the mall.

[align=left]Input[/align]
The input consists of multiple test cases.
The
first line of the input contains one integer T (1<=T<=10), which is the
number of test cases.
For each test case, there is one integer N
(1<=N<=20) in the first line, representing the number of shopping
malls.
The following N lines each contain three integers X,Y,R, representing
that the mall has a shape of a circle with radius R and its center is positioned
at (X,Y). X and Y are in the range of [-10000,10000] and R is a positive integer
less than 2000.

[align=left]Output[/align]
For each test case, output one line contains a real
number rounded to 4 decimal places, representing the minimum radius of the giant
umbrella that meets the demands.

[align=left]Sample Input[/align]

1

2

0 0 1

2 0 1

[align=left]Sample Output[/align]

2.0822

[align=left]Source[/align]
2009
Asia Ningbo Regional Contest Hosted by NIT

[align=left]Recommend[/align]
lcy | We have carefully selected several similar
problems for you: 3268 3265 3269 3262 3263

题意:给出一些圆,选择其中一个圆的圆心为圆心,然后画一个大圆,要求大圆最少覆盖每个圆的一半面积。求大圆最小时的半径。

枚举每个点,用二分求出需要的圆,更新最小值即可。
其中用到了圆相交面积,可以参考这题: http://www.cnblogs.com/pshw/p/5711251.html

附上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 25

const double pi = acos(-1.0);
const double EPS = 1e-8;
int n;

double max(double a,double b)
{
return a>b?a:b;
}

double min(double a,double b)
{
return a<b?a:b;
}

struct Round
{
double x,y;
double r;
} rr
,s;

double dis(Round a, Round b)  ///两点之间的长度
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double solve(Round a, Round b)   ///求两圆相交的面积
{
double d = dis(a, b);
if(d >= a.r + b.r)
return 0;
else if(d <= fabs(a.r-b.r))
{
double r = a.r < b.r?a.r : b.r;
return pi * r * r;
}
double ang1 = acos((a.r * a.r + d * d - b.r * b.r) / 2.0 / a.r / d);
double ang2 = acos((b.r * b.r + d * d - a.r * a.r) / 2.0 / b.r / d);
double ret = ang1 * a.r * a.r + ang2 * b.r * b.r - d * a.r * sin(ang1);
return ret;
}

bool check(Round s)
{
for(int i=0; i<n; i++)  ///大圆是否覆盖每个圆的一半面积
{
if(solve(s, rr[i]) * 2 < pi * rr[i].r * rr[i].r)
return false;  ///不满足直接返回
}
return true;
}

double bin(double l, double r, Round s) ///二分,找出最小圆的半径
{
double mid;
while(fabs(l - r) >= EPS) ///精度划分
{
mid = (l + r) / 2;
s.r = mid;
if(check(s)) ///满足返回的说明半径长度足够,有可能可以更短
r=mid;
else     ///不满足返回的说明半径长度不够,需要更长
l=mid+EPS;
}
return mid;
}

int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0; i<n; i++)
scanf("%lf%lf%lf",&rr[i].x,&rr[i].y,&rr[i].r);
double ans = 1e10;
for(i=0; i<n; i++)
{
s.x = rr[i].x;
s.y = rr[i].y;
double right = 0;
for(j=0; j<n; j++)
{
right = max(right, dis(s, rr[j]) + rr[j].r);
///以当前点为圆心,找出可以覆盖所有的圆面积的最长半径
}
ans = min(ans, bin(0, right, s)); ///二分搜索,记录最小的圆的半径
}
printf("%.4f\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: