hdu 5046 二分+DLX模板
http://acm.hdu.edu.cn/showproblem.php?pid=5046
n城市建k机场使得,是每个城市最近机场的距离的最大值最小化
二分+DLX
模板题
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
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 n;
LL s[65][65];
LL le[10005];
struct point{
LL x,y;
}p[65];
int ok(LL M)
{
A.init(n);
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(s[i][j]<=M)
A.add(i,j);
return A.DFS(0);
} int main(){
int _,cas = 1;
RD(_);
while(_--){
RD2(n,m);
for(int i = 1;i <= n;++i){
scanf("%I64d%I64d",&p[i].x,&p[i].y);
}
int mm = 0;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
LL len = abs(p[i].x - p[j].x) + abs(p[i].y - p[j].y);
s[i][j] = s[j][i] = len;
le[mm++] = len;
}
}
sort(le,le+mm);
mm = unique(le,le+mm)-le;
int l = 0,r = mm - 1;
LL ans = 0;
while(l <= r){
LL mid = (l + r)>>1;
if(ok(le[mid]))
ans = le[mid],r = mid - 1;
else l = mid + 1;
}
printf("Case #%d: ",cas++);
cout<<ans<<endl;
}
return 0;
}
hdu 5046 二分+DLX模板的更多相关文章
- HDU 5046 Airport(dlx)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意:n个城市修建m个机场,使得每个城市到最近进场的最大值最小. 思路:二分+dlx搜索判定. ...
- HDU 3656 二分+dlx判定
Fire station Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 5046 Airport(DLX反复覆盖)
HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstd ...
- hdu 4024 二分
转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html 一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...
- poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析
题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...
- [ACM] POJ 3740 Easy Finding (DLX模板题)
Easy Finding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 4343 Des ...
- HDU 5046
同样是二分+DLX即可. #include <iostream> #include <cstdio> #include <cstring> #include < ...
- LeetCode 二分查找模板 I
模板 #1: int binarySearch(vector<int>& nums, int target){ if(nums.size() == 0) return -1; in ...
- (中等) HDU 5046 Airport ,DLX+可重复覆盖+二分。
Description The country of jiuye composed by N cites. Each city can be viewed as a point in a two- d ...
随机推荐
- css 设置元素背景为透明
要设置某一元素的背景为透明,在 chrome .firefox.opera 下是这样的: rgba 中的最后一个参数 0.4 就是想要的透明度,范围在0-1之间. 在 ie 中一般是这样的: filt ...
- Ansible 从MySQL数据库添加或删除用户
mysql_user - 从MySQL数据库添加或删除用户. 概要 要求(在执行模块的主机上) 选项 例子 笔记 状态 支持 概要 从MySQL数据库添加或删除用户. 要求(在执行模块的主机上) My ...
- pip & Jinja2
[pip&Jinja2] 2.7.9 以及上版本的 python 安装程序会自动安装 pip. 升级:pip install --upgrade pip. 使用pip安装Jinja2:sudo ...
- ORACLE用户永不被锁
alter profile default limit FAILED_LOGIN_ATTEMPTS UNLIMITED;
- Python bytearray() 函数
Python bytearray() 函数 Python 内置函数 描述 bytearray() 方法返回一个新字节数组.这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x < ...
- 不同的子序列 · Distinct Subsequences
[抄题]: 给出字符串S和字符串T,计算S的不同的子序列中T出现的个数. 子序列字符串是原始字符串通过删除一些(或零个)产生的一个新的字符串,并且对剩下的字符的相对位置没有影响.(比如,“ACE”是“ ...
- OC 线程操作 - GCD快速迭代
- (void)forDemo{ //全都是在主线程操作的 ; i<; i++) { NSLog(@"--%@", [NSThread currentThread]); } ...
- [leetcode]689. Maximum Sum of 3 Non-Overlapping Subarrays三个非重叠子数组的最大和
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...
- appcache的一个特殊用法
Application Cache是HTML5里出现的用来实现离线应用的技术方案.在使用了appcache的页面会被缓存,同时浏览器检查manifest文件有没有变化,如果有变化,只有当用户下次进行访 ...
- Linux 添加硬盘
一.简介 本文介绍为Linux 添加硬盘的基本方法,同时适用于为虚拟机添加硬盘的情况. 二.添加小于2T的硬盘 1)分区 fdisk /dev/hda 2)建立文件系统 3)设置开机自动挂载磁盘 ...