hdu 5046 Airport (重复覆盖)

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 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.
 

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

二分 + 重复覆盖

/*======================================================
# 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[N], D[N], R[N], L[N], row[N], col[N];
    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[N];


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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值