HDU 2295 Radar(舞蹈链+二分)

Radar

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


Problem Description
N cities of the Java Kingdom need to be covered by radars for being in a state of war. Since the kingdom has M radar stations but only K operators, we can at most operate K radars. All radars have the same circular coverage with a radius of R. Our goal is to minimize R while covering the entire city with no more than K radars.
 

Input
The input consists of several test cases. The first line of the input consists of an integer T, indicating the number of test cases. The first line of each test case consists of 3 integers: N, M, K, representing the number of cities, the number of radar stations and the number of operators. Each of the following N lines consists of the coordinate of a city.
Each of the last M lines consists of the coordinate of a radar station.

All coordinates are separated by one space.
Technical Specification

1. 1 ≤ T ≤ 20
2. 1 ≤ N, M ≤ 50
3. 1 ≤ K ≤ M
4. 0 ≤ X, Y ≤ 1000
 

Output
For each test case, output the radius on a single line, rounded to six fractional digits.
 

Sample Input
 
  
1 3 3 2 3 4 3 1 5 4 1 1 2 2 3 3
 

Sample Output
 
  
2.236068

题解:
二分枚举半径的值,对于每个雷达能覆盖到的点在舞蹈链上建图。
代码:   
#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
const int Maxn=100005;
int x1[maxn],y11[maxn],x2[maxn],y2[maxn];
struct node
{
    int L[Maxn],R[Maxn],U[Maxn],D[Maxn];
    int col[Maxn],row[Maxn];
    int H[Maxn],S[Maxn];
    int ansd,n,m,len;
    void init(int _n,int _m)
    {
        n=_n,m=_m;
        for(int i=0;i<=m;i++)
        {
            U[i]=D[i]=i;
            L[i]=i-1;
            R[i]=i+1;
            S[i]=0;
        }
        L[0]=m;R[m]=0;ansd=1e9;len=m;
        for(int i=1;i<=n;i++)
            H[i]=-1;
    }
    void Link(int r,int c)
    {
        len++;
        S[c]++;
        row[len]=r;
        col[len]=c;
        D[len]=D[c];
        U[len]=c;
        U[D[c]]=len;
        D[c]=len;
        if(H[r]==-1)
            H[r]=R[len]=L[len]=len;
        else
        {
           R[len]=R[H[r]];
           L[len]=H[r];
           L[R[H[r]]]=len;
           R[H[r]]=len;
        }
    }
    void remove(int c)
    {
        for(int i=D[c];i!=c;i=D[i])
        {
            L[R[i]]=L[i];
            R[L[i]]=R[i];
        }
    }
    void resume(int c)
    {
        for(int i=U[c];i!=c;i=U[i])
        {
            L[R[i]]=i;
            R[L[i]]=i;
        }
    }
    bool v[Maxn];
    int f()
    {
        for(int i=R[0];i!=0;i=R[i])
            v[i]=1;
        int ans=0;
        for(int i=R[0];i!=0;i=R[i])
        {
            if(v[i])
            {
                ans++;
                v[i]=0;
                for(int j=D[i];j!=i;j=D[j])
                {
                    for(int k=R[j];k!=j;k=R[k])
                        v[col[k]]=0;
                }
            }
        }
        return ans;
    }
    void Dance(int d)
    {
        if(d+f()>=ansd)return;
        if(R[0]==0)
        {
            ansd=min(ansd,d);
            return;
        }
        int c=R[0];
        for(int i=R[0];i!=0;i=R[i])
        {
            if(S[i]<S[c])
                c=i;
        }
        for(int i=D[c];i!=c;i=D[i])
        {
            remove(i);
            for(int j=R[i];j!=i;j=R[j])
                remove(j);
            Dance(d+1);
            for(int j=L[i];j!=i;j=L[j])
                resume(j);
            resume(i);
        }
    }
}g;
bool check(int j,int i,double r)
{
    double d=(x1[i]-x2[j])*(x1[i]-x2[j])+(y11[i]-y2[j])*(y11[i]-y2[j]);
    if(d<=r*r)return 1;
    else return 0;
}
bool is(int n,int m,int k,double r)
{
    g.init(m,n);
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(check(i,j,r))
                g.Link(i,j);
        }
    }
    g.Dance(0);
    if(g.ansd<=k)return 1;
    return 0;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&x1[i],&y11[i]);
        for(int i=1;i<=m;i++)
            scanf("%d%d",&x2[i],&y2[i]);
        double l=0.0,r=1500;
        while(r-l>1e-8)
        {
            double mid=(l+r)/2;
            if(is(n,m,k,mid))r=mid;
            else l=mid;
        }
        printf("%.6lf\n",r);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/albertluf/article/details/80284925
今日推荐