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

思路是没有问题的,但是写的有点丑,很多地方还可以合并。一开始思路就没有问题的,但是这种题就是找bug很头疼。QwQ!

结果是maxn开了510,忘记了两倍,晕死了。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
const int B=;
const int R=;
const int W=;
vector<int>G[maxn];
vector<int>G2[maxn];
int dis[][maxn],Dis;//Dis是S点,T点
int x,y,x1,y1,x2,y2,a,b,n,ans;
int col[maxn],q[maxn],num;
void init()
{
for(int i=;i<=*n;i++) G[i].clear();
for(int i=;i<=*n;i++) G2[i].clear();
ans=-;
}
void scan()
{
int i;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
Dis=abs(x1-x2)+abs(y1-y2);
for(i=;i<=n;i++){
scanf("%d%d",&x,&y);
dis[][i]=abs(x-x1)+abs(y-y1);
dis[][i]=abs(x-x2)+abs(y-y2);
}
for(i=;i<=a;i++){
scanf("%d%d",&x,&y);
G[x].push_back(y+n);
G[x+n].push_back(y);
G[y].push_back(x+n);
G[y+n].push_back(x);
}
for(i=;i<=b;i++){
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
G[x+n].push_back(y+n);
G[y+n].push_back(x+n);
} }
bool dfs(int u)
{
if(col[u]==R) return false;
if(col[u]==B) return true;
col[u]=B;
col[u>n?u-n:u+n]=R;
q[++num]=u;
for(int i=;i<G[u].size();i++) if(!dfs(G[u][i])) return false;
for(int i=;i<G2[u].size();i++) if(!dfs(G2[u][i])) return false;
return true;
}
bool check(int x)
{
int i,j;
for(i=;i<=n;i++) if(dis[][i]>x&&dis[][i]>x) return false;
for(i=;i<=*n;i++) G2[i].clear();
for(i=;i<=*n;i++) col[i]=;
for(i=;i<=n;i++)
for(j=i+;j<=n;j++){
int d1=dis[][i]+dis[][j];
int d2=dis[][i]+dis[][j]+Dis;
int d3=dis[][i]+dis[][j];
int d4=dis[][i]+dis[][j]+Dis;
if(d1>x&&d2>x&&d3>x&&d4>x) return false;
if(d1>x){
G2[i].push_back(j+n);
G2[j].push_back(i+n);
}
if(d2>x){
G2[i].push_back(j);
G2[j+n].push_back(i+n);
}
if(d3>x){
G2[i+n].push_back(j);
G2[j+n].push_back(i);
}
if(d4>x){
G2[i+n].push_back(j+n);
G2[j].push_back(i);
}
}
for(i=;i<=*n;i++){
if(col[i]) continue;
num=;
if(!dfs(i)){
for(j=;j<=num;j++) {
col[q[j]>n?q[j]-n:q[j]+n]=W;
col[q[j]]=W;
}
if(!dfs(i>n?i-n:i+n)) return false;
}
}
return true;
}
int main()
{
while(~scanf("%d%d%d",&n,&a,&b)){
init();
int L=,R=;
scan();
while(L<=R){
int mid=(L+R)>>;
if(check(mid)){ ans=mid;R=mid-;}
else L=mid+;
}
printf("%d\n",ans);
}
return ;
}

下面这样建图有些问题,读者可以思考一下。

         if(d2<=x&&d1>x&&d3>x){
G2[i].push_back(j+n);
G2[j+n].push_back(i);
}
if(d1<=x&&d2>x&&d4>x){
G2[i].push_back(j);
G2[j].push_back(i);
}
if(d4<=x&&d3>x&&d1>x){
G2[i+n].push_back(j);
G2[j].push_back(i+n);
}
if(d3<=x&&d4>x&&d2>x){
G2[i+n].push_back(j+n);
G2[j+n].push_back(i+n);
}

HDU1815 Building roads(二分+2-SAT)的更多相关文章

  1. POJ Building roads [二分答案 2SAT]

    睡觉啦 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...

  2. poj 2749 Building roads (二分+拆点+2-sat)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 De ...

  3. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  4. Building roads

    Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. [POJ2749]Building roads(2-SAT)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8153   Accepted: 2772 De ...

  6. poj 3625 Building Roads

    题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...

  7. BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

    计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...

  8. bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...

  9. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer J ...

随机推荐

  1. Python学习进程(12)模块

        模块让你能够有逻辑地组织你的Python代码段.     (1)python模块: 模块化的好处: 1.把相关的代码分配到一个模块里能让你的代码更好用,更易懂. 2.模块也是Python对象, ...

  2. .ssh中的文件的分别意义

    当我们在用户的主目录使用如下命令: cd (进入个人主目录,默认为/home/hadoop) ssh-keygen -t rsa -P '' (注:最后是二个单引号) 表示在用户的主目录创建ssh登陆 ...

  3. Git版本控制系统VCS

    Git版本控制系统VCS 一.版本控制系统基本情况说明 版本控制是一种记录一个或者若干个文件内容的变化,以便将来查阅特定版本修订情况的系统 1.作用 记录文件的所有历史变化 随时可回复到任何一个历史状 ...

  4. ag-grid

    使用: import { AgGridVue } from "ag-grid-vue"; <ag-grid-vue style="width:100%;height ...

  5. php数组函数-array_rand()

    array_rand()函数返回数组中的一个随机键名,或者如果指定函数返回键名不止一个,则返 回一个包含随机键名的数组. array_rand(array,number); array:必需.规定数组 ...

  6. 淘宝分类常见---部分显示和全部显示的js效果

    需求就是,点击“更多按钮”,显示全部的分类详情,再次点击,显示部分分类. 展开: 收起: 结构: <div class="SubBox" id="SubBox&qu ...

  7. 【P1379】八数码难题(搜索+暴力)

    这个题真是... 不想说什么了,及其复杂和烦人的一道题.基础思路就是bfs,用两个队列分别进行0的位置的计算和每一步的状态..然而这个题最重要的一点在于判重,实际上可以康托展开用全排列的个数进行判重, ...

  8. Flume具体应用(多案例)

    日志采集 对于flume的原理其实很容易理解,我们更应该掌握flume的具体使用方法,flume提供了大量内置的Source.Channel和Sink类型.而且不同类型的Source.Channel和 ...

  9. Linux上SFTP用法

    SFTP简介 sftp是一个交互式的文件传输协议,类似于ftp,但它进行加密传输,比ftp更安全. SFTP用法 localhost 从远程主机获取文件或目录到本地目录下 sftp>get /h ...

  10. spring boot: 条件注解@Condition

    @Conditional根据满足某一个特定的条件创建一个特定的Bean(基于条件的Bean的创建,即使用@Conditional注解). 比方说,当一个jar包在一个类的路径下的时候,自动配置一个或多 ...