【题目描述】
The country of jiuye composed by N cites. Each city can be viewed as a point in a two- dimensional plane with integer coordinates (x,y). The distance between city i and city j is defined by d ij = |x i - x j| + |y i - y j|. jiuye want to setup airport in K cities among N cities. So he need your help to choose these K cities, to minimize the maximum distance to the nearest airport of each city. That is , if we define d i(1 ≤ i ≤ N ) as the distance from city i to the nearest city with airport. Your aim is to minimize the value max{d i|1 ≤ i ≤ N }. You just output the minimum.
jiuye国由N个城市组成,每个城市可以被看作一个在二维平面上的整数坐标(x,y),城市i和城市j之间的距离dij=|x i - x j| + |y i - y j|。jiuye想在N座城市中设立K个机场,所以他需要你帮助选择这K个城市尽量使每个城市到最近机场的最大距离最小也就是说,如果我们将di(1≤i≤N)定义为从城市i到最近的有机场的城市的距离。您的目标是最小化值max{dI|1≤i≤N}。你只需输出最小值。
【输入】
The first line of the input is T (1 ≤ T ≤ 100), which stands for the number of test cases you need to solve.
第一行一个整数T (1 ≤ T ≤ 100),表示你需要解决的测试样例的数量。
The first line of each case contains two integers N ,K (1 ≤ N ≤ 60,1 ≤ K ≤ N ),as mentioned above.
每个样例的第一行包含两个在前面被提到的整数N和K(1 ≤ N ≤ 60,1 ≤ K ≤ N )。
The next N lines, each lines contains two integer x i and y i (-10 9 ≤ x i, y i ≤ 10 9), denote the coordinates of city i.
后面N行,每行包含两个整数xi和yi (-109 ≤ x i, y i ≤ 10 9),表示第i座城市的坐标。
【输出】
For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then a single integer means the minimum.
对于每个测试样例,一开始输出“Case #t: ”,没有引号,t表示测试样例的序号。然后一个整数表示最小值。
【样例输入】
2
3 2
0 0
4 0
5 1
4 2
0 3
1 0
3 0
8 9
【样例输出】
Case #1: 2
Case #2: 4
题目链接:https://cn.vjudge.net/problem/HDU-5046
和前面卫星那道题很像,二分找答案+DLX判断是否可行,虽然过了,但是感觉能优化的地方挺多的
代码如下:
#include <iostream>
#include <cmath>
using namespace std;
static const int MAXN=60+10;
static const int MAXM=60+10;
static const int MAXNODE=MAXN*MAXM;
int x[MAXN],y[MAXN],k;
struct DLX {
int n,m,size;
int U[MAXNODE],D[MAXNODE],R[MAXNODE],L[MAXNODE],Row[MAXNODE],Col[MAXNODE];
int H[MAXN],S[MAXM];
int ans[MAXN],ansd;
bool vis[MAXNODE];
void init(int _n,int _m)
{
n=_n;
m=_m;
for(int i=0;i<=m;i++)
{
S[i]=0;
U[i]=D[i]=i;
L[i]=i-1;
R[i]=i+1;
}
R[m]=0;
L[0]=m;
size=m;
for(int i=1;i<=n;i++)
H[i]=-1;
}
void Link(int r,int c)
{
++S[Col[++size]=c];
Row[size]=r;
D[size]=D[c];
U[D[c]]=size;
U[size]=c;
D[c]=size;
if(H[r]<0)
H[r]=L[size]=R[size]=size;
else
{
R[size]=R[H[r]];
L[R[H[r]]]=size;
L[size]=H[r];
R[H[r]]=size;
}
}
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]]=R[L[i]]=i;
}
int f()
{
int res=0;
for(int i=R[0];i!=0;i=R[i])
vis[i]=true;
for(int i=R[0];i!=0;i=R[i])
if(vis[i])
{
res++;
vis[i]=false;
for(int j=D[i];j!=i;j=D[j])
for(int t=R[j];t!=j;t=R[t])
vis[Col[t]]=false;
}
return res;
}
bool Dance(int d)
{
if(d+f()>k)
return false;
if(R[0]==0)
return d<=k;
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);
if(Dance(d+1))
return true;
for(int j=L[i];j!=i;j=L[j])
resume(j);
resume(i);
}
return false;
}
};
DLX dlx;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
int T;
cin>>T;
for(int kase=1;kase<=T;kase++)
{
int n;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>x[i]>>y[i];
long long r=4e9;
long long l=0;
while(l<r)
{
dlx.init(n,n);
long long mid=(r+l)/2;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(1ll*abs(x[i]-x[j])+1ll*abs(y[i]-y[j])<=mid)
dlx.Link(i,j);
if(dlx.Dance(0)) r=mid;
else l=mid+1;
}
cout<<"Case #"<<kase<<": "<<l<<endl;
}
return 0;
}