hdu5046(重复覆盖+二分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5046
题意:要在n个城市里建造不超过k个机场覆盖所有城市,问机场城市之间最大距离最小为多少。
分析:二分距离+DLX判断,n个城市n列,然后n行,每行城市i在二分的距离内能到达列j就标为1,问题转化为选不超过k行来覆盖所有列(可重复覆盖)。
注意:最大距离为4*1e9,爆int了,要用long long,不然会TLE...
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 10007
#define inf 0x3f3f3f3f
#define N 100010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxnode=;
const int MaxN=;
const int MaxM=;
int k;
struct DLX
{
int n,m,size;
int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
int H[MaxN],S[MaxM];
int ansd,ans[MaxN];
void init(int _n,int _m)
{
n=_n;m=_m;
for(int i=;i<=m;i++)
{
S[i]=;
U[i]=D[i]=i;
L[i]=i-;
R[i]=i+;
}
R[m]=;L[]=m;
size=m;
for(int i=;i<=n;i++)H[i]=-;
}
void Link(int r,int c)
{
++S[Col[++size]=c];
Row[size]=r;
D[size]=D[c];
U[D[c]]=size;
U[size]=c;
D[c]=size;
if(H[r]<)H[r]=L[size]=R[size]=size;
else
{
R[size]=R[H[r]];
L[R[H[r]]]=size;
L[size]=H[r];
R[H[r]]=size;
}
}
void Remove(int c)
{
for(int i = D[c];i != c;i = D[i])
L[R[i]] = L[i], R[L[i]] = R[i];
}
void Resume(int c)
{
for(int i = U[c];i != c;i = U[i])
L[R[i]]=R[L[i]]=i;
} bool vis[maxnode];
int h()
{
int res=;
for(int c=R[];c!=;c=R[c])vis[c]=true;
for(int c=R[];c!=;c=R[c])
if(vis[c])
{
res++;
vis[c]=false;
for(int i=D[c];i!=c;i=D[i])
for(int j=R[i];j!=i;j=R[j])
vis[Col[j]]=false;
}
return res;
}
bool Dance(int d)//d为递归深度
{
if(d+h()>k)return false;
if(R[]==)//找到解
return d<=k;
//找S最小的C列
int c=R[];//第一个未删除的列
for(int i=R[];i!=;i=R[i])
if(S[i]<S[c])c=i;
for(int i=D[c];i!=c;i=D[i])//用结点i所在的行覆盖第c列
{
Remove(i);
for(int j=R[i];j!=i;j=R[j])Remove(j);//删除节结点i所在行覆盖第c列
if(Dance(d+))return true;
for(int j=L[i];j!=i;j=L[j])Resume(j);// 恢复
Resume(i);
}
return false;
}
};
DLX g;
struct node
{
LL x,y;
}c[MaxN];
LL dist(node a,node b)
{
return (LL)((LL)abs(a.x-b.x)+(LL)abs(a.y-b.y));
}
int main()
{
int T,n,cas=;
int mnx,mxx,mny,mxy;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)scanf("%I64d%I64d",&c[i].x,&c[i].y);
LL l=,r=1000000000000LL,ans;
while(l<=r)
{
LL mid=(l+r)/;
g.init(n,n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(dist(c[i],c[j])<=mid)
g.Link(i,j);
if(g.Dance())r=mid-,ans=mid;
else l=mid+;
}
printf("Case #%d: %I64d\n",cas++,ans);
}
}
hdu5046(重复覆盖+二分)的更多相关文章
- HDU5046 Airport dancing links 重复覆盖+二分
这一道题和HDU2295是一样 是一个dancing links重复覆盖解决最小支配集的问题 在给定长度下求一个最小支配集,只要小于k就行 然后就是二分答案,每次求最小支配集 只不过HDU2295是浮 ...
- hdu3656Fire station(DLX重复覆盖 + 二分)
题目请戳这里 题目大意:一个城市n个点,现在要建m个消防站,消防站建在给定的n个点中.求建m个消防站后,m个消防站要覆盖所有的n个点的覆盖半径最小. 题目分析:重复覆盖问题,DLX解决.不过要求覆盖半 ...
- hdu2295(重复覆盖+二分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2295 题意::一个国家有n个城市,有m个地方可以建造雷达,最多可以建K个雷达(K>=1 & ...
- hdu 2295 dlx重复覆盖+二分答案
题目大意: 有一堆雷达工作站,安放至多k个人在这些工作站中,找到一个最小的雷达监控半径可以使k个工作人所在的雷达工作站覆盖所有城市 二分半径的答案,每次利用dlx的重复覆盖来判断这个答案是否正确 #i ...
- (中等) 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 ...
- (中等) HDU 2295 , DLX+重复覆盖+二分。
Description N cities of the Java Kingdom need to be covered by radars for being in a state of war. S ...
- hdu5064 DLX可重复覆盖+二分
这题题意是 给了n个城市 在其中小于等于k个城市建立机场然后 使得最远的那个离机场的城市距离最短 二分答案 ,我们对于每次的mid 重新建图然后再来一次DLX,每个点可以覆盖的点建立一条联系就ok了 ...
- hdu2295DLX重复覆盖+二分
题目是说 给了n个城市 m个雷达 你只能选择其中的k个雷达进行使用 你可以设置每个雷达的半径,最后使得所有城市都被覆盖,要求雷达的半径尽可能的小(所有雷达的半径是一样的) 二分最小半径,然后每次重新建 ...
- hdu 2295 Radar 重复覆盖+二分
题目链接 给m个雷达, n个城市, 以及每个城市的坐标, m个雷达里只能使用k个, 在k个雷达包围所有城市的前提下, 求最小半径. 先求出每个雷达到所有城市的距离, 然后二分半径, 如果距离小于二分的 ...
随机推荐
- uva 10692 - Huge Mods(数论)
题目链接:uva 10692 - Huge Mods 题目大意:给出一个数的次方形式,就它模掉M的值. 解题思路:依据剩余系的性质,最后一定是行成周期的,所以就有ab=abmod(phi[M])+ph ...
- C语言总结之---关键字
我记得我开始学习C语言的时候,那时候还在读高中,我们老师就把C语言的关键字,全部写在黑板上,老师说我们下面的两节课的内容就是(把它给记下来) 你还记得标准C有多少个关键字吗? 第一:关键字描述 C99 ...
- JavaScript编程:使用DOM操作样式表
6.使用DOM操作样式表: 操纵元素的Style样式属性: background-color:style.backgroundColor color:style.col ...
- Swift - 类型嵌套(以扑克牌结构体为例)
类型嵌套,简单来说实在一个类型中包含另外一个类型.我们拿一副扑克来说明. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 //类 ...
- [Ext JS 4]性能优化
一般的优化技巧 1. 检查你定义的时间监听器 正确的设置事件监听器对性能会有很大的影响. 举例来说, 在定义一个store的时候,设置一个load 的事件去触发从后台读取数据,如果设置single 的 ...
- uploadfiy使用
动态加参数:$("#file_upload").uploadify("settings", "formData", { knowledgeI ...
- Spring Security Source Code -- 验证标准流程
除了初始阶段: 主干验证流程链: MyInvocationSecurityMetadataSource.getAttributes(Object) line: 43 MyFilterSecur ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- abap alv multiple header using write
A standard SAP ALV list report will show only one line header, but there will be a requirement somed ...
- ural 1519 Formula 1
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1519 题目分类:插头dp 题意:求经过所有可行点的哈密顿回路的个数 * 不可走 . 可 ...