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

hdu 5046 Airport (重复覆盖)

2015-10-09 15:33 369 查看


Airport

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

Total Submission(s): 1481 Accepted Submission(s): 469



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

二分 + 重复覆盖

/*======================================================
# Author: whai
# Last modified: 2015-10-09 11:30
# Filename: hdu5046.cpp
======================================================*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>

using namespace std;

#define LL __int64
#define PB push_back
#define P pair<int, int>
#define X first
#define Y second

const int MAXM = 65;
const int MAXN = 65;
const int N = MAXN * MAXM;
const int INF = 0x3f3f3f3f;
struct DLX {
int n, m, size;
int U
, D
, R
, L
, row
, col
;
int H[MAXN], S[MAXM];
int ans[MAXN], ans_cnt;
void init(int _n, int _m) {
ans_cnt = INF;
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;
}
bool v[MAXM];
int f() {
int ret = 0;
for (int c = R[0]; c != 0; c = R[c])v[c] = true;
for (int c = R[0]; c != 0; c = R[c])
if (v[c]) {
ret++;
v[c] = false;
for (int i = D[c]; i != c; i = D[i])
for (int j = R[i]; j != i; j = R[j])
v[col[j]] = false;
}
return ret;
}
//注意这些可以加入剪枝优化的
/*void dance(int d) {
if (d + f() >= ans_cnt) return;
if (R[0] == 0) {
if (d < ans_cnt) ans_cnt = d;
return ;
}
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);
//ans[d] = row[i];
for (int j = R[i]; j != i; j = R[j]) remove(j);
dance(d + 1);
for (int j = L[i]; j != i; j = L[j]) resume(j);
resume(i);
}
}*/

bool dance(int d, int limit) {
if (d + f() > limit) return false;
if (R[0] == 0) return d <= limit;
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, limit)) return true;
for (int j = L[i]; j != i; j = L[j]) resume(j);
resume(i);
}
return false;
}
} dlx;

P a
;

LL _abs(int x) {
if(x < 0) return -x;
return x;
}

LL dis(P a, P b) {
return _abs(a.X - b.X) + _abs(a.Y - b.Y);
}

bool ok(LL d, int n, int k) {
dlx.init(n, n);
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(dis(a[i], a[j]) <= d) {
dlx.link(i + 1, j + 1);
}
}
}
return dlx.dance(0, k);
}

void gao(int n, int k) {
LL L = 0, R = 4 * 1e9, ans = 4 * 1e9;
while(L < R) {
LL mid = (L + R) >> 1;
if(!ok(mid, n, k)) {
L = mid + 1;
} else {
ans = min(ans, mid);
R = mid;
}
}
if(ok(L, n, k)) ans = min(ans, L);
if(ok(R, n, k)) ans = min(ans, R);
printf("%I64d\n", ans);
}

int main() {
int T;
int cas = 0;
scanf("%d", &T);
while(T--) {
int n, k;
scanf("%d%d", &n, &k);
for(int i = 0; i < n; ++i) {
scanf("%d%d", &a[i].X, &a[i].Y);
}
printf("Case #%d: ", ++cas);
gao(n, k);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: