您的位置:首页 > 大数据 > 人工智能

HDU_5046_Airport(DancingLinksX重复覆盖+二分)

2016-05-02 21:13 561 查看


Airport

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1664 Accepted Submission(s): 534



Problem Description

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 dij = |xi - xj| + |yi -
yj|. 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 di(1 ≤ i ≤ N ) as the distance from
city i to the nearest city with airport. Your aim is to minimize the value max{di|1 ≤ i ≤ N }. You just output the minimum.

Input

The first line of the input is T (1 ≤ T ≤ 100), which stands for the number of test cases you need to solve.

The first line of each case contains two integers N ,K (1 ≤ N ≤ 60,1 ≤ K ≤ N ),as mentioned above.

The next N lines, each lines contains two integer xi and yi (-109 ≤ xi, yi ≤ 109), denote the coordinates of city i.

Output

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.

Sample Input

2
3 2
0 0
4 0
5 1
4 2
0 3
1 0
3 0
8 9


Sample Output

Case #1: 2
Case #2: 4


Source

2014 ACM/ICPC Asia Regional Shanghai Online

Recommend

hujie

题意

给你N个城市,让你选其中K个造机场

然后让每个城市到机场的距离最小值中的最大值最小

距离是曼哈顿距离

解法

可以初始化出不同城市的距离

二分一个距离

城市为行和列

如果两个城市间距离不大于二分的值就对应1

然后用DancingLinksX重复覆盖判断这个最小的最大距离可不可以

这里二分有一个小技巧

由于数据范围比较大,

因此做一个距离数组,按距离排序

然后在距离数组中二分速度会更快

但是自己的程序并没有那样写,直接二分的0到无穷

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

typedef long long LL;
const int M=65;
const int MN=M;
const int MM=M;
const int MNN=MN*MM+MM; //最大点数

struct DLX
{
int n,m,si;//n行数m列数si目前有的节点数
//十字链表组成部分
int U[MNN],D[MNN],L[MNN],R[MNN],Row[MNN],Col[MNN];
//第i个结点的U向上指针D下L左R右,所在位置Row行Col列
int H[MN],S[MM]; //记录行的选择情况和列的覆盖情况
int ansd,ans[MN];//ansd最小步数,需要初始化
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;
si=m;                 //目前用了前0~m个结点
for(int i=1;i<=n;i++)
H[i]=-1;
}
void link(int r,int c)    //插入点(r,c)
{
++S[Col[++si]=c];     //si++;Col[si]=c;S[c]++;
Row[si]=r;
D[si]=D[c];
U[D[c]]=si;
U[si]=c;
D[c]=si;
if(H[r]<0)
H[r]=L[si]=R[si]=si;
else
{
R[si]=R[H[r]];
L[R[H[r]]]=si;
L[si]=H[r];
R[H[r]]=si;
}
}
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;
}
bool v[MNN];
int h() //估值
{
int ret=0;
for(int c=R[0];c!=0;c=R[c])
v[c]=1;
for(int c=R[0];c!=0;c=R[c])
if(v[c])
{
ret++;
v[c]=0;
for(int i=D[c];i!=c;i=D[i])
for(int j=R[i];j!=i;j=R[j])
v[Col[j]]=0;
}
return ret;
}
bool dance(int d)
{
if(d+h()>ansd)  //利用A*优化
return 0;
if(R[0]==0)
{
return ansd>=d;
}
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 1;
for(int j=L[i];j!=i;j=L[j])
resume(j);
resume(i);
}
return 0;
}
}dlx;

int x[M],y[M];
LL dis[M][M];

LL bs(int n,int k,LL md)
{
LL lo=0,hi=md;
LL mi,ans=1e15;
while(lo<=hi)
{
mi=lo+(hi-lo)/2;
dlx.ansd=k;
dlx.init(n,n);
for(int i=1;i<=n;i++)
{
dlx.link(i,i);
for(int j=i+1;j<=n;j++)
if(dis[i][j]<=mi)
{
dlx.link(i,j);
dlx.link(j,i);
}
}
if(dlx.dance(0))
{
ans=min(ans,mi);
hi=mi-1;
}
else
lo=mi+1;
}
return ans;
}

int main()
{
int t;
int n,k;
scanf("%d",&t);
for(int ca=1;ca<=t;ca++)
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d%d",&x[i],&y[i]);
LL md=0;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
dis[j][i]=dis[i][j]=abs((LL)x[i]-x[j])+abs((LL)y[i]-y[j]);
md=max(md,dis[i][j]);
}
printf("Case #%d: %I64d\n",ca,bs(n,k,md));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: