poj 2749 Building roads(2-sat)
2015-09-17 19:34
405 查看
Description
Input
Output
Sample Input
Sample Output
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条件与上面类似
View Code
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条件与上面类似
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<math.h> #include<algorithm> #include<queue> #include<set> #include<bitset> #include<map> #include<vector> #include<stdlib.h> #include <stack> using namespace std; #define PI acos(-1.0) #define max(a,b) (a) > (b) ? (a) : (b) #define min(a,b) (a) < (b) ? (a) : (b) #define ll long long #define eps 1e-10 #define MOD 1000000007 #define N 1<<16 #define M 1<<16 #define inf 1<<30 int n,A,B; //////////////////////////////////////////////////////// int tot; int head ; int vis ; int tt; int scc; stack<int>s; int dfn ,low ; int col ; struct Node { int from; int to; int next; }edge[N<<3]; void init() { tot=0; scc=0; tt=0; memset(head,-1,sizeof(head)); memset(dfn,-1,sizeof(dfn)); memset(low,0,sizeof(low)); memset(vis,0,sizeof(vis)); memset(col,0,sizeof(col)); while(!s.empty()){ s.pop(); } } void add(int s,int u)//邻接矩阵函数 { edge[tot].from=s; edge[tot].to=u; edge[tot].next=head[s]; head[s]=tot++; } void tarjan(int u)//tarjan算法找出图中的所有强连通分支 { dfn[u] = low[u]= ++tt; vis[u]=1; s.push(u); int cnt=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(dfn[v]==-1) { // sum++; tarjan(v); low[u]=min(low[u],low[v]); } else if(vis[v]==1) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]) { int x; scc++; do{ x=s.top(); s.pop(); col[x]=scc; vis[x]=0; }while(x!=u); } } bool two_sat(){ for(int i=0;i<2*n;i++){ if(dfn[i]==-1){ tarjan(i); } } for(int i=0;i<n;i++){ if(col[2*i]==col[2*i+1]){ return false; } } return true; } //////////////////////////////////////// int x ,y ; int hate1 ,hate2 ; int like1 ,like2 ; int d ; int D; ///////////////////////////////////////////////// bool solve(int mid){ init(); for(int i=0;i<A;i++){//相互讨厌的建图 add(hate1[i]*2,hate2[i]*2+1); add(hate2[i]*2,hate1[i]*2+1); add(hate1[i]*2+1,hate2[i]*2); add(hate2[i]*2+1,hate1[i]*2); } for(int i=0;i<B;i++){//相互喜欢的建图 add(like1[i]*2,like2[i]*2); add(like2[i]*2,like1[i]*2); add(like1[i]*2+1,like2[i]*2+1); add(like2[i]*2+1,like1[i]*2+1); } for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(d[i]+d[j]>mid){ add(i*2,j*2+1); add(j*2,i*2+1); } if(d[i+n]+d[j+n]>mid){ add(i*2+1,j*2); add(j*2+1,i*2); } if(d[i]+d[j+n]+D>mid){ add(i*2,j*2); add(j*2+1,i*2+1); } if(d[i+n]+d[j]+D>mid){ add(j*2,i*2); add(i*2+1,j*2+1); } } } if(two_sat()) return true; return false; } //////////////////////////////////////////////// int main() { while(scanf("%d%d%d",&n,&A,&B)==3){ int sx1,sy1,sx2,sy2; scanf("%d%d%d%d",&sx1,&sy1,&sx2,&sy2); for(int i=0;i<n;i++){ scanf("%d%d",&x[i],&y[i]); } int num1,num2; for(int i=0;i<A;i++){ scanf("%d%d",&hate1[i],&hate2[i]); hate1[i]--;//记得--啊 hate2[i]--; } for(int i=0;i<B;i++){ scanf("%d%d",&like1[i],&like2[i]); like1[i]--;//记得--啊 like2[i]--; } for(int i=0;i<n;i++){ d[i]=abs(x[i]-sx1)+abs(y[i]-sy1); d[i+n]=abs(x[i]-sx2)+abs(y[i]-sy2); } D=abs(sx1-sx2)+abs(sy1-sy2); int low=0; int high=inf; int ans=-1;//没有合适的方案输出-1,太粗心了 while(low<high){ int mid=(low+high)>>1; //printf("%d\n",mid); if(solve(mid)){ ans=mid; high=mid; } else{ low=mid+1; } } printf("%d\n",ans); /* int l=0, r=8000000, mid, ans=-1; while(l <= r){ mid = (l+r)>>1; //buildGraph(mid); if(solve(mid)){ ans = mid; r = mid-1; } else l = mid+1; } printf("%d\n", ans); */ } return 0; }
View Code
相关文章推荐
- hdu 1242 Rescue 搜索 bfs 优先队列
- ipad开发中UIPopoverController中自定义view在Xcode6中尺寸匹配问题
- UVa 1626:Brackets Sequence(DP)
- UIday1801:沙盒三个文件的路径的获取、简单与复杂对象的读写、NSUserDefaults、NSFileManager
- 拖拽UITextView退出键盘
- APK安装出错:INSTALL_FAILED_UID_CHANGED
- android ui分析 - 今日头条UI整体结构
- IOS每天15个注意点系列之UI-应用管理
- wpf探秘之B哥Dispatcher私房菜(2)最终回
- UICollectionView
- UI —— 计算器
- request.getParameter和request.getAttribute之间的区别
- (转)Git Gui for Windows的建库、克隆(clone)、上传(push)、下载(pull)、合并
- UIday1703:UITest17 代码练习
- django1.6 CSRF verification failed. Request aborted. 用出现表单提交
- html中获取select的value值和text值
- UIscrollView的几个属性contentSize contentOffset contentInset
- ios UIWindow 错误使用导致无法接收motionEnded(摇一摇)函数
- UIAlertController和UIImagePickerController的使用
- UIday1702:KVO 观察者设计模式 代码实现 常用