Airport
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 965 Accepted Submission(s): 282
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 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.
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 x i and y i (-10 9 ≤ x i, y i ≤ 10 9), denote the coordinates of city i.
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 x i and y i (-10 9 ≤ x i, y i ≤ 10 9), 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
#include<stdio.h>
#include<string.h>
#define N 4000
typedef __int64 ll;
#define inf 0x3fffffff
int size,s[N],u[N],d[N],l[N],r[N],h[N],row[N],col[N],vis[N];
struct node
{
ll x,y;
} a[N];
void init(int n,int m)
{
int i;
for(i=0; i<=m; i++)
{
s[i]=0;
d[i]=u[i]=i;
l[i]=i-1;
r[i]=i+1;
}
l[0]=m;
r[m]=0;
size=m;
for(i=1; i<=n; i++)
h[i]=-1;
}
void link(int x,int y)
{
++s[col[++size]=y];
row[size]=x;
u[d[y]]=size;
d[size]=d[y];
d[y]=size;
u[size]=y;
if(h[x]<0)
h[x]=l[size]=r[size]=size;
else
{
l[r[h[x]]]=size;
r[size]=r[h[x]];
r[h[x]]=size;
l[size]=h[x];
}
}
void remove(int c)
{
int i;
for(i=d[c]; i!=c; i=d[i])
{
l[r[i]]=l[i];
r[l[i]]=r[i];
}
}
void resume(int c)
{
int i;
for(i=u[c]; i!=c; i=u[i])
{
l[r[i]]=i;
r[l[i]]=i;
}
}
int f()
{
int i,j,k,num=0;
memset(vis,0,sizeof(vis));
for(i=r[0]; i!=0; i=r[i])
{
if(!vis[i])
{
num++;
for(j=d[i]; j!=i; j=d[j])
for(k=r[j]; k!=j; k=r[k])
vis[col[k]]=1;
}
}
return num;
}
int dance(int x,int k)
{
int c,i,j;
if(x+f()>k)//减枝
return 0;
if(r[0]==0)
{
if(x<=k)
return 1;
else
return 0;
}
c=r[0];
for(i=r[0]; i!=0; i=r[i])
if(s[i]<s[c])
c=i;
for(i=d[c]; i!=c; i=d[i])
{
remove(i);
for(j=r[i]; j!=i; j=r[j])
remove(j);
if(dance(x+1,k))
return 1;
for(j=l[i]; j!=i; j=l[j])
resume(j);
resume(i);
}
return 0;
}
ll dis(int i,int j)
{
ll s=0;
s+=a[i].x<a[j].x ? a[j].x-a[i].x : a[i].x-a[j].x;
s+=a[i].y<a[j].y ? a[j].y-a[i].y : a[i].y-a[j].y;
return s;
}
int main()
{
int t,cnt=1,i,n,k,j;
ll l,r,mid,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(i=1; i<=n; i++)
scanf("%I64d%I64d",&a[i].x,&a[i].y);
l=0;
r=4000000000;
while(l<=r)
{
mid=(l+r)/2;
// printf("mid=%lld\n",mid);
init(n,n);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(dis(i,j)<=mid)
{
// printf("%lld %lld %d %d\n",dis(i,j),mid,i,j);
link(i,j);
}
if(dance(0,k))
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
printf("Case #%d: %I64d\n",cnt++,ans);
}
return 0;
}