Airport
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 306 Accepted Submission(s): 67
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
题意:有N个城市,现在要选K个城市建飞机场,每个城市会去离自己最近的飞机场,要求距离以飞机场最远的距离最小。
思路:我用DLX搜的,不知道有没有更好的方法,我首先二分一个距离,因为有了距离的要求,那么就知道了,当在一个城市建飞机场能覆盖到哪些城市了,所以这里我们能用重复覆盖的算法来搜。。。。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <queue>
#include <cmath>
#include <cmath>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define rrep(i,b,a) for(int i=(b);i>=(int)(a);--i)
#define clr(a,x) memset(a,x,sizeof(a))
#define eps 1e-9
#define LL long long
#define zero(x) (-eps < (x) && (x) < eps)
#define FOR(i,s,A) for(int i=A[s];i!=s;i=A[i])
const int maxn = 60 + 5;
LL d[maxn][maxn];
LL X[maxn],Y[maxn];
int N,K;
inline LL mabs(LL x) { return x < 0 ? -x : x; }
void input()
{
scanf("%d%d",&N,&K);
rep(i,1,N+1) scanf("%I64d%I64d",&X[i],&Y[i]);
rep(i,1,N+1) rep(j,1,N+1) d[i][j] = mabs(X[i]-X[j]) + mabs(Y[i]-Y[j]);
}
int L[maxn*maxn],R[maxn*maxn],U[maxn*maxn],D[maxn*maxn];
int S[maxn],col[maxn*maxn];
int cnt;
void addrow(const vector<int>& v)
{
int head = cnt;
rep(i,0,v.size()) {
int c = v[i];
L[cnt] = cnt - 1; R[cnt] = cnt + 1;
U[cnt] = U[c]; D[cnt] = c;
D[U[cnt]] = cnt; U[c] = cnt;
++S[c];
col[cnt] = c;
++cnt;
}
L[head] = cnt-1; R[cnt-1] = head;
}
inline void LrRemove(int x) { L[R[x]] = L[x]; R[L[x]] = R[x]; }
inline void LrResume(int x) { L[R[x]] = R[L[x]] = x; }
inline void RemoveColumn(int c)
{
FOR(i,c,D) LrRemove(i);
}
inline void ResumeColumn(int c)
{
FOR(i,c,U) LrResume(i);
}
int H()
{
int h = 0;
bool vis[maxn] = { 0 };
FOR(i,0,R) if (!vis[i]) {
++h;
vis[i] = true;
FOR(j,i,D) FOR(k,j,R) vis[col[k]] = true;
}
return h;
}
bool dfs(int d)
{
if (d > K) return false;
if (R[0] == 0) return true;
if (d + H() > K) return false;
int c = R[0];
FOR(i,0,R) if (S[i] < S[c]) c = i;
if (S[c] == 0) return false;
FOR(i,c,D) {
RemoveColumn(i);
FOR(j,i,R) RemoveColumn(j);
if (dfs(d+1)) return true;
FOR(j,i,L) ResumeColumn(j);
ResumeColumn(i);
}
return false;
}
bool ok(LL limit)
{
rep(i,0,N+1) {
L[i] = i-1; R[i] = i + 1;
S[i] = 0; col[i] = i;
U[i] = D[i] = i;
}
L[0] = N; R[N] = 0;
cnt = N+1;
rep(i,1,N+1) {
vector<int> v;
rep(j,1,N+1) if (d[i][j] <= limit)
v.push_back(j);
addrow(v);
}
return dfs(0);
}
void solve()
{
LL left = 0, right = 1e10;
rep(i,1,N+1) {
LL x = 0;
rep(j,1,N+1) x = max(x,d[i][j]);
right = min(right,x);
}
LL mid;
LL ans = right;
while (left <= right) {
mid = left + right >> 1;
if (ok(mid)) right = mid - 1, ans = min(ans,mid);
else left = mid + 1;
}
printf("%I64d\n",ans);
}
void Getinput()
{
freopen("in.txt","w",stdout);
int T = 100;
printf("%d\n",T);
while (T--) {
N = 60; K = rand()%N+1;
printf("%d %d\n",N,K);
rep(i,0,N) {
int x = rand()%1000000001;
int y = rand()%1000000001;
if (rand()%2) printf("%d",-x);
else printf("%d",x);
if (rand()%2) printf(" %d\n",y);
else printf(" %d\n",-y);
}
}
}
int main()
{
// Getinput(); return 0;
#ifdef ACM
freopen("in.txt","r",stdin);
#endif
int T; cin >> T;
rep(cas,1,T+1) {
input();
printf("Case #%d: ",cas);
solve();
}
}