首页 > ACM题库 > HDU-杭电 > HDU 3264-Open-air shopping malls-计算几何-[解题报告]HOJ
2014
03-13

HDU 3264-Open-air shopping malls-计算几何-[解题报告]HOJ

Open-air shopping malls

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

1
2
0 0 1
2 0 1

样例输出:

2.0822

此题意很简单,思路也容易想到。但是我就是卡在了求两圆相交求面积的地方。还好现在有了模板了。

不说了,直接贴代码,还得多多总结,二分真的很好用,要学会用啊!


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
#define INF 2147483647
#define N 25
#define PI 3.141592653
#define EPS 1e-8
struct point
{
    double x;
    double y;
}p[N];
double r[N];
int n;
double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double interarea(point a,double ra,point b,double rb)   //求圆相交面积的模板 重点
{
    double ans=0;
    double d=dis(a,b);
    double temp;
    if(ra<rb)
    {
        temp=ra;
        ra=rb;
        rb=temp;
    }
    if(d>=ra+rb)return 0;        //相离
    if(d<=ra-rb)return PI*rb*rb;         //内含
    double angle1=acos((ra*ra+d*d-rb*rb)/2.0/ra/d);
    double angle2=acos((rb*rb+d*d-ra*ra)/2.0/rb/d);
    ans-=d*ra*sin(angle1);
    ans+=angle1*ra*ra+angle2*rb*rb;
    return ans;
}
bool istrue(int t,double ra)
{
    for(int i=0;i<n;i++)
    {
            double area=interarea(p[t],ra,p[i],r[i]);
            if(area<0.5*PI*r[i]*r[i])return false;
    }
    return true;
}
double getr(int i)
{
    double l,r,mid;
    l=0;r=50000;
    while(l+EPS<=r)
    {
        mid=(l+r)/2;
        if(istrue(i,mid))r=mid-EPS;
        else l=mid+EPS;
    }
    return mid;

}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lf",&p[i].x,&p[i].y,&r[i]);
        }
        double ans=INF;
        for(int i=0;i<n;i++)
        {
            double radiu=getr(i);
            if(ans>radiu)ans=radiu;
        }
        printf("%.4lf\n",ans);
    }
    return 0;
}

参考:http://blog.csdn.net/cxiaokai/article/details/6664875


  1. 一开始就规定不相邻节点颜色相同,可能得不到最优解。我想个类似的算法,也不确定是否总能得到最优解:先着一个点,随机挑一个相邻点,着第二色,继续随机选一个点,但必须至少有一个边和已着点相邻,着上不同色,当然尽量不增加新色,直到完成。我还找不到反例验证他的错误。。希望LZ也帮想想, 有想法欢迎来邮件。谢谢