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
spent from one house number to another is equal to the distance between two points corresponding to the house numbers. The government decides to build M fire stations from N houses. (If a station is build in Pi, We can think the station is next to the house
and the time from the station to the house is considered zero.) It is obvious that if some place such as Pi is breaking out of fire, the nearest station will dispatched a fire engine quickly rushed to the rescue scene. The time it takes from this station to
the rescue scene is called rescue time. Now you need to consider about a problem that how to choice the positions of the M fire station to minimize the max rescue time of all the houses.
M is the number of fire stations. Then N lines is following. The ith line contains two integers Xi and Yi (0 ≤ Xi, Yi ≤ 10000), which stands for the coordinate of the ith house.
2
4 2
1 1
1 2
2 3
2 4
4 1
1 1
1 2
2 3
2 4
1.000000
2.236068
一開始二分距离,然后枚举建边,dlx判定tle。学习别人的写法。把两点间距离的序列排序,二分下标,这样时效高了非常多。
距离序列第二重循环仅仅枚举一半wa,所有枚举ac,非常不理解。
代码:
/* ***********************************************
Author :_rabbit
Created Time :2014/4/28 14:08:33
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
struct Point{
double x,y;
}city[600];
double dis[600][600],dd[500100];
double dist(Point a,Point b){
double s=a.x-b.x,t=a.y-b.y;
return sqrt(s*s+t*t);
}
struct DLX{
const static int maxn=311110;
#define FF(i,A,s) for(int i = A[s];i != s;i = A[i])
int L[maxn],R[maxn],U[maxn],D[maxn];
int size,col[maxn],row[maxn],s[maxn],H[maxn];
bool vis[700];
int ans[maxn],cnt;
void init(int m){
for(int i=0;i<=m;i++){
L[i]=i-1;R[i]=i+1;U[i]=D[i]=i;s[i]=0;
}
memset(H,-1,sizeof(H));
L[0]=m;R[m]=0;size=m+1;
}
void link(int r,int c){
U[size]=c;D[size]=D[c];U[D[c]]=size;D[c]=size;
if(H[r]<0)H[r]=L[size]=R[size]=size;
else {
L[size]=H[r];R[size]=R[H[r]];
L[R[H[r]]]=size;R[H[r]]=size;
}
s[c]++;col[size]=c;row[size]=r;size++;
}
void del(int c){//精确覆盖
L[R[c]]=L[c];R[L[c]]=R[c];
FF(i,D,c)FF(j,R,i)U[D[j]]=U[j],D[U[j]]=D[j],--s[col[j]];
}
void add(int c){ //精确覆盖
R[L[c]]=L[R[c]]=c;
FF(i,U,c)FF(j,L,i)++s[col[U[D[j]]=D[U[j]]=j]];
}
bool dfs(int k){//精确覆盖
if(!R[0]){
cnt=k;return 1;
}
int c=R[0];FF(i,R,0)if(s[c]>s[i])c=i;
del(c);
FF(i,D,c){
FF(j,R,i)del(col[j]);
ans[k]=row[i];if(dfs(k+1))return true;
FF(j,L,i)add(col[j]);
}
add(c);
return 0;
}
void remove(int c){//反复覆盖
FF(i,D,c)L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c){//反复覆盖
FF(i,U,c)L[R[i]]=R[L[i]]=i;
}
int A(){//估价函数
int res=0;
memset(vis,0,sizeof(vis));
FF(i,R,0)if(!vis[i]){
res++;vis[i]=1;
FF(j,D,i)FF(k,R,j)vis[col[k]]=1;
}
return res;
}
void dfs(int now,int &lim){//反复覆盖
if(R[0]==0)cnt=now,lim=min(lim,now);
else if(now+A()<lim){
int temp=INF,c;
FF(i,R,0)if(temp>=s[i])temp=s[i],c=i;
FF(i,D,c){
ans[now]=i;
remove(i);FF(j,R,i)remove(j);
dfs(now+1,lim);
FF(j,L,i)resume(j);resume(i);
}
}
}
}dlx;
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int T,n,m;
cin>>T;
while(T--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%lf%lf",&city[i].x,&city[i].y);
int pp=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
dis[i][j]=dist(city[i],city[j]);
dd[pp++]=dis[i][j];
}
sort(dd,dd+pp);
int left=0,right=pp-1;
while(left<right){
int mid=(left+right)/2;
dlx.init(n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(dis[i][j]<=dd[mid])dlx.link(i,j);
int ans=INF;
dlx.dfs(0,ans);
if(ans>m)left=mid+1;
else right=mid;
}
printf("%.6lf\n",dd[left]);
}
return 0;
}
HDU 3656 二分+dlx判定的更多相关文章
- hdu 5046 二分+DLX模板
http://acm.hdu.edu.cn/showproblem.php?pid=5046 n城市建k机场使得,是每个城市最近机场的距离的最大值最小化 二分+DLX 模板题 #include < ...
- hdu 3622 二分+2-SAT判定
思路:如题 #include<iostream> #include<algorithm> #include<cstring> #include<cstdio& ...
- HDU 5046 Airport(dlx)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意:n个城市修建m个机场,使得每个城市到最近进场的最大值最小. 思路:二分+dlx搜索判定. ...
- hdu 4024 二分
转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html 一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...
- [NOIP 2010] 关押罪犯 (二分+二分图判定 || 并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- HDU 3586 二分答案+树形DP判定
HDU 3586 『Link』HDU 3586 『Type』二分答案+树形DP判定 ✡Problem: 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...
- hdu 3656 DLX
思路:二分枚举建边,用DLX判断是否满足. #include<set> #include<cmath> #include<queue> #include<cs ...
- [DLX反复覆盖] hdu 3656 Fire station
题意: N个点.再点上建M个消防站. 问消防站到每一个点的最大距离的最小是多少. 思路: DLX直接二分推断TLE了. 这时候一个非常巧妙的思路 我们求的距离一定是两个点之间的距离 因此我们把距离都求 ...
- HDU 2295 Radar (DLX + 二分)
Radar Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
随机推荐
- 6.10---mybatis中两张表查询数据dao层
- 【Spring】IOC
浅谈IOC IOC的理论背景 图1:传统系统中,对象之间相互引用的一幅图,在采用面向对象方法设计的软件系统中,它的底层的实现都是由n个对象所组成的,所有的对象通彼此之间的合作最终实现系统的业务逻辑,如 ...
- which
功能说明:显示命令的全路径. 参数选项: -a 默认在PATH路径中由前往后查找命令,如果查找到了,就停止匹配.使用-a选项将遍历所有PATH路径,输出所有匹配项. 参数-a把所有匹配命令路 ...
- java_File对象
package File; import java.io.File; import java.io.IOException; public class file { public static voi ...
- 转录组入门(3):了解fastq测序数据
sra文件转换为fastq格式 fastq-dump -h --split-3 也就是说如果SRA文件中只有一个文件,那么这个参数就会被忽略.如果原文件中有两个文件,那么它就会把成对的文件按*_1.f ...
- swift-新手必看的基础部分
Swift 是一门开发 iOS, OS X 和 watchOS 应用的新语言.然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的. 常量和变量 ...
- 洛谷P1598 垂直柱状图
模拟题...我自己一直被光标下去上不去怎么模拟困扰,实际上可以直接从高到低,从左到右模拟 我的代码(算法借鉴题解) #include <bits/stdc++.h> using names ...
- Scrapy实战:使用IDE工具运行爬虫
一般我们运行爬虫程序都是使用命令行,比如:scrapy crwal sobook.不过这多少有些不方便,可以使用下面的方法使用IDE的方式运行爬虫 我这边使用的是pycharm软件,在pycharm里 ...
- JS布尔值(Boolean)转换规则
原文作者: louis 原文链接: http://louiszhai.github.io/2015/12/11/js.boolean/ 语法 众所周知, JavaScript有五个基本的值类型:num ...
- 32.修改IK分词器源码来基于mysql热更新词库
主要知识点, 修改IK分词器源码来基于mysql热更新词库 一.IK增加新词的原因 在第32小节中学习到了直接在es的词库中增加词语,来扩充自已的词库,但是这样做有以下缺点: (1)每次添加完 ...