HDU5046-Airport

19 篇文章 0 订阅

Airport

                                                                      Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                              Total Submission(s): 2247    Accepted Submission(s): 711


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
 

题意:有n个城市,要再其中k个城市中建机场,问每个城市到机场最近距离的最大值的最小值是多少

解题思路:二分+舞蹈链重复覆盖


#include <iostream>      
#include <cstdio>      
#include <cstring>      
#include <string>      
#include <algorithm>      
#include <cctype>      
#include <map>      
#include <cmath>      
#include <set>      
#include <stack>      
#include <queue>      
#include <vector>      
#include <bitset>      
#include <functional>      

using namespace std;

#define LL long long      
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 300005;

int n, K;
LL x[1009], y[1009];

struct DLX
{
	int L[maxn], R[maxn], U[maxn], D[maxn];
	int row[maxn], col[maxn], sum[maxn], ans[maxn];
	int n, m, num, cnt;
	int vis[maxn];
	void add(int k, int l, int r, int u, int d, int x, int y)
	{
		L[k] = l;   R[k] = r;   U[k] = u;
		D[k] = d;   row[k] = x;  col[k] = y;
	}
	void reset(int n, int m)
	{
		this->n = n;   this->m = m;
		for (int i = 0; i <= m; i++)
		{
			add(i, i - 1, i + 1, i, i, 0, i);
			sum[i] = 0;
		}
		L[0] = m, R[m] = 0, cnt = m + 1;
	}
	void insert(int x, int y)
	{
		int temp = cnt - 1;
		if (row[temp] != x)
		{
			add(cnt, cnt, cnt, U[y], y, x, y);
			U[D[cnt]] = cnt; D[U[cnt]] = cnt;
		}
		else
		{
			add(cnt, temp, R[temp], U[y], y, x, y);
			R[L[cnt]] = cnt; L[R[cnt]] = cnt;
			U[D[cnt]] = cnt; D[U[cnt]] = cnt;
		}
		sum[y]++, cnt++;
	}
	void Remove(int k)
	{
		for (int i = D[k]; i != k; i = D[i])
		{
			L[R[i]] = L[i];
			R[L[i]] = R[i];
		}
	}
	void Resume(int k)
	{
		for (int i = U[k]; i != k; i = U[i]) L[R[i]] = R[L[i]] = i;
	}
	int A()
	{
		int dis = 0;
		for (int i = R[0]; i != 0; i = R[i]) vis[i] = 0;
		for (int i = R[0]; i != 0; i = R[i])
			if (!vis[i])
			{
				dis++, vis[i] = 1;
				for (int j = D[i]; j != i; j = D[j])
					for (int k = R[j]; k != j; k = R[k])
						vis[col[k]] = 1;
			}
		return dis;
	}
	bool Dfs(int k)
	{
		if (!R[0]) { num = min(num, k); return true; }
		else if (k + A() > K) { num = 0x7FFFFFFF; return false; }
		else
		{
			int now = R[0];
			for (int i = R[0]; i != 0; i = R[i])
				if (sum[now] > sum[i]) now = i;
			for (int i = D[now]; i != now; i = D[i])
			{
				Remove(i);
				for (int j = R[i]; j != i; j = R[j]) Remove(j);
				if (Dfs(k + 1)) return true;
				for (int j = L[i]; j != i; j = L[j]) Resume(j);
				Resume(i);
			}
		}
		return false;
	}
	void mul()
	{
		num = 0x7FFFFFFF;
	}
}dlx;

int check(LL k)
{
	dlx.reset(n, n);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			LL temp = abs(x[i] - x[j]) + abs(y[i] - y[j]);
			if (k >= temp) dlx.insert(i, j);
		}
	dlx.mul();
	dlx.Dfs(0);
	if (dlx.num <= K) return 1;
	return 0;
}

int main()
{
	int t, cas = 0;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &K);
		for (int i = 1; i <= n; i++) scanf("%lld%lld", &x[i], &y[i]);
		LL l = 0, r = 0 ,ans;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				r = max(r, abs(x[i] - x[j]) + abs(y[i] - y[j]));
		while (r >= l)
		{
			LL mid = (l + r) / 2;
			if (check(mid)) r = mid-1, ans = mid;
			else l = mid + 1;
		}
		printf("Case #%d: %lld\n", ++cas, ans);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值