[poj] 2749 building roads
原题
2-SAT+二分答案!
最小的最大值,这肯定是二分答案。而我们要2-SATcheck是否在该情况下有可行解。
对于目前的答案limit,首先把爱和恨连边,然后我们n^2枚举每两个点通过判断距离来实现连边,然后跑2-SAT判断是否有可行解
O(n^2logn)
想起来和听起来都很难写,事实上还好吧…
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cstring>
#define inf 97797977
#define N 510
using namespace std;
int n,m,A,B,lovex[2*N],lovey[2*N],hatex[2*N],hatey[2*N],dis1[N],dis2[N],dis,sx1,sx2,sy1,sy2,x[N],y[N],head[2*N];
int bel[2*N],cnt=1,l,r=inf,dfn[2*N],low[2*N],t,mid,sum;
stack <int> stk;
bool instk[2*N];
struct hhh
{int to,next;
}edge[10*N*N];int read()
{int ans=0,fu=1;char j=getchar();for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;if (j=='-') j=getchar(),fu=-1;for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';return ans*fu;
}void add(int u,int v)
{edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++;
}bool build()
{for (int i=1;i<=B;i++){add(lovex[i],lovey[i]);add(lovey[i],lovex[i]);add(lovex[i]+n,lovey[i]+n);add(lovey[i]+n,lovex[i]+n);}for (int i=1;i<=A;i++){add(hatex[i],hatey[i]+n);add(hatex[i]+n,hatey[i]);add(hatey[i]+n,hatex[i]);add(hatey[i],hatex[i]+n);}for (int i=1;i<=n;i++)for (int j=i+1;j<=n;j++){int t=0;if (dis1[i]+dis+dis2[j]>mid){add(i,j);add(j+n,i+n);t++;}if (dis2[i]+dis+dis1[j]>mid){add(i+n,j+n);add(j,i);t++;}if (dis1[i]+dis1[j]>mid){add(i,j+n);add(j,i+n);t++;}if (dis2[i]+dis2[j]>mid){add(i+n,j);add(j+n,i);t++;}if (t==4) return 0;}return 1;
}void Tarjan(int u)
{dfn[u]=low[u]=++t;stk.push(u);instk[u]=1;for (int i=head[u],v;i;i=edge[i].next){v=edge[i].to;if (!dfn[v]){Tarjan(v);low[u]=min(low[u],low[v]);}else if (instk[v]) low[u]=min(low[u],dfn[v]);}if (low[u]==dfn[u]){sum++;int t;do{t=stk.top();bel[t]=sum;stk.pop();instk[t]=0;}while(t!=u);}
}bool check()
{for (int i=1;i<=n;i++)if (bel[i]==bel[i+n]) return 0;return 1;
}int get1(int i)
{return abs(sx1-x[i])+abs(sy1-y[i]);
}int get2(int i)
{return abs(sx2-x[i])+abs(sy2-y[i]);
}int main()
{n=read();A=read();B=read();sx1=read();sy1=read();sx2=read();sy2=read();dis=abs(sx1-sx2)+abs(sy1-sy2);for (int i=1;i<=n;i++){x[i]=read();y[i]=read();}for (int i=1;i<=A;i++){hatex[i]=read();hatey[i]=read();}for (int i=1;i<=B;i++){lovex[i]=read();lovey[i]=read();}for (int i=1;i<=n;i++){dis1[i]=get1(i);dis2[i]=get2(i);}while (l<r){mid=(l+r)>>1;cnt=1;memset(dfn,0,sizeof(dfn));memset(bel,0,sizeof(bel));memset(head,0,sizeof(head));if (build()){t=1;sum=1;for (int i=1;i<=2*n;i++)if (!dfn[i]) Tarjan(i);if (check()) r=mid;else l=mid+1;}else l=mid+1;}printf("%d",l>=inf?-1:l);return 0;
}
转载于:https://www.cnblogs.com/mrha/p/7856125.html
[poj] 2749 building roads相关推荐
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得随意牛棚两个 ...
- POJ 2749 Building roads 2-sat+二分答案
把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check 1 #include<cstdio> 2 #include<algo ...
- POJ 2749 Building roads
POJ_2749 一开始没有想到去二分距离,看了别人的报告之后恍然大悟. 这是一个2-SAT的问题,首先我们要去找到核心变量,可以看出每个牛的有着要么和S1相连,要么和S2相连的逻辑关系,因此可以把奶 ...
- USACO07DEC道路建设Building Roads(prim算法+堆优化与Kruskal+路径压缩对比)
目录 primprimprim算法 KruskalKruskalKruskal算法 P2872 [USACO07DEC]道路建设Building Roads 4 1 1 1 3 1 2 3 4 3 1 ...
- 洛谷——P2872 [USACO07DEC]道路建设Building Roads
P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...
- [BZOJ1626][Usaco2007 Dec]Building Roads 修建道路
1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1730 Solved ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路(最小生成树)
1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1709 Solved ...
- 【POJ 1788 --- Building a New Depot】
[POJ 1788 --- Building a New Depot] Description Advanced Cargo Movement, Ltd. is successfully expand ...
- 【POJ - 1724 】ROADS (带限制的最短路 或 dfs 或 A*算法,双权值)
题干: N cities named with numbers 1 ... N are connected with one-way roads. Each road has two paramete ...
最新文章
- undefined reference to `cv::VideoCapture::VideoCapture()‘
- Nats的消息通信模型
- 记住要重置线程上下文类加载器
- 数据库 日期格式操作
- 1.6解不等式 1.6.1 平方根不等式
- Linux下禁止使用swap及防止OOM机制导致进程被kill掉
- python vtk_Python vtk.vtkRenderer方法代码示例
- 数据结构——散列表--线性探测法
- Shell脚本之shift用法
- 查看windows产品密钥
- 1. spark ML概述
- 英文歌曲:cross every river( 穿过每一条河 )
- 计算机中c语言的应用特点,计算机中C语言应用特点.doc
- linux rc目录,linux /etc/rc.d/目录及rc.local的详解
- 解决电脑无法运行 Mscomctl.ocx控件 常见的4个问题
- 安全四大顶会论文查询
- FILE NAME(filename)文件名的理解
- 用nodejs写一个命令行应用-前言
- 苹果笔记本python怎么换行_python怎么换行,我的换行就是执行啊
- python 发红包import random用redenv_python 发红包
热门文章
- HTML 鼠标坐标和元素坐标
- iOS-----简易地CocoaAsyncSocket使用
- iOS6 自动布局 入门–Auto Layout(转)
- 不做旁观者,给博主最有力的支持——博客之星评选,期待您的支持,谢谢路过的朋友投上您宝贵的一票...
- Project Chameleon Work In Progress 11
- 10 | 案例篇:系统的软中断CPU使用率升高,我该怎么办?
- 10.13 nc:多功能网络工具
- 【Q】之Linux中的防火墙netfilter iptables
- python try catch 能知道是哪一行代码_能导致3.7亿美元损失的异常,到底是怎么回事?...
- 基于php和服务器推技术的web即时聊天系统,基于php和服务器推技术的Web即时聊天系统...