HDU 5046 Airport(dlx)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5046

题意:n个城市修建m个机场,使得每个城市到最近进场的最大值最小。

思路:二分+dlx搜索判定。

const int INF=1000000005;
const int N=4444;

int m;

struct node
{
	int L[N],R[N],D[N],U[N],e;
	int col[N];
	int H[N],num[N];

	int visit[N],KK;

	void init(int n)
	{
		KK=0;
		int i;
		for(i=0;i<=n;i++)
		{
			if(i==n) L[i]=0;
			else L[i]=i+1;
			if(i==0) R[i]=n;
			else R[i]=i-1;

			num[i]=0;

			H[i]=-1;
			D[i]=U[i]=i;
		}
		e=n+1;
	}

	void add(int r,int c)
	{
		U[e]=c;
		D[e]=D[c];
		U[D[c]]=e;
		D[c]=e;
		if(H[r]<0) H[r]=L[e]=R[e]=e;
		else
		{
			L[e]=L[H[r]];
			R[e]=H[r];
			R[L[H[r]]]=e;
			L[H[r]]=e;
		}
		num[c]++;
		col[e]=c;
		e++;
	}

	void remove(int c)
	{
		int i;
		for(i=D[c];i!=c;i=D[i])
		{
			R[L[i]]=R[i];
			L[R[i]]=L[i];
		}
	}

	void resume(int c)
	{
		int i;
		for(i=U[c];i!=c;i=U[i])
		{
			L[R[i]]=R[L[i]]=i;
		}
	}

	int astar()
	{
		KK++;
		int ans=0,i,j,k;
		for(i=L[0];i!=0;i=L[i]) if(KK!=visit[i])
		{
			visit[i]=KK;
			ans++;
			for(j=D[i];j!=i;j=D[j]) for(k=L[j];k!=j;k=L[k])
			{
				visit[col[k]]=KK;
			}
		}
		return ans;
	}

	int DFS(int cnt)
	{
		if(L[0]==0) return 1;
		if(cnt+astar()>m) return 0;

		int tmp=INF,c,i;
		for(i=L[0];i!=0;i=L[i]) if(num[col[i]]<tmp)
		{
			tmp=num[col[i]];
			c=i;
		}
		for(i=D[c];i!=c;i=D[i])
		{
			remove(i);
			int j;
			for(j=L[i];j!=i;j=L[j]) remove(j);
			if(DFS(cnt+1)) return 1;

			for(j=R[i];j!=i;j=R[j]) resume(j);
			resume(i);
		}
		return 0;
	}
}A;

int a[66][2];
i64 dis[66][66];
int n;
i64 d[4444];

inline i64 dist(int i,int j)
{
	return ((i64)abs(a[i][0]-a[j][0]))+abs(a[i][1]-a[j][1]);
}

int ok(i64 M)
{
	A.init(n);
	int i,j;
	for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(dis[i][j]<=M)
	{
		A.add(i,j);
	}
	return A.DFS(0);
}

i64 cal()
{
	int num=0;
	int i,j;
	for(i=1;i<=n;i++) for(j=1;j<=n;j++) dis[i][j]=dist(i,j),d[num++]=dis[i][j];
	sort(d,d+num);
	int M=unique(d,d+num)-d;
	int low=0,high=M-1;
	i64 ans=0;

	while(low<=high)
	{
		int mid=(low+high)>>1;
		if(ok(d[mid])) ans=d[mid],high=mid-1;
		else low=mid+1;
	}
	return ans;
}

int main()
{
	int num=0;
	int T=getInt();
	while(T--)
	{
		n=getInt();
		m=getInt();
		int i;
		for(i=1;i<=n;i++) scanf("%d%d",&a[i][0],&a[i][1]);
		printf("Case #%d: ",++num);
		cout<<cal()<<endl;
	}
}
时间: 2024-10-12 16:40:02

HDU 5046 Airport(dlx)的相关文章

HDU 5046 Airport(DLX反复覆盖)

HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstdio> #include <cstring> using namespace std; const int MAXNODE = 4005; const int MAXM = 65; const int MAXN = 65; const int INF = 0x3f3f3f3f; int K;

HDU 5046 Airport(DLX重复覆盖)

HDU 5046 Airport 题目链接 题意:给定一些机场,要求选出K个机场,使得其他机场到其他机场的最大值最小 思路:二分+DLX重复覆盖去判断即可 代码: #include <cstdio> #include <cstring> using namespace std; const int MAXNODE = 4005; const int MAXM = 65; const int MAXN = 65; const int INF = 0x3f3f3f3f; int K;

HDU 5046 Airport(DLX可重复覆盖)

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

(中等) HDU 5046 Airport ,DLX+可重复覆盖+二分。

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

hdu 5046 Airport

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5046 2014 ACM/ICPC Asia Regional Shanghai Online的题,DLX重复覆盖.. 虽然不放在DLX专题我都看不出来.. 二分距离,判断条件就是K个城市能不能满足条件. #include <iostream> #include <cstring> #include <set> #include <cstdio> #include &

HDU 5046 Airport ( Dancing Links 重复覆盖 )

今年上海网络赛的一道题目 , 跟 HDU 2295 如出一辙 , 就是距离的计算一个是欧几里得距离 , 一个是曼哈顿距离 学完DLX感觉这题好水 ,就是一个裸的重复覆盖 注意下别溢出就行了 #include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> #include <stdlib.h> using na

HDU 5046 Airport ( Dancing Links 反复覆盖 )

今年上海网络赛的一道题目 , 跟 HDU 2295 如出一辙 . 就是距离的计算一个是欧几里得距离 , 一个是曼哈顿距离 学完DLX感觉这题好水 ,就是一个裸的反复覆盖 注意下别溢出即可了 #include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> #include <stdlib.h> using na

HDU 3656 二分+dlx判定

Fire station Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1308    Accepted Submission(s): 434 Problem Description A city's map can be seen as a two dimensional plane. There are N houses in

HDU 3335 Divisibility (DLX)

Divisibility Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3335 Appoint description:  System Crawler  (2015-04-10) Description As we know,the fzu AekdyCoin is famous of math,especially in the