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. 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
|
Sample Output
|
Source
POJ Monthly - 2006.01.22 - zhucheng
|
Recommend
威士忌
|
- /*
- 题意:有n个牛棚,给出坐标,农夫想先建两个中转站s1,s2,然后每个牛棚通过中转站进行相互联通,但是给出a,b牛棚的牛相互厌恶
- 不能通过同一个中转站,c,d两个牛棚的牛相互喜欢,必须通过同一个中转站。s1 ,s2是连通的中间有一条路,现在让你求怎么样
- 建边,才能使这些牛棚距离最大的两个牛棚的距离最小。
- 题意:和maximum shortest distance(最大团)相似,就是二分距离建边,然后判断的时候只需要用2-SAT跑一下看是否可以解决就可
- 以,问题的关键就在于如何建边,每次二分判断的时候,先按照A B的要求进行建边,然后按照如果距离大于mid的建边,然后再判
- 段是不是可以解决2-SAT问题。
- #错误:RE正在debug
- build 写的不是很好
- */
- #include<bits/stdc++.h>
- using namespace std;
- /*********************************************2-SAT模板*********************************************/
- const int maxn=+;
- struct TwoSAT
- {
- int n;//原始图的节点数(未翻倍)
- vector<int> G[maxn*];//G[i].j表示如果mark[i]=true,那么mark[j]也要=true
- bool mark[maxn*];//标记
- int S[maxn*],c;//S和c用来记录一次dfs遍历的所有节点编号
- //从x执行dfs遍历,途径的所有点都标记
- //如果不能标记,那么返回false
- bool dfs(int x)
- {
- if(mark[x^]) return false;//这两句的位置不能调换
- if(mark[x]) return true;
- mark[x]=true;
- S[c++]=x;
- for(int i=;i<G[x].size();i++)
- if(!dfs(G[x][i])) return false;
- return true;
- }
- void init(int tol)
- {
- n=tol;
- for(int i=;i<*tol;i++)
- G[i].clear();
- memset(mark,,sizeof(mark));
- }
- //加入(x,xval)或(y,yval)条件
- //xval=0表示假,yval=1表示真
- void add_clause(int x,int xval,int y,int yval)//这个地方不是一尘不变的,而是参照问题的约束条件进行加边
- {
- x=x*+xval;
- y=y*+yval;
- G[x^].push_back(y);//这是建双向边
- G[y^].push_back(x);
- }
- //判断当前2-SAT问题是否有解
- bool solve()
- {
- for(int i=;i<*n;i+=)
- if(!mark[i] && !mark[i+])
- {
- c=;
- if(!dfs(i))
- {
- while(c>) mark[S[--c]]=false;
- if(!dfs(i+)) return false;
- }
- }
- return true;
- }
- }TS;
- /*********************************************2-SAT模板*********************************************/
- struct Point{
- int x,y;
- Point(){}
- Point(int a,int b){
- x=a;
- y=b;
- }
- void input(){
- scanf("%d%d",&x,&y);
- }
- };
- int dis(Point a,Point b){//曼哈顿距离
- int dx=a.x-b.x;
- int dy=a.y-b.y;
- return abs(dx)+abs(dy);
- }
- int n,A,B;
- Point s1,s2;//中转站
- Point p[maxn];//牛棚的坐标
- Point a[maxn*],b[maxn*];//用来标记A B给出的关系
- int g[maxn][maxn][];//离散化两点间的距离,两点的距离总共有四种状态,都在s1,都在s2,交叉的两种
- int sTos=;//s1和s2间的距离
- void init(){//初始化出所有的两点间的距离
- for(int i=;i<n;i++){
- for(int j=;j<i;j++){
- g[i][j][]=dis(p[i],s1)+dis(p[j],s1);//都在s1
- g[i][j][]=dis(p[i],s1)+dis(p[j],s2)+sTos;//i在s1 j在s2
- g[i][j][]=dis(p[i],s2)+dis(p[j],s1)+sTos;//i在s2 j在s1
- g[i][j][]=dis(p[i],s2)+dis(p[j],s2);//都在s2
- }
- }
- }
- bool judge(int mid){//按照要求将所有的边建好
- TS.init(n); /* × */
- for(int i=;i<A;i++){//相互喜欢的,都在s1或者s2
- TS.add_clause(a[i].x-,,a[i].y-,);
- TS.add_clause(a[i].y-,,a[i].x-,);
- }
- for(int i=;i<B;i++){//相互讨厌的,只要不在一块就行
- TS.add_clause(b[i].x-,,b[i].x-,);
- TS.add_clause(b[i].y-,,b[i].y-,);
- TS.add_clause(b[i].x-,,b[i].y-,);
- TS.add_clause(b[i].y-,,b[i].x-,);
- }
- for(int i=;i<n;i++){ /* √ */
- for(int j=;j<i;j++){
- if(g[i][j][]>mid){
- TS.add_clause(i,,j,);//都在s1
- }
- if(g[i][j][]>mid){
- TS.add_clause(i,,j,);//i在s1 j在s2
- }
- if(g[i][j][]>mid){
- TS.add_clause(i,,j,);//i在s2 j在s1
- }
- if(g[i][j][]>mid){
- TS.add_clause(i,,j,);//都在s2
- }
- }
- }
- return TS.solve();
- }
- int main(){
- // freopen("in.txt","r",stdin);
- while(scanf("%d%d%d",&n,&A,&B)!=EOF){
- s1.input();s2.input();
- // cout<<s1.x<<" "<<s1.y<<" "<<s2.x<<" "<<s2.y<<endl;
- sTos=dis(s1,s2);
- // cout<<sTos<<endl;
- for(int i=;i<n;i++){
- p[i].input();
- // cout<<p[i].x<<" "<<p[i].y<<endl;
- }//处理点的输入
- init();
- // for(int i=0;i<n;i++){
- // for(int j=0;j<n;j++){
- // cout<<g[i][j][0]<<" ";
- // }cout<<endl;
- // }
- for(int i=;i<A;i++){
- a[i].input();
- // cout<<a[i].x<<" "<<a[i].y<<endl;
- }
- for(int i=;i<B;i++){
- b[i].input();
- // cout<<b[i].x<<" "<<b[i].y<<endl;
- }
- if(judge()==false){//如果这种状态不可能的就直接输出就行了
- puts("-1");
- continue;
- }
- int l=,r=;
- while(l<r){
- // cout<<l<<" "<<r<<endl;
- int mid=(l+r)/;
- if(judge(mid)==false)
- l=mid+;
- else r=mid;
- }
- printf("%d\n",l);
- }
- return ;
- }
Building roads的更多相关文章
- poj 3625 Building Roads
题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...
- poj 2749 Building roads (二分+拆点+2-sat)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6229 Accepted: 2093 De ...
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )
计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads
P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...
- [POJ2749]Building roads(2-SAT)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8153 Accepted: 2772 De ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树
1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer J ...
- 洛谷——P2872 [USACO07DEC]道路建设Building Roads
P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...
- USACO Building Roads
洛谷 P2872 [USACO07DEC]道路建设Building Roads 洛谷传送门 JDOJ 2546: USACO 2007 Dec Silver 2.Building Roads JDOJ ...
随机推荐
- FS BPM 业余研发(用户详细操作手册--单人串行/并行)之 深圳分公司技术部请假审批流程
1.FS BPM 简介 BPM软件中BPM是英文字母缩写,大致有二个意思.第一.Business Process Management,即业务流程管理,是一套达成企业各种业 务环节整合的全面管理模式. ...
- linux文件权限解析(摘)
用户组 在linux中的每个用户必须属于一个组,不能独立于组外.在linux中每个文件有所有者.所在组.其它组的概念 - 所有者 - 所在组 - 其它组 - 改变用户所在的组 所有者 一般为文件的创建 ...
- 7-21(排序) PAT排名汇总
计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科学的评价计算机程序设计人才, ...
- FZU 1919 -- K-way Merging sort(记忆化搜索)
题目链接 Problem Description As we all known, merge sort is an O(nlogn) comparison-based sorting algorit ...
- Redis缓存项目应用架构设计一
一些项目整理出的项目中引入缓存的架构设计方案,希望能帮助你更好地管理项目缓存,作者水平有限,如有不足还望指点. 一.基础结构介绍 项目中对外提供方法的是CacheProvider和MQProvider ...
- PDO浅谈之php连接mysql
一.首先我们先说一下什么是pdo? 百科上说 PDO扩展为PHP访问数据库定义了一个轻量级的.一致性的接口,它提供了一个数据访问抽象层,这样,无论使用什么数据库,都可以通过一致的函数执行查询和获取数 ...
- sql2008 发送邮件
--"管理"-"数据库邮件"-右键"配置数据库右键" Exec msdb.dbo.sp_send_dbmail @profile_name= ...
- Bootstrap表格样式(附源码文件)--Bootstrap
1.表格默认样式 <h4>表格默认样式</h4><table><!--默认样式--> <tr><th>序号</th> ...
- 转载:C#特性-表达式树
原文地址:http://www.cnblogs.com/tianfan/ 表达式树基础 刚接触LINQ的人往往觉得表达式树很不容易理解.通过这篇文章我希望大家看到它其实并不像想象中那么难.您只要有普通 ...
- 南天PR2、PR2E驱动下载,xp,win7,win8,win8.1,win10 32位64位驱动下载安装教程
家里开淘宝店,有个针式打印机驱动.电脑各种换系统,为了装这个驱动可是废了不小的劲.不敢独享,所以现在把各种驱动以及安装教程分享出来. 注意: 打印机在开机状态下,电脑在开机状态下,不要插拔连接线!!! ...