首页 > 代码库 > hdu 3264 Open-air shopping malls 求两圆相交

hdu 3264 Open-air shopping malls 求两圆相交

对每个圆二分半径寻找可行的最小半径,然后取最小的一个半径。

对于两圆相交就只要求到两个扇形,然后减去两个全等三角形就行了。


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define pi acos(-1.0)
#define eps 1e-8
#define maxn 50
int n;
struct point{
    double x;
    double y;
    double r;
}c[maxn];
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 area(point a,double ra,point b,double rb)
{
    double ans=0;
    double d=dis(a,b);
    double temp;
    if(ra<rb)  swap(ra,rb);
    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 cover_half(point a,double ra,point b,double rb)  //a是有伞的圆,b是其他圆
{
    return area(a,ra,b,rb)>=0.5*rb*rb*pi;
}
bool isok(double r,int k)
{
    for(int i=1;i<=n;i++)
    {
        if(!cover_half(c[k],r,c[i],c[i].r)) return false;
    }
    return true;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf",&c[i].x,&c[i].y,&c[i].r);
        }
        double ans=5000000;
        for(int i=1;i<=n;i++){
            double l=0.0,r=5000000,mid;
            while(l+eps<=r)
            {
                mid=(l+r)/2;
                if(isok(mid,i)) r=mid-eps;
                else l=mid+eps;
            }
            ans=min(ans,mid);
        }
        printf("%.4lf\n",ans);
    }
    return 0;
}