HDU 3264 Constructing Roads(二分法)

Constructing Roads
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
 

Description

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.
 

Input

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.
 

Output

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.
 

Sample Input

1 2 0 0 1 2 0 1
 

Sample Output

2.0822
 
 

题目的意思是:这个城市有许多的圆形商贸中心,这些圆形不相交,不包含。这天下雨了,需要一把巨大的伞来覆盖所有的商业中心,
要求这把伞以某一个商业圆的圆心作为它的圆心,并且至少覆盖商业圆面积的一半。求满足条件的最小的伞的半径

思路是:枚举把所有的商业中心当做圆心时,能取到的半径(这是因为题目的范围很小)
用二分法判断相交圆的面积是否等于商业圆面积的一半

 

# include<stdio.h>
# include<math.h>
double r2,d;    
        //r2代表商贸圆的半径,d代表伞的圆心到商贸圆的圆心的距离,这两个变量用在求相交圆的面积上
double ss;            //商贸圆的面积的一半
double pi=acos(-1.0);
double left,right,mid;            //二分法用到的变量
struct node{
    double x,y,r;
}m[25]; 
double area(double r1){        //求相交圆的面积
    double a1=acos((r1*r1+d*d-r2*r2)/(2.0*r1*d));
    double a2=acos((r2*r2+d*d-r1*r1)/(2.0*r2*d));
    double s=(a1*r1*r1+a2*r2*r2-r1*d*sin(a1));
    return s;
}

void f(){    //二分法求当伞刚好覆盖商贸圆面积的一半时的半径
    double s;    //表示相交圆的面积
    s=area(mid);    
    while(1){
        if(fabs(s-ss)<=0.0000001) break;    //double型变量不能直接用等于
        if(ss-s>0.0000001) left = mid+0.0000001;
        if(s-ss>0.0000001) right = mid-0.0000001;
        mid=(left+right)/2.0;
        s=area(mid);
    }
}
int main(){
    int T,n,i,j;
    double max,a,b,min;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%lf%lf%lf",&m[i].x,&m[i].y,&m[i].r);

        if(n==1){        //判断只有一个商业圆的情况,这一点一直没看出来,浪费了大把的时间T_T
            printf("%.4lf\n",m[0].r/sqrt(2));
            continue;
        }

        min=200010;        //挑选满足条件的伞的最小的半径
        for(i=0;i<n;i++)    //遍历把每个商业圆的圆心当做伞的圆心
        {
            max=0;    //要覆盖所有的圆形,所以需要最大的半径
            for(j=0;j<n;j++)
            { 
                if(j==i)  continue;
                a=m[i].x-m[j].x;
                b=m[i].y-m[j].y;
                r2=m[j].r;
                d=sqrt((a*a)+(b*b));
                ss=r2*r2*pi;
                ss=(double)1.0/2.0*ss;
                left=d;        //二分的左边界
                right=d+r2;        //二分的右边界
                mid=(left+right)/2.0;
                f();
                if(mid-max>0.0000001)
                    max=mid;
            }
            if(max-min<0.0000001)
                min=max;
        }
        printf("%.4lf\n",min);
    }
    return 0;
}

 

你可能感兴趣的:(struct)