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 ...
随机推荐
- 第一个struct2程序(2)
第三步 需要使用ActionForm了.在Struts1.x中,必须要单独建立一个ActionForm类(或是定义一个动作Form),而在Struts2中ActionForm和Action已经二合一了 ...
- [图解tensorflow源码] 入门准备工作
tensorflow使用了自动化构建工具bazel.脚本语言调用c或cpp的包裹工具swig.使用EIGEN作为矩阵处理工具.Nvidia-cuBLAS GPU加速计算库.结构化数据存储格式prot ...
- linux 批量删除文件名中有换行符
ls -i | grep ^M | awk '{print $1}' | xargs -t -I [] find . -inum [] -exec rm -if {} \; 注意^M 是ctrl+v ...
- RecycleView实现侧滑删除item
对于列表空间的侧滑操作,网上有很多开源的空间可以使用,Google在它的新控件RecycleView中增加了侧滑的API,完全遵循Material Design设计规范,下面看看效果演示: 下面看看介 ...
- ThreadPoolExecutor的execute源码分析
上一篇文章指出,ThreadPoolExecutor执行的步骤如下: 向线程池中添加任务,当任务数量少于corePoolSize时,会自动创建thead来处理这些任务: 当添加任务数大于corePoo ...
- Android笔记:RelativeLayout
RelativeLayout 又称作相对布局,也是一种非常常用的布局.和 LinearLayout 的排列规则不同,RelativeLayout 显得更加随意一些,它可以通过相对定位的方式让控件出现在 ...
- inner join和outer join
内连接 只连接匹配的行 左外连接 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行 A left join B等价于A left ...
- mongo通信协议
先是一个包头: struct MsgHeader { int32 messageLength; // total message size, including this int32 requestI ...
- 转)Ubuntu安装mysql5.7
主要参考http://blog.csdn.net/q894523017/article/details/50705392 包去官网下载,解压,安装步骤如下: 上文中有错误,正确如下: sudo dpk ...
- Swift 项目中可能用到的第三方框架
这里记录下swift开发中可能用的框架 , 最近浏览了不少,积累在这里,以后用的时候方便查阅.顺便推荐给大家! 这里的框架都是纯swift的 , 感谢开源 ,感谢大神们 . 下拉刷新 BreakOut ...