POJ2749——Building roads
来源:互联网 发布:现代汉语语法教程淘宝 编辑:程序博客网 时间:2024/06/05 09:56
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|.
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].
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 112750 28546 15361 320556706 388710754 816612668 1938015788 160593 42 3
Sample Output
53246
Source
POJ Monthly--2006.01.22,zhucheng
哎,二分是想到了,但是姿势想不出,看了眼题解立马想通了,~~~膜拜下
题目给的条件来建图很容易想到,但是在二分的时候,要枚举点,如果距离超过max,例如2个点到第一个站的距离和超过max,那么就要连一条边,i->j' j->i'
哎,二分是想到了,但是姿势想不出,看了眼题解立马想通了,~~~膜拜下
题目给的条件来建图很容易想到,但是在二分的时候,要枚举点,如果距离超过max,例如2个点到第一个站的距离和超过max,那么就要连一条边,i->j' j->i'
#include<queue>#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int N =1010;struct node{ int from; int to; int next;}edge[N*N],redge[N*N];int head[N],rhead[N];bool instack[N];int DFN[N],block[N],color[N],in_deg[N],low[N],Stack[N],cfl[N];int index,tot,sccnum,Top,rtot,n,m;int dist1[N],dist2[N];//每一只牛到s1,s2的距离 struct node2{int u,v;}hate[N],like[N];void addedge(int from,int to){ edge[tot].from=from; edge[tot].to=to; edge[tot].next=head[from]; head[from]=tot++;}/*void raddedge(int from,int to){ redge[rtot].from=from; redge[rtot].to=to; redge[rtot].next=rhead[from]; rhead[from]=rtot++;}*/void tarjan(int u){ DFN[u]=low[u]=++index; instack[u]=1; Stack[Top++]=u; for (int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if (DFN[v]==-1) { tarjan(v); if(low[u]>low[v]) low[u]=low[v]; } else if (instack[v] && low[u]>DFN[v]) low[u]=DFN[v]; } if (DFN[u]==low[u]) { int v; sccnum++; do { Top--; v=Stack[Top]; block[v]=sccnum; instack[v]=0; } while (v!=u); }}/*void topo_sort(){ queue<int>qu; memset(color,0,sizeof(color)); for (int i=1;i<=sccnum;i++) if (in_deg[i]==0) qu.push(i); while (!qu.empty()) { int u=qu.front(); qu.pop(); if (!color[u]) { color[u]=1; color[cfl[u]]=-1; } for (int i=rhead[u];i!=-1;i=redge[i].next) { int v=redge[i].to; in_deg[v]--; if (!in_deg[v]) qu.push(v); } }}*/bool judge(){ for (int i=0;i<n;i++) { if (block[2*i]==block[2*i+1]) return false; //cfl[block[2*i]]=block[2*i+1]; //cfl[block[2*i+1]]=block[2*i]; } return true;}void init(){memset(block,-1,sizeof(block)); memset(DFN,-1,sizeof(DFN)); memset(instack,0,sizeof(instack)); index=sccnum=Top=0;}/*void r_build(){memset(in_deg,0,sizeof(in_deg));memset(rhead,-1,sizeof(rhead));rtot=0; for (int i=0;i<tot;i++) { int u=edge[i].from; int v=edge[i].to; if (block[u]!=block[v]) { raddedge(block[v],block[u]); in_deg[block[u]]++; } }}*//*void solve(){ build();init(); for (int i=1;i<=2*n;i++) if (DFN[i]==-1) tarjan(i); if (!judge()) printf("bad luck\n"); else { bool flag=false;r_build(); topo_sort();}*/int main(){int A,B,d_ab;int sx1,sy1,sx2,sy2; while (~scanf("%d%d%d",&n,&A,&B)) { scanf("%d%d%d%d",&sx1,&sy1,&sx2,&sy2); d_ab=abs(sx1-sx2)+abs(sy1-sy2); int l=0,r=40000000,mid; int a,b,ans=-1; int x,y,u,v; for(int i=0;i<n;i++) { scanf("%d%d",&x,&y); dist1[i]=abs(x-sx1)+abs(y-sy1); dist2[i]=abs(x-sx2)+abs(y-sy2); } for(int i=0;i<A;i++) { scanf("%d%d",&hate[i].u,&hate[i].v); hate[i].u--; hate[i].v--; } for(int i=0;i<B;i++) { scanf("%d%d",&like[i].u,&like[i].v); like[i].u--; like[i].v--; } while(l<=r) { mid=(l+r)>>1; memset(head,-1,sizeof(head)); tot=0; init(); for(int i=0;i<A;i++)//互相厌恶的牛,分开 { u=hate[i].u; v=hate[i].v; addedge(2*u,2*v+1); addedge(2*v,2*u+1); addedge(2*v+1,2*u); addedge(2*u+1,2*v); } for(int i=0;i<B;i++) { u=like[i].u; v=like[i].v; addedge(2*u,2*v); addedge(2*v,2*u); addedge(2*v+1,2*u+1); addedge(2*u+1,2*v+1); } for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) { if(dist1[i]+dist1[j]>mid) { addedge(2*i,2*j+1); addedge(2*j,2*i+1); } if(dist2[i]+dist2[j]>mid) { addedge(2*i+1,2*j); addedge(2*j+1,2*i); } if(dist1[i]+dist2[j]+d_ab>mid) { addedge(2*i,2*j); addedge(2*j+1,2*i+1); } if(dist2[i]+dist1[j]+d_ab>mid) { addedge(2*i+1,2*j+1); addedge(2*j,2*i); } } for(int i=0;i<2*n;i++) if(DFN[i]==-1) tarjan(i); if(judge()){ans=mid;r=mid-1;}elsel=mid+1; }printf("%d\n",ans); } return 0;}
0 0
- POJ2749——Building roads
- 2-SAT——6.0(poj2749 Building roads)
- Poj2749 Building Roads
- poj2749 Building roads
- poj2749 Building roads
- POJ2749 Building roads
- poj2749 Building roads 2-sat
- POJ2749 Building roads【2-SAT】
- poj2749:Building roads(2-SAT)
- 【二分+2-SAT验证】POJ2749[Building roads]题解
- poj3625——Building Roads
- Building Roads
- POJ--2749[Building roads] 2-SAT第五题,2—SAT+二分判定
- POJ 3625 Building Roads
- POJ3625 Building Roads
- Building Roads(3625)
- poj 3625 Building Roads
- POJ 2749 Building Roads
- 如何让公共播放器支持特殊格式文件
- 【转】g++参数大全 gcc参数大全
- VC2008精简版下载地址
- copssh 点击红色启动按钮 直接闪退或退出
- 自定义ListView的点击Item项后进入另一个activity背景色改变在pressed或selected等状态时的颜色
- POJ2749——Building roads
- 黑马程序员_函数与数组
- Director视频编码解决方案
- Function Run Fun (HDU 1331) —— 记忆化搜索DP
- Android JNI 开发之NDK环境配置
- http://www.doudoutxt.com/files/article/51465/473yz952.html
- 【转】g++编译优化详解
- PIGS(最大流_增广路算法)
- 和尚挑水