poj 2749 Building roads (二分+拆点+2-sat)
|
Building roads
Description
Farmer John's farm has N barns, and there are some cows that live in each barn. The cows like to drop around, so John wants to build some roads to connect these barns. If he builds roads for every pair of different barns, then he must build N * (N - 1) / 2
roads, which is so costly that cheapskate John will never do that, though that's the best choice for the cows. Clever John just had another good idea. He first builds two transferring point S1 and S2, and then builds a road connecting S1 and S2 and N roads connecting each barn with S1 or S2, namely every barn will connect with S1 or S2, but not both. So that every pair of barns will be connected by the roads. To make the cows don't spend too much time while dropping around, John wants to minimize the maximum of distances between every pair of barns. That's not the whole story because there is another troublesome problem. The cows of some barns hate each other, and John can't connect their barns to the same transferring point. The cows of some barns are friends with each other, and John must connect their barns to the same transferring point. What a headache! Now John turns to you for help. Your task is to find a feasible optimal road-building scheme to make the maximum of distances between every pair of barns as short as possible, which means that you must decide which transferring point each barn should connect to. We have known the coordinates of S1, S2 and the N barns, the pairs of barns in which the cows hate each other, and the pairs of barns in which the cows are friends with each other. Note that John always builds roads vertically and horizontally, so the length of road between two places is their Manhattan distance. For example, saying two points with coordinates (x1, y1) and (x2, y2), the Manhattan distance between them is |x1 - x2| + |y1 - y2|. Input
The first line of input consists of 3 integers N, A and B (2 <= N <= 500, 0 <= A <= 1000, 0 <= B <= 1000), which are the number of barns, the number of pairs of barns in which the cows hate each other and the number of pairs of barns in which the cows are friends
with each other. Next line contains 4 integer sx1, sy1, sx2, sy2, which are the coordinates of two different transferring point S1 and S2 respectively. Each of the following N line contains two integer x and y. They are coordinates of the barns from the first barn to the last one. Each of the following A lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows hate each other. The same pair of barns never appears more than once. Each of the following B lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows are friends with each other. The same pair of barns never appears more than once. You should note that all the coordinates are in the range [-1000000, 1000000]. Output
You just need output a line containing a single integer, which represents the maximum of the distances between every pair of barns, if John selects the optimal road-building scheme. Note if there is no feasible solution, just output -1.
Sample Input 4 1 1 Sample Output 53246 Source
POJ Monthly--2006.01.22,zhucheng
|
题意:
有 N 个牛栏,如今通过一条通道(s1,s2)把他们连起来,他们之间有一些约束关系,一些牛栏不能连在同一个点,一些牛栏必须连在同一个点,如今问有没有可能把他们都连好,并且满足全部的约束关系,假设能够,输出两个牛栏之间距离最大值的最小情况。
思路:
二分枚举最长距离。用2SAT推断可行与否。最后输出答案,假设没有,那么输出-1
条件1 i,j 相互讨厌, <i,j+n> <i+n,j> <j,i+n> <j+n,i>
条件2 i,j 关系好 <i,j> <j,i> <j+n,i+n> <i+n,j+n>
条件3
1:dis(i,s1) + dis(j,s1)>m <i,j+n> <j,i+n>
2:i j都连s2的时候与上面类似
3:dis(i,s1)+dis(s1,s2)+dis(s2,j)>m <i,j> <j+n,i+n>
4:i连s2 j连s1条件与上面类似
代码:
#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
#define maxn 1005
#define MAXN 4000005
using namespace std; int n,m1,m2,num,flag,ans,tot;
int head[maxn],X[2005],Y[2005],dist1[maxn],dist2[maxn];
int scc[maxn];
int vis[maxn];
int stack1[maxn];
int stack2[maxn];
struct edge
{
int v,next;
} g[MAXN]; void init()
{
memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
memset(scc,0,sizeof(scc));
stack1[0] = stack2[0] = num = 0;
flag = 1;
}
void addedge(int u,int v)
{
num++;
g[num].v = v;
g[num].next = head[u];
head[u] = num;
}
int abs(int x)
{
if(x>=0) return x;
return -x;
}
int caldist(int x1,int y1,int x2,int y2)
{
return abs(x1-x2)+abs(y1-y2);
}
void dfs(int cur,int &sig,int &cnt)
{
if(!flag) return;
vis[cur] = ++sig;
stack1[++stack1[0]] = cur;
stack2[++stack2[0]] = cur;
for(int i = head[cur]; i; i = g[i].next)
{
if(!vis[g[i].v]) dfs(g[i].v,sig,cnt);
else
{
if(!scc[g[i].v])
{
while(vis[stack2[stack2[0]]] > vis[g[i].v])
stack2[0] --;
}
}
}
if(stack2[stack2[0]] == cur)
{
stack2[0] --;
++cnt;
do
{
scc[stack1[stack1[0]]] = cnt;
int tmp = stack1[stack1[0]];
if((tmp >= n && scc[tmp - n] == cnt) || (tmp < n && scc[tmp + n] == cnt))
{
flag = false;
return;
}
}
while(stack1[stack1[0] --] != cur);
}
}
void Twosat()
{
int i,sig,cnt;
sig = cnt = 0;
for(i=0; i<n+n&&flag; i++)
{
if(!vis[i]) dfs(i,sig,cnt);
}
}
void solve()
{
int i,j,u,v,t,le=0,ri=4000000,mid;
ans=-1;
while(le<=ri)
{
mid=(le+ri)>>1;
init();
num=0;
for(i=1;i<=m1;i++)
{
u=X[i],v=Y[i];
addedge(u,v+n);
addedge(u+n,v);
addedge(v,u+n);
addedge(v+n,u);
}
for(i=m1+1;i<=m1+m2;i++)
{
u=X[i],v=Y[i];
addedge(u,v);
addedge(v,u);
addedge(u+n,v+n);
addedge(v+n,u+n);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j) continue ;
if(dist1[i]+dist1[j]>mid) addedge(i,j+n);
if(dist2[i]+dist2[j]>mid) addedge(i+n,j);
if(dist1[i]+dist2[j]+tot>mid) addedge(i,j);
if(dist2[i]+dist1[j]+tot>mid) addedge(i+n,j+n);
}
}
Twosat();
if(flag)
{
ans=mid;
ri=mid-1;
}
else le=mid+1;
}
}
int main()
{
int i,j,t,x,y,x1,y1,x2,y2;
while(~scanf("%d%d%d",&n,&m1,&m2))
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
tot=caldist(x1,y1,x2,y2);
for(i=0; i<n; i++)
{
scanf("%d%d",&x,&y);
dist1[i]=caldist(x,y,x1,y1);
dist2[i]=caldist(x,y,x2,y2);
}
for(i=1;i<=m1+m2;i++)
{
scanf("%d%d",&X[i],&Y[i]);
X[i]--; Y[i]--;
}
solve();
printf("%d\n",ans);
}
return 0;
}
poj 2749 Building roads (二分+拆点+2-sat)的更多相关文章
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- POJ 2749 Building roads 2-sat+二分答案
把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check #include<cstdio> #include<algorith ...
- [poj] 2749 building roads
原题 2-SAT+二分答案! 最小的最大值,这肯定是二分答案.而我们要2-SATcheck是否在该情况下有可行解. 对于目前的答案limit,首先把爱和恨连边,然后我们n^2枚举每两个点通过判断距离来 ...
- poj 3625 Building Roads
题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...
- POJ Building roads [二分答案 2SAT]
睡觉啦 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...
- POJ 2749 2SAT判定+二分
题意:图上n个点,使每个点都与俩个中转点的其中一个相连(二选一,典型2-sat),并使任意两点最大 距离最小(最大最小,2分答案),有些点相互hata,不能选同一个中转点,有些点相互LOVE,必需选相 ...
- poj 3625 Building Roads(最小生成树,二维坐标,基础)
题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> ...
- poj 2749
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6091 Accepted: 2046 De ...
- Building roads
Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互(转)
http://blog.csdn.net/javanian/article/details/8194265
- 你好,C++(33)对象生死两茫茫 6.2.3 一个对象的生与死:构造函数和析构函数
6.2.2 使用类创建对象 完成某个类的声明并且定义其成员函数之后,这个类就可以使用了.一个定义完成的类就相当于一种新的数据类型,我们可以用它来定义变量,也就是创建这个类所描述的对象,表示现实世界中 ...
- Java获取昨天的时间
Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); String yesterday = new ...
- Linux(Debian)上安装Redis教程
-- 第一步下载文件到该目录 cd /usr/local/src wget http:.tar.gz 解压 tar xzf redis.tar.gz -- 第二步编译安装 make make all ...
- 适应手机端的jQuery图片滑块动画
一款比较特别的jQuery图片滑块插件,它不仅在PC浏览器上可以使用,而且更适合在手机端的网页中使用.这款jQuery插件不仅可以定义图片切换的方向,而且可以即时切换图片切换的动画方式,可以有平移.翻 ...
- C语言到底怎么分配空间
程序分为:代码区.数据区.bss区.堆区.栈区.平时常用区分的是代码区.堆区.栈区.下面加上例子区分一下. 3 代码区顾名思义就是存放代码的,里面的内容是不可以修改的.例如你定义了一个变量char * ...
- linux c最简单的加密程序
最初的密码程序是在Hirst First c里面看到的,大概内容如下:对待加密的字符串的每一个字符和某个数值进行一次按位异或得到密文,再进行一次按位异或得到明文. 补充知识:按位异或的结果是“同位得1 ...
- Android模拟器genymotion安装与eclipse 插件安装
推荐一款Android模拟器"Genymotion",有点速度快,占用资源少,可整合eclipse.闲话少谈,看安装步骤. 1.下载地址:https://www.genymotio ...
- JavaBean与EJB的区别与应用
JavaBean 是一种组件,它在内部有接口或有与其相关的属性,以便不同人在不同时间开发的 bean 可以询问和集成. EJB 是部署在服务器上的可执行组件或商业对象.有一个协议允许对其进行远程访问或 ...
- Solr4.8.0源码分析(10)之Lucene的索引文件(3)
Solr4.8.0源码分析(10)之Lucene的索引文件(3) 1. .si文件 .si文件存储了段的元数据,主要涉及SegmentInfoFormat.java和Segmentinfo.java这 ...