[POJ2749]Building roads(2-SAT)

Building roads

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8153   Accepted: 2772

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
12750 28546 15361 32055
6706 3887
10754 8166
12668 19380
15788 16059
3 4
2 3

Sample Output

53246

Source

POJ Monthly--2006.01.22,zhucheng

[Submit]   [Go Back]   [Status]   [Discuss]

考虑二分答案,然后根据题目给出的限制以及这个二分出来的距离限制建图,2-SAT解决。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define mem(a) memset(a,0,sizeof(a))
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 #define For(i,x) for (int i=h[x],k; i; i=nxt[i])
 7 using namespace std;
 8
 9 const int N=1010,M=N*N*2;
10 int a,b,n,mx,ans,cnt,scc,top,L,R,tim,dis,x1,y1,x2,y2,x,y,dis1[N],dis2[N],ax[N],ay[N],bx[N],by[N];
11 int to[M],nxt[M],q[N],dfn[N],inq[N],h[N],low[N],bel[N];
12 void add(int u,int v){ to[++cnt]=v; nxt[cnt]=h[u]; h[u]=cnt; }
13
14 void init(){ cnt=top=scc=tim=0; mem(h); mem(dfn); mem(inq); }
15
16 int cal(int x1,int y1,int x2,int y2){ return abs(x2-x1)+abs(y2-y1); }
17
18 void tarjan(int x){
19     dfn[x]=low[x]=++tim; inq[x]=1; q[++top]=x;
20     For(i,x) if (!dfn[k=to[i]]) tarjan(k),low[x]=min(low[x],low[k]);
21         else if (inq[k]) low[x]=min(low[x],dfn[k]);
22     if (dfn[x]==low[x]){
23         scc++; int t;
24         do { t=q[top--]; bel[t]=scc; inq[t]=0; }while(t!=x);
25     }
26 }
27
28 bool jud(int mid){
29     init();
30     rep(i,1,n) rep(j,i+1,n){
31         if (dis1[i]+dis1[j]>mid) add(i,n+j),add(j,n+i);
32         if (dis2[i]+dis2[j]>mid) add(i+n,j),add(j+n,i);
33         if (dis1[i]+dis2[j]+dis>mid) add(i,j),add(j+n,i+n);
34         if (dis2[i]+dis1[j]+dis>mid) add(i+n,j+n),add(j,i);
35     }
36     rep(i,1,a) add(ax[i],ay[i]+n),add(ay[i]+n,ax[i]),add(ay[i],ax[i]+n),add(ax[i]+n,ay[i]);
37     rep(i,1,b) add(bx[i],by[i]),add(by[i],bx[i]),add(bx[i]+n,by[i]+n),add(by[i]+n,bx[i]+n);
38     rep(i,1,2*n) if (!dfn[i]) tarjan(i);
39     rep(i,1,n) if (bel[i]==bel[i+n]) return 0;
40     return 1;
41 }
42
43 int main(){
44     freopen("poj2749.in","r",stdin);
45     freopen("poj2749.out","w",stdout);
46     while (~scanf("%d%d%d",&n,&a,&b)){
47         scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
48         dis=cal(x1,y1,x2,y2); mx=0;
49         rep(i,1,n){
50             scanf("%d%d",&x,&y);
51             dis1[i]=cal(x,y,x1,y1); dis2[i]=cal(x,y,x2,y2);
52             mx=max(mx,max(dis1[i],dis2[i]));
53         }
54         mx=mx*2+dis;
55         rep(i,1,a) scanf("%d%d",&ax[i],&ay[i]);
56         rep(i,1,b) scanf("%d%d",&bx[i],&by[i]);
57         int L=0,R=mx; ans=-1;
58         while (L<=R){
59             int mid=(L+R)>>1;
60             if (jud(mid)) ans=mid,R=mid-1; else L=mid+1;
61         }
62         printf("%d\n",ans);
63     }
64     return 0;
65 }

原文地址:https://www.cnblogs.com/HocRiser/p/8674356.html

时间: 2024-10-10 00:40:48

[POJ2749]Building roads(2-SAT)的相关文章

2-SAT入门 poj2749 Building roads

poj2749 Building roads http://poj.org/problem?id=2749 二分答案,以后构造布尔表达式. 相互讨厌是!((a and b)or(!a and !b) 化简得 (!a or !b)and(a or b) 相互喜欢是!(a and !b)or(!a and b) 化简得 (!a or b)and(a or !b) 然后枚举点对讨论一个点对和S1,S2相连会不会超过lim来添加限制. 一共有四种情况都差不多,比如都连到S1会超过lim,就添加!(!a

POJ2749 Building roads

嘟嘟嘟 最近把21天漏的给不上. 今天重温了一下2-SAT,感觉很简单.就是把所有条件都转化成如果--必然能导出--.然后就这样连边建图,这样一个强连通分量中的所有点必然都是真或者假.从而根据这个点拆点后的两个点是否在一个强连通分量里判断是否有解. 这题人很容易想到拆点:\(i\)表示\(i\)连向\(s_1\),\(i + n\)表示\(i\)连向\(s_2\). 这道题关键在于距离这个限制.我们肯定能想到二分,但是接下来我就没想出来,因为2-SAT的图的边权是没有意义的.但实际上这个也是可以

[BZOJ1626][Usaco2007 Dec]Building Roads 修建道路

1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1730  Solved: 727 [Submit][Status][Discuss] Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场之间原本就有道路相连.

hdu 1815 Building roads

Building roads Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 822    Accepted Submission(s): 239 Problem Description Farmer John's farm has N barns, and there are some cows that live in each b

poj 3625 Building Roads

题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already conn

洛谷——P2872 [USACO07DEC]道路建设Building Roads

P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms

Building roads

Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 34 Accepted Submission(s): 13   Problem Description Farmer John's farm has N barns, and there are some cows that live in each barn.

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

HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得任意牛棚两个都可以有道路联通,现在要让每个牛棚都连接一条路到S1或者S2. 有a对牛棚互相有仇恨,所以不能让他们的路连接到同一个中转站.还有b对牛棚互相喜欢,所以他们的路必须连到同一个中专站. 道路的长度是两点的曼哈顿距离. 问最小的任意两牛棚间的距离中的最大值是多少? 思路:二分距离,考虑每两个牛棚之间4种连

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

Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 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 ro