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 ...
随机推荐
- git 拉取某个分支到本地
git 拉取其实只需要 git fetch origin xxx. git pull origin xxx即可
- javascript 函数对象
http://hi.baidu.com/gdancer/blog/item/a59e2c12479b4e54f919b814.html jQuery的一些写法就是基于这篇文章的原理的.. 函数 ...
- ingress 密码验证
traefik ingress 上面的方式需要引入haprox或者nginx,多引入了一个代理转发层,其实ingress本身就提供了basic auth的支持,在ingress规则中添加额外的认证an ...
- 第六章 图(b1)邻接矩阵
- java小知识点简单回顾
1.java的数据类型分为两种:简单类型和引用类型(数组.类以及接口).注意,java没有指针的说法,只有引用.简单类型的变量被声明时,存储空间也同时被分配:而引用类型声明变量(对象)时,仅仅为其分配 ...
- Python bool() 函数
Python bool() 函数 Python 内置函数 描述 bool() 函数用于将给定参数转换为布尔类型,如果没有参数,返回 False. bool 是 int 的子类. 语法 以下是 boo ...
- [leetcode]238. Product of Array Except Self除了自身以外的数组元素乘积
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- 给dede添加栏目图片和栏目描述
有的时候我们希望调用栏目时把栏目的图片和描述调出来,但dede好像没有提供栏目图片这个功能,而栏目的描述也是给meta:Description使用的,不是很方便. 所以我们需要自已给dede添加图 ...
- Halcon小函数的封装和代码导出
一.Halcon小函数的封装和修改 1.名词解释: 算子:指Halcon中最基础.最底层的函数(即你看不到它的代码实现),一个算子只有一句话,例如threshold算子. 小函数:由多个算子组合成的函 ...
- golang开发集训营
初识golang Go与VScode开发工具 Golang开门见山