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.
题目就是让我们找到一个最小的R,能够在M个站里面找到K个来覆盖N个城市。
假设知道R的话,就可以通过DLX判断这个R成不成立,然后二分查找R就好。。。。。。
代码如下:
#include<iostream>
#include<cstring> using namespace std; const int MaxN=;
const int MaxM=;
const int MaxNode=MaxN*MaxM; int K;
int N,M; struct DLX
{
int D[MaxNode],U[MaxNode],L[MaxNode],R[MaxNode],col[MaxNode];
int H[MaxN],S[MaxM];
int n,m,size; void init(int _n,int _m)
{
n=_n;
m=_m; for(int i=;i<=m;++i)
{
D[i]=U[i]=i;
L[i]=i-;
R[i]=i+; S[i]=;
}
L[]=m;
R[m]=; size=m; for(int i=;i<=n;++i)
H[i]=-;
} void Link(int r,int c)
{
col[++size]=c;
++S[c]; U[size]=U[c];
D[size]=c;
D[U[c]]=size;
U[c]=size; if(H[r]==-)
H[r]=L[size]=R[size]=size;
else
{
L[size]=L[H[r]];
R[size]=H[r];
R[L[H[r]]]=size;
L[H[r]]=size;
}
} void remove(int c)
{
for(int i=D[c];i!=c;i=D[i])
{
R[L[i]]=R[i];
L[R[i]]=L[i];
}
} void resume(int c)
{
for(int i=U[c];i!=c;i=U[i])
R[L[i]]=L[R[i]]=i;
} bool vis[MaxM]; int getH()
{
int ret=; for(int c=R[];c!=;c=R[c])
vis[c]=; for(int c=R[];c!=;c=R[c])
if(vis[c])
{
++ret;
vis[c]=; for(int i=D[c];i!=c;i=D[i])
for(int j=R[i];j!=i;j=R[j])
vis[col[j]]=;
} return ret;
} bool Dance(int d)
{
if(d+getH()>K)
return ; if(R[]==)
{
if(d<=K)
return ;
return ;
} int c=R[]; for(int i=R[];i!=;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); if(Dance(d+))
return ; for(int j=L[i];j!=i;j=L[j])
resume(j); resume(i);
} return ;
}
}; DLX dlx;
int Rx[],Ry[],Cx[],Cy[]; void solve()
{
double L=,R=1001.0,Mid; while(R-L>0.0000001)
{
Mid=(L+R)/; dlx.init(M,N); for(int i=;i<=M;++i)
for(int j=;j<=N;++j)
if(Mid*Mid>=(Rx[i]-Cx[j])*(Rx[i]-Cx[j])+(Ry[i]-Cy[j])*(Ry[i]-Cy[j]))
dlx.Link(i,j); if(dlx.Dance())
R=Mid;
else
L=Mid;
} cout<<L<<endl;
} int main()
{
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(); int T;
cin>>T; while(T--)
{
cin>>N>>M>>K; for(int i=;i<=N;++i)
cin>>Cx[i]>>Cy[i]; for(int i=;i<=M;++i)
cin>>Rx[i]>>Ry[i]; solve();
} return ;
}