二分答案 + 2-SAT验证

POJ 稳过,HDU C++ 超时,G++ 550ms左右AC

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std; const int maxn=+;
int N,A,B;
int left,right,mid;
int s1x,s1y,s2x,s2y;
struct point
{
int x,y;
int p_s1;
int p_s2;
}p[maxn];
int Hx[maxn],Hy[maxn];
int Lx[maxn],Ly[maxn]; stack<int>S;
vector<int>G[maxn];
vector<int>FG[maxn];
int Belong[maxn];
int flag[maxn];
int Block; int dis1,dis2,dis3; void init()
{
for(int i=;i<maxn;i++) G[i].clear();
for(int i=;i<maxn;i++) FG[i].clear();
memset(Belong,,sizeof Belong);
memset(flag,,sizeof flag);
while(!S.empty()) S.pop();
Block=;
} void addEgde(int x,int y)
{
G[x].push_back(y);
FG[y].push_back(x);
} int Dis(int x1,int y1,int x2,int y2)
{
return abs(x1-x2)+abs(y1-y2);
} void read()
{
scanf("%d%d%d%d",&s1x,&s1y,&s2x,&s2y);
for(int i=;i<N;i++)
scanf("%d%d",&p[i].x,&p[i].y);
for(int i=;i<=A;i++)
{
scanf("%d%d",&Hx[i],&Hy[i]);
Hx[i]--;Hy[i]--;
}
for(int i=;i<=B;i++)
{
scanf("%d%d",&Lx[i],&Ly[i]);
Lx[i]--;Ly[i]--;
}
} void dfs1(int now)
{
flag[now]=;
for(int i=;i<G[now].size();i++)
if(!flag[G[now][i]])
dfs1(G[now][i]);
S.push(now);
} void dfs2(int now)
{
Belong[now]=Block;
for(int i=;i<FG[now].size();i++)
if(!Belong[FG[now][i]])
dfs2(FG[now][i]);
} bool judge()
{
for(int i=;i<*N;i++) if(!flag[i]) dfs1(i);
while(!S.empty())
{
int Top=S.top(); S.pop();
if(!Belong[Top])
{
Block++;
dfs2(Top);
}
}
for(int i=;i<N;i++)
if(Belong[*i]==Belong[*i+])
return ;
return ;
} void solve()
{
left=;right=;
int ans=-;
while(left<=right)
{
mid=(left+right)/;
init(); for(int i=;i<N;i++){
for(int j=i+;j<N;j++)
{
int pi_s1=p[i].p_s1;
int pi_s2=p[i].p_s2;
int pj_s1=p[j].p_s1;
int pj_s2=p[j].p_s2;
int s1_s2=Dis(s1x,s1y,s2x,s2y); if(pi_s1+pj_s1>mid) {
addEgde(*i,*j+);
addEgde(*j,*i+);
} if(pi_s2+pj_s2>mid) {
addEgde(*i+,*j);
addEgde(*j+,*i);
} if(pi_s1+s1_s2+pj_s2>mid) {
addEgde(*i,*j);
addEgde(*j+,*i+);
} if(pi_s2+s1_s2+pj_s1>mid) {
addEgde(*i+,*j+);
addEgde(*j,*i);
}
} } for(int i=;i<=A;i++)
{
addEgde(*Hx[i],*Hy[i]+);
addEgde(*Hy[i]+,*Hx[i]);
addEgde(*Hx[i]+,*Hy[i]);
addEgde(*Hy[i],*Hx[i]+);
}
for(int i=;i<=B;i++)
{
addEgde(*Lx[i],*Ly[i]);
addEgde(*Ly[i],*Lx[i]);
addEgde(*Lx[i]+,*Ly[i]+);
addEgde(*Ly[i]+,*Lx[i]+);
}
if(judge()) ans=mid,right=mid-;
else left=mid+;
}
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d%d%d",&N,&A,&B))
{
read();
for(int i=;i<N;i++)
{
p[i].p_s1=Dis(p[i].x,p[i].y,s1x,s1y);
p[i].p_s2=Dis(p[i].x,p[i].y,s2x,s2y);
}
solve();
}
return ;
}

HDU 1815 Building roads的更多相关文章

  1. HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang

    感慨一下,区域赛的题目果然很费脑啊!!不过确实是一道不可多得的好题目!! 题目大意:给你一棵有n个节点的树,让你移动树中一条边的位置,即将这条边连接到任意两个顶点(边的大小不变),要求使得到的新树的直 ...

  2. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  3. poj 3625 Building Roads

    题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...

  4. poj 2749 Building roads (二分+拆点+2-sat)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 De ...

  5. BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

    计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...

  6. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  7. HDU 5033 Building(单调栈)

    HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...

  8. Building roads

    Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. HDU—— 5159 Building Blocks

    Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks. LeL ...

随机推荐

  1. how to download image from any web page in java 下载图片

    http://stackoverflow.com/questions/5882005/how-to-download-image-from-any-web-page-in-java (throws I ...

  2. AC_CONFIG_HEADER

    configure.in里有宏AC_CONFIG_HEADER()时用. AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用.

  3. 找最大重复次数的数和重复次数(C++ Pair)

    Problem A: 第一集 你好,世界冠军 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 265  Solved: 50[Submit][Statu ...

  4. Cowrie蜜罐部署教程【转载】

    0.蜜罐分类: 低交互:模拟服务和漏洞以便收集信息和恶意软件,但是攻击者无法和该系统进行交互: 中等交互:在一个特有的控制环境中模拟一个生产服务,允许攻击者的部分交互: 高交互:攻击者可以几乎自由的访 ...

  5. 使用queryperf对DNS服务器作压力测试

    一.querperf简介 当我们把DNS服务器配置好后,我们肯定会想测试一下DNS服务器的性能如何,上线后如果请求数够多服务器还能否响应?于是,我们可以使用软件模拟环境,对DNS服务器作评估性的测试. ...

  6. Android ADT安装时卡在Calculating requirements and dependencies

    AndroidSDK及Eclipse安装都很顺利,但是在Eclipse下安装ADT插件时,先采用点击Help->installnew software->Add...,无论输入https: ...

  7. 依赖注入(DI)和控制反转(IOC)【回顾】

    在java开发中广泛的使用了IOC的思想,在PHP中同样也在广泛使用. interface Coder { public function coding(); } 实现类Javaer class Ja ...

  8. 安装apache服务出错,无法启动此程序,因为计算机中丢失VCRUNTIME140.dll 尝试重新安装此程序以解决此问题

    错误信息:无法启动此程序,因为计算机中丢失VCRUNTIME140.dll 尝试重新安装此程序以解决此问题 错误场景:在使用["D:\Program Files\httpd-2.4.20-x ...

  9. Android PopupWindow菜单

    初学Android,引用了这篇文章的代码 http://www.cnblogs.com/jiezzy/archive/2012/08/15/2640584.html 使用PopupWindow制作自定 ...

  10. Windows环境下google protobuf入门

    我使用的是最新版本的protobuf(protobuf-2.6.1),编程工具使用VS2010.简单介绍下google protobuf: google protobuf 主要用于通讯,是google ...