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