poj 2749 Building roads(2-sat)

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

题意:

 

有 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条件与上面类似

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<math.h>
  7 #include<algorithm>
  8 #include<queue>
  9 #include<set>
 10 #include<bitset>
 11 #include<map>
 12 #include<vector>
 13 #include<stdlib.h>
 14 #include <stack>
 15 using namespace std;
 16 #define PI acos(-1.0)
 17 #define max(a,b) (a) > (b) ? (a) : (b)
 18 #define min(a,b) (a) < (b) ? (a) : (b)
 19 #define ll long long
 20 #define eps 1e-10
 21 #define MOD 1000000007
 22 #define N 1<<16
 23 #define M 1<<16
 24 #define inf 1<<30
 25 int n,A,B;
 26
 27 ////////////////////////////////////////////////////////
 28 int tot;
 29 int head[N];
 30 int vis[N];
 31 int tt;
 32 int scc;
 33 stack<int>s;
 34 int dfn[N],low[N];
 35 int col[N];
 36 struct Node
 37 {
 38     int from;
 39     int to;
 40     int next;
 41 }edge[N<<3];
 42 void init()
 43 {
 44     tot=0;
 45     scc=0;
 46     tt=0;
 47     memset(head,-1,sizeof(head));
 48     memset(dfn,-1,sizeof(dfn));
 49     memset(low,0,sizeof(low));
 50     memset(vis,0,sizeof(vis));
 51     memset(col,0,sizeof(col));
 52     while(!s.empty()){
 53           s.pop();
 54     }
 55 }
 56 void add(int s,int u)//邻接矩阵函数
 57 {
 58     edge[tot].from=s;
 59     edge[tot].to=u;
 60     edge[tot].next=head[s];
 61     head[s]=tot++;
 62 }
 63 void tarjan(int u)//tarjan算法找出图中的所有强连通分支
 64 {
 65     dfn[u] = low[u]= ++tt;
 66     vis[u]=1;
 67     s.push(u);
 68     int cnt=0;
 69     for(int i=head[u];i!=-1;i=edge[i].next)
 70     {
 71         int v=edge[i].to;
 72         if(dfn[v]==-1)
 73         {
 74         //    sum++;
 75             tarjan(v);
 76             low[u]=min(low[u],low[v]);
 77         }
 78         else if(vis[v]==1)
 79           low[u]=min(low[u],dfn[v]);
 80     }
 81     if(dfn[u]==low[u])
 82     {
 83         int x;
 84         scc++;
 85         do{
 86             x=s.top();
 87             s.pop();
 88             col[x]=scc;
 89             vis[x]=0;
 90         }while(x!=u);
 91     }
 92 }
 93 bool two_sat(){
 94
 95     for(int i=0;i<2*n;i++){
 96         if(dfn[i]==-1){
 97             tarjan(i);
 98         }
 99     }
100     for(int i=0;i<n;i++){
101         if(col[2*i]==col[2*i+1]){
102             return false;
103         }
104     }
105     return true;
106 }
107 ////////////////////////////////////////
108
109
110 int x[N],y[N];
111 int hate1[N],hate2[N];
112 int like1[N],like2[N];
113 int d[N];
114 int D;
115 /////////////////////////////////////////////////
116 bool solve(int mid){
117     init();
118     for(int i=0;i<A;i++){//相互讨厌的建图
119         add(hate1[i]*2,hate2[i]*2+1);
120         add(hate2[i]*2,hate1[i]*2+1);
121         add(hate1[i]*2+1,hate2[i]*2);
122         add(hate2[i]*2+1,hate1[i]*2);
123     }
124     for(int i=0;i<B;i++){//相互喜欢的建图
125         add(like1[i]*2,like2[i]*2);
126         add(like2[i]*2,like1[i]*2);
127         add(like1[i]*2+1,like2[i]*2+1);
128         add(like2[i]*2+1,like1[i]*2+1);
129     }
130     for(int i=0;i<n;i++){
131         for(int j=i+1;j<n;j++){
132             if(d[i]+d[j]>mid){
133                 add(i*2,j*2+1);
134                 add(j*2,i*2+1);
135             }
136             if(d[i+n]+d[j+n]>mid){
137                 add(i*2+1,j*2);
138                 add(j*2+1,i*2);
139             }
140             if(d[i]+d[j+n]+D>mid){
141                 add(i*2,j*2);
142                 add(j*2+1,i*2+1);
143
144             }
145             if(d[i+n]+d[j]+D>mid){
146                 add(j*2,i*2);
147                 add(i*2+1,j*2+1);
148             }
149         }
150     }
151     if(two_sat()) return true;
152     return false;
153 }
154 ////////////////////////////////////////////////
155 int main()
156 {
157     while(scanf("%d%d%d",&n,&A,&B)==3){
158
159              int sx1,sy1,sx2,sy2;
160              scanf("%d%d%d%d",&sx1,&sy1,&sx2,&sy2);
161
162              for(int i=0;i<n;i++){
163                  scanf("%d%d",&x[i],&y[i]);
164           }
165           int num1,num2;
166           for(int i=0;i<A;i++){
167               scanf("%d%d",&hate1[i],&hate2[i]);
168               hate1[i]--;//记得--啊
169               hate2[i]--;
170
171           }
172           for(int i=0;i<B;i++){
173               scanf("%d%d",&like1[i],&like2[i]);
174               like1[i]--;//记得--啊
175               like2[i]--;
176           }
177           for(int i=0;i<n;i++){
178               d[i]=abs(x[i]-sx1)+abs(y[i]-sy1);
179               d[i+n]=abs(x[i]-sx2)+abs(y[i]-sy2);
180           }
181           D=abs(sx1-sx2)+abs(sy1-sy2);
182           int low=0;
183           int high=inf;
184           int ans=-1;//没有合适的方案输出-1,太粗心了
185           while(low<high){
186               int mid=(low+high)>>1;
187               //printf("%d\n",mid);
188               if(solve(mid)){
189                   ans=mid;
190                   high=mid;
191               }
192               else{
193
194                   low=mid+1;
195               }
196           }
197           printf("%d\n",ans);
198
199          /* int l=0, r=8000000, mid, ans=-1;
200         while(l <= r){
201             mid = (l+r)>>1;
202             //buildGraph(mid);
203             if(solve(mid)){
204                 ans = mid;
205                 r = mid-1;
206             } else
207                 l = mid+1;
208         }
209         printf("%d\n", ans);
210         */
211     }
212     return 0;
213 }

时间: 2024-10-23 16:32:21

poj 2749 Building roads(2-sat)的相关文章

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

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

题意: 给n个村庄的坐标和两个特殊点s1,s2的坐标,现在要将每个村庄连到s1或s2上,使n个村庄间的最大距离最小. 分析: 二分任意两村庄间的最大距离,用2-sat判断该最大距离是否可行,二分的时候可以顺便记录答案,不用等最后区间为空时再输出l或l-1. 代码: //poj 2749 //sep9 #include <iostream> #include <vector> using namespace std; const int maxN=1024; const int ma

POJ 1947 Rebuilding Roads (树形DP)

题意:给一棵树,在树中删除一些边,使得有一个连通块刚好为p个节点,问最少需要删除多少条边? 思路: 因为任一条边都可能需要被删除,独立出来的具有p个节点的连通块可能在任意一处地方.先从根开始DFS,然后进行树DP,dp[t][i]表示在以t为根的子树中删除i个点需要删除多少条边.dp[t][n-p]有可能是答案了,但是这种仅考虑到从树上脱落掉部分子树,那么留下的连通块通常是与1号点(树根)相连的,那如果所需要的连通块是在某棵子树中呢?将所有可能的子树取出来,若该子树节点数>=p,那么就可以在该子

POJ 1947 Rebuilding Roads(树形DP)

题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j]-1,son是u的儿子节点.初始是将所有的儿子都断开,然后-1代表的是这个儿子我需要了,不断了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream>

POJ 2749 Building roads 2-sat+二分答案

把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 #include<stack> 6 #define N 5010 7 #define INF 4000000 8 using namespace std; 9 int di

[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

Poj 1112 Rebuilding Roads(树形DP+背包)

题意:给你由N个点构成一颗树,问要孤立出一个有P个节点的子树最少需要删除多少条边.N的范围最大为150 N的范围不大,很容易想到在树上面做背包.把每个节点都看成一个背包,然后把每个儿子节点都看成是一组物品.为什么是一组呢,那是因为假设以儿子为根的节点的子树有S个节点,那么就有S+1种情况,要么将这整棵子树舍弃,要么从这个子树中取1-S个节点. 设f[i][j]为以i为根节点的子树,孤立出以i为根节点,一共含有j个节点的子树最少需要删除的边数(不包括删除i和他父亲的连接的那条边(假设i不是根节点)

HDU 1102 &amp;&amp; POJ 2421 Constructing Roads (经典MST~Prim)

链接:http://poj.org/problem?id=2421  或   http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We