题目

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|.

输入格式

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].

输出格式

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.

输入样例

4 1 1

12750 28546 15361 32055

6706 3887

10754 8166

12668 19380

15788 16059

3 4

2 3

输出样例

53246

题解

题目大意:

有n个点,分别向两个点S1,S2其中一个连边,S1和S2之间有连边,且存在一些点必须连到同一个点上或必须不连到同一个点上,两点间距离用曼哈顿距离计算,求两点间距离最大值最小是多少?

题解

最大值最小,二分答案

对于二分出的最大值md,\(O(n^2)\)枚举所有点对,尝试两点间的四种连接关系【即谁连S1,谁连S2,或都连其中一个】,判断四种关系中两点间距离是否满足条件,不满足则在2-sat图上加边增加限制

当然,如果一个点连到Sx本身就超过了md,则也要增加限制

原来的限制也要建上

复杂度\(O((n^2 + A + B)log(md))\)

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
#define cls(x) memset(x,0,sizeof(x))
using namespace std;
const int maxn = 2105,maxm = 1000005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
return out * flag;
}
int h[maxn],ne;
struct EDGE{int to,nxt;}ed[maxm];
void build(int u,int v){ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;}
int dfn[maxn],low[maxn],Scc[maxn],st[maxn],scci,top,cnt;
void dfs(int u){
dfn[u] = low[u] = ++cnt;
st[++top] = u;
Redge(u){
if (!dfn[to = ed[k].to]){
dfs(to);
low[u] = min(low[u],low[to]);
}else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
}
if (dfn[u] == low[u]){
scci++;
do{Scc[st[top]] = scci;}while (st[top--] != u);
}
}
int n,m,q,X[maxn],Y[maxn],D,N,A[maxn],B[maxn],d1[maxn],d2[maxn];
int dis(int u,int v){return abs(X[u] - X[v]) + abs(Y[u] - Y[v]);}
void init(){
cls(dfn); cls(Scc); cls(h); ne = 1; scci = cnt = top = 0;
}
bool check(int md){
init();
REP(i,m){
build(A[i],B[i] + n),build(A[i] + n,B[i]);
build(B[i],A[i] + n),build(B[i] + n,A[i]);
}
for (int i = m + 1; i <= m + q; i++){
build(A[i],B[i]),build(A[i] + n,B[i] + n);
build(B[i],A[i]),build(B[i] + n,A[i] + n);
}
for (int i = 1; i <= n; i++){
if (d1[i] > md) build(i,i + n);
if (d2[i] > md) build(i + n,i);
}
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++){
if (d1[i] + d1[j] > md) build(i,j + n),build(j,i + n);
if (d2[i] + d2[j] > md) build(i + n,j),build(j + n,i);
if (d1[i] + d2[j] + D > md) build(i,j),build(j + n,i + n);
if (d2[i] + d1[j] + D > md) build(i + n,j + n),build(j,i);
}
for (int i = 1; i <= N; i++) if (!dfn[i]) dfs(i);
for (int i = 1; i <= n; i++) if (Scc[i] == Scc[i + n]) return false;
return true;
}
int main(){
n = read(); m = read(); q = read(); N = n << 1;
X[N + 1] = read(); Y[N + 1] = read(); X[N + 2] = read(); Y[N + 2] = read();
D = dis(N + 1,N + 2);
REP(i,n) X[i] = read(),Y[i] = read(),d1[i] = dis(i,N + 1),d2[i] = dis(i,N + 2);
REP(i,m) A[i] = read(),B[i] = read();
REP(i,q) A[m + i] = read(),B[m + i] = read();
int L = 0,R = 10000000,mid;
while (L < R){
mid = L + R >> 1;
if (check(mid)) R = mid;
else L = mid + 1;
}
if (!check(L)) puts("-1");
else printf("%d\n",L);
return 0;
}

POJ2749 Building roads 【2-sat】的更多相关文章

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

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

  2. POJ1251 Jungle Roads 【最小生成树Prim】

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19536   Accepted: 8970 Des ...

  3. POJ2749 Building roads

    嘟嘟嘟 最近把21天漏的给不上. 今天重温了一下2-SAT,感觉很简单.就是把所有条件都转化成如果--必然能导出--.然后就这样连边建图,这样一个强连通分量中的所有点必然都是真或者假.从而根据这个点拆 ...

  4. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

  5. codeforces 544 D Destroying Roads 【最短路】

    题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs ...

  6. 【NOIP模拟】roads(最短路径转最小生成树)

    题目背景 SOURCE:NOIP2016-RZZ-1 题目描述 有 N 个城市,这些城市通过 M 条无向边互相连通,每条边有一个权值 Ci ,表示这条边的长度为 2^(Ci) ,没有两条边的长度是相同 ...

  7. 【POJ 1947】 Rebuilding Roads

    [题目链接] 点击打开链接 [算法] f[i][j]表示以i为根的子树中,最少删多少条边可以组成j个节点的子树 树上背包,即可 [代码] #include <algorithm> #inc ...

  8. 【codeforces 746G】New Roads

    [题目链接]:http://codeforces.com/problemset/problem/746/G [题意] 给你3个数字n,t,k; 分别表示一棵树有n个点; 这棵树的深度t,以及叶子节点的 ...

  9. 洛谷 P2872 [USACO07DEC]道路建设Building Roads 题解

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

随机推荐

  1. Shell重启Tomcat脚本

    #!/bin/bash echo -e "\n\n\n" #force kill flag,if equal [f] to force kill all flag="He ...

  2. PAT (Basic Level) Practise (中文)- 1002. 写出这个数 (20)

    http://www.patest.cn/contests/pat-b-practise/1002 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个 ...

  3. Andrew NG 自动化所演讲(20140707):DeepLearning Overview and Trends

    出处 以下内容转载于 网友 Fiona Duan,感谢作者分享 (原作的图片显示有问题,所以我从别处找了一些附上,小伙伴们可以看看).最近越来越觉得人工智能,深度学习是一个很好的发展方向,应该也是未来 ...

  4. 高阶函数 -------JavaScript

    高阶函数 本文摘要:http://www.liaoxuefeng.com/ JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作 ...

  5. 国产中标麒麟Linux部署dotnet core 环境并运行项目 (二) 部署运行控制台项目

    背景 在上一篇文章安装dotnet core,已经安装好dotnet core了.之前只是安装成功了dotnet, 输入dotnet --info,可以确认安装成功了,但是在运行代码时,还是报错了,本 ...

  6. Oracle 数据处理

    1. 对维度按照度量值的排名进行统计得分,第一名100分,第二名99分,第三名98……可以先进行排名,然后用 得分值+1,减去排名既是所得分数. -- 建表 create table province ...

  7. 基础篇(2):c++顺序结构程序设计

    一个程序最基本的结构莫过于3种:顺序,选择,循环.这篇讲讲顺序结构. c++语言的运算符与表达式数量之多,在高级语言中是少见的,也使得它的语言功能十分完善. c++的运算符有单目与双目之分(作用于一个 ...

  8. mysql 查询出 n小时 以前的数据

    select * FROM biaoming WHERE TIMESTAMPDIFF(SECOND ,CREATE_TIME,now() ) > 3*60*60

  9. 二十三、MySQL 事务

    MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数 ...

  10. 十七、MySQL UNION 操作符

    MySQL UNION 操作符 本教程为大家介绍 MySQL UNION 操作符的语法和实例. 描述 MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中.多 ...