Airport
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1889 Accepted Submission(s): 602
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
Sample Input
2
3 2
0 0
4 0
5 1
4 2
0 3
1 0
3 0
8 9Sample Output
Case #1: 2
Case #2: 4Source
2014 ACM/ICPC Asia Regional Shanghai Online
这个题我在一个诡异的地方卡了一上午,WA,TLE十多次,一直没过,算了,不说了,我们来看这道题。
首先就是二分答案,然后可以算出如果在每个城市建机场可以覆盖多少个城市,然后再根据这个来建立一个矩阵,然后就是用DLK来跑一个覆盖问题,但是这里不是精准覆盖,应为可以重复,所以再删除某一个节点的时候就不用把和它相连的都删了,只用删它所在的那一列就可以了
就是相当于选出不超过k 行使得每列都有1
然后还可以用估价函数来增速
然后在这里说一下,二分的时候一定要从0开始,不然GG
下面放代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x) memset(x,0,sizeof(x))
#define clrmax(x) memset(x,127,sizeof(x))
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define M 65
struct link2
{
link2 *right,*left,*up,*down,*col;
int row,count;
}*head,*c[M],*Link[M*M],dizhi[M*M+M];
int t,T;
int n,k;
void disable(link2 *&lik)
{
//lik->left->right=lik->right;
//lik->right->left=lik->left;
for(link2 *i=lik->down;i!=lik;i=i->down)
{
i->left->right=i->right;
i->right->left=i->left;
}
}
void enable(link2 *&lik)
{
//lik->left->right=lik;
//lik->right->left=lik;
for(link2 *i=lik->down;i!=lik;i=i->down)
{
i->left->right=i;
i->right->left=i;
}
}
int Vis[M],vis[M];
int h()
{
int ret=0;
clr(vis);
for(link2 *i=head->right;i!=head;i=i->right)
{
if(vis[i->row])continue;
ret++;
vis[i->row]=1;
for(link2 *j=i->down;j!=i;j=j->down)
for(link2 *k=j->left;k!=j;k=k->left)
vis[k->col->row]=1;
}
return ret;
}
int ans[M],sta;
bool dance(int x)
{
if(x+h()>k)return 0;
if(head->right==head)
return x<=k;
link2 *temp=head->right;
link2 *i=temp->right;
while(i!=head)
{
if(i->count<temp->count)
temp=i;
i=i->right;
}
if(temp->down==temp)return 0;
//Vis[temp->row]=1;
i=temp->down;
while(i!=temp)
{
//ans[++sta]=i->row;
link2 *j=i->right;
while(j!=i)
{
disable(j);
//Vis[j->col->row]=1;
j=j->right;
}
disable(i);
if(dance(x+1))return 1;
enable(i);
j=i->right;
while(j!=i)
enable(j),Vis[j->col->row]=0,j=j->right;
//ans[sta--]=0;
i=i->down;
}
//Vis[temp->row]=0;
return 0;
}
void addline(int *index,int len,int row)
{
for(int i=1;i<=len;i++)
{
t++;
if(i==1)
{
Link[t]=&dizhi[t+T];
Link[t]->right=Link[t];
Link[t]->left=Link[t];
}
else
{
Link[t]=&dizhi[t+T];
Link[t]->left=Link[t-1];
Link[t]->right=Link[t-1]->right;
Link[t-1]->right->left=Link[t];
Link[t-1]->right=Link[t];
}
int j=index[i];
Link[t]->down=c[j];
Link[t]->up=c[j]->up;
c[j]->up->down=Link[t];
c[j]->up=Link[t];
Link[t]->col=c[j];
Link[t]->row=row;
c[j]->count++;
}
}
struct point
{
ll x,y;
}a[M];
ll dis(point x,point y)
{
ll ret1=abs(x.x-y.x);
ll ret2=abs(x.y-y.y);
return ret1+ret2;
}
bool check(ll x)
{
clr(dizhi);
clr(Link);
clr(c);
t=T=0;
head=&dizhi[T++];
for(int i=1;i<=n;i++)
c[i]=&dizhi[T++];
head->left=c[n];
head->right=c[1];
for(int i=1;i<=n;i++)
{
c[i]->left=i==1?head:c[i-1];
c[i]->right=i==n?head:c[i+1];
c[i]->up=c[i]->down=c[i];
c[i]->count=0;
c[i]->row=i;
}
for(int i=1;i<=n;i++)
{
int index[M],sta=0;
clr(index);
for(int j=1;j<=n;j++)
if(dis(a[i],a[j])<=x)
index[++sta]=j;
addline(index,sta,i);
}
clr(Vis);clr(ans);sta=0;
return dance(0);
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int t;
scanf("%d",&t);
for(int o=1;o<=t;o++)
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[i].x=x;
a[i].y=y;
}
ll r=4*1E9+5,l=1,mid;
while(r>l)
{
mid=(l+r)>>1;
if(check(mid))r=mid;
else l=mid+1;
}
printf("Case #%d: "AUTO"\n",o,l);
}
return 0;
}
大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。