Building roads

Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30 Accepted Submission(s): 12
 
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
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,要求每个农场要么和S1场地连接要么和S2场地连接,且每个农场之间的连接距离的最大值最小 ,有a对仓库不能连同一个中转站,b对仓库必须连同一个中转站。

代码:

/*
二分枚举最大距离L,判断一下每个农场可连接的场地(以下的连边表示,a表示和S1连接,!a表示和S2连接)
(前提是dis[a][s1/s2]<=L,dis[b][s1/s2]<=L......................)
如果dis[a][S1] + dis[b][S1] > L,那么表明a和b不能同时和S1连接,连边a -> !b, b->!a
如果dis[a][S2] + dis[b][S2] > L,那么表明a和b不能同时和S2连接,连边!a -> b, !b->a
如果dis[a][S1] + dis[b][S2] + dis[S1][S2] > L,那么表明a农场连接S1时,b农场不能连接S2。b农场连接S2时,a农场不能连接S1,连边 a->b, !b->!a
如果dis[a][S2] + dis[b][S1] + dis[S1][S2] > L,那么表明a农场连接S2时,b农场不能连接S1。b农场连接S1时,a农场不能连接S2,连边 !a->!b, b->a 接下来还要处理A中不可连接限制和B种连接限制.
注意:二分范围如果小了会wa的。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=;
int dis[][],x[],y[],likx[],liky[],hatx[],haty[];
/********************** 2-sat模板 **********************/
struct Twosat{
int n;
vector<int> g[maxn*];
bool mark[maxn*];
int s[maxn*],c;
bool dfs(int x){
if(mark[x^]) return false;
if(mark[x]) return true;
mark[x]=true;
s[c++]=x;
for(int i=;i<(int)g[x].size();i++)
if(!dfs(g[x][i])) return false;
return true;
}
void init(int n){
this->n=n;
for(int i=;i<n*;i++) g[i].clear();
memset(mark,,sizeof(mark));
}
void add_clause(int x,int xval,int y,int yval){//这个函数随题意变化
x=x*+xval;
y=y*+yval;
g[x].push_back(y);
}
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;
}
};
/*********************** 2-sat模板 ************************/
int main(){
int n,A,B,a,b;
Twosat solver;
while(~scanf("%d%d%d",&n,&A,&B)){
scanf("%d%d%d%d",&x[n],&y[n],&x[n+],&y[n+]);
for(int i=;i<n;i++){
scanf("%d%d",&x[i],&y[i]);
dis[i][n]=dis[n][i]=(fabs(x[i]-x[n])+fabs(y[i]-y[n]));
dis[i][n+]=dis[n+][i]=(fabs(x[i]-x[n+])+fabs(y[i]-y[n+]));
} dis[n][n+]=dis[n+][n]=(fabs(x[n]-x[n+])+fabs(y[n]-y[n+]));
for(int i=;i<A;i++){
scanf("%d%d",&a,&b);
a--;b--;
hatx[i]=a;haty[i]=b;
}
for(int i=;i<B;i++){
scanf("%d%d",&a,&b);
a--;b--;
likx[i]=a;liky[i]=b;
}
int L=,R=,M,ans=-;
while(L<=R){
M=(L+R)/;
solver.init(n);
for(int i=;i<A;i++){
solver.add_clause(hatx[i],,haty[i],);
solver.add_clause(hatx[i],,haty[i],);
solver.add_clause(haty[i],,hatx[i],);
solver.add_clause(haty[i],,hatx[i],);
}
for(int i=;i<B;i++){
solver.add_clause(likx[i],,liky[i],);
solver.add_clause(likx[i],,liky[i],);
solver.add_clause(liky[i],,likx[i],);
solver.add_clause(liky[i],,likx[i],);
}
for(int i=;i<n;i++){
//if(dis[i][n]>M) solver.add_clause(i,0,i,1);
//if(dis[i][n+1]>M) solver.add_clause(i,1,i,0);
for(int j=i+;j<n;j++){
if(dis[i][n]<=M&&dis[j][n]<=M&&dis[i][n]+dis[j][n]>M){
solver.add_clause(i,,j,);
solver.add_clause(j,,i,);
}
if(dis[i][n+]<=M&&dis[j][n+]<=M&&dis[i][n+]+dis[j][n+]>M){
solver.add_clause(i,,j,);
solver.add_clause(j,,i,);
}
if(dis[i][n]<=M&&dis[j][n+]<=M&&dis[i][n]+dis[j][n+]+dis[n][n+]>M){
solver.add_clause(i,,j,);
solver.add_clause(j,,j,);
}
if(dis[i][n+]<=M&&dis[j][n]<=M&&dis[i][n+]+dis[j][n]+dis[n][n+]>M){
solver.add_clause(i,,j,);
solver.add_clause(j,,i,);
}
}
}
if(solver.solve()) {R=M-;ans=M;}
else L=M+;
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. javax.el.PropertyNotFoundException 出错
  2. 解决:j-link V8下载器灯不亮,无法正常烧写固件
  3. Java生成BASE64编码
  4. 利用Shell命令获取IP地址
  5. hdu1031 Design T-Shirt
  6. tomcat+nginx+redis实现均衡负载、session共享(一)
  7. 【codechef】FN/Fibonacci Number
  8. vim学习之git for windows
  9. GIT 身份验证失败问题解决方案,由于修改密码产生的问题
  10. mysql获取随机字符串和随机数的方法
  11. phpmyadmin无法访问503错误的解决方法
  12. 取代iframe框架
  13. js知识点: 数组
  14. 【Java】HashTable和HashMap区别
  15. 图片和span水平垂直居中
  16. final用户体验报告
  17. BZOJ 3165: [Heoi2013]Segment
  18. 手脱nSPack 2.2
  19. Logon database Tiger引起DataGuard不同步问题
  20. Ubuntu的SWAP设置

热门文章

  1. C语言链接数据库
  2. 左值&amp;右值
  3. LintCode-204.单例
  4. %pylab ipython 中文
  5. MyBatis事务管理机制
  6. 单点登录Windows实现
  7. 命名空间(namespace)// 友元函数
  8. 实用图像处理入门 - 1 - opencv VS2012 环境搭建
  9. 【bzoj2190】[SDOI2008]仪仗队 欧拉函数
  10. 【bzoj3036】绿豆蛙的归宿 期望dp