POJ2749 Building roads 【2-sat】
题目
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】的更多相关文章
- [POJ2749]Building roads(2-SAT)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8153 Accepted: 2772 De ...
- POJ1251 Jungle Roads 【最小生成树Prim】
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19536 Accepted: 8970 Des ...
- POJ2749 Building roads
嘟嘟嘟 最近把21天漏的给不上. 今天重温了一下2-SAT,感觉很简单.就是把所有条件都转化成如果--必然能导出--.然后就这样连边建图,这样一个强连通分量中的所有点必然都是真或者假.从而根据这个点拆 ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- codeforces 544 D Destroying Roads 【最短路】
题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs ...
- 【NOIP模拟】roads(最短路径转最小生成树)
题目背景 SOURCE:NOIP2016-RZZ-1 题目描述 有 N 个城市,这些城市通过 M 条无向边互相连通,每条边有一个权值 Ci ,表示这条边的长度为 2^(Ci) ,没有两条边的长度是相同 ...
- 【POJ 1947】 Rebuilding Roads
[题目链接] 点击打开链接 [算法] f[i][j]表示以i为根的子树中,最少删多少条边可以组成j个节点的子树 树上背包,即可 [代码] #include <algorithm> #inc ...
- 【codeforces 746G】New Roads
[题目链接]:http://codeforces.com/problemset/problem/746/G [题意] 给你3个数字n,t,k; 分别表示一棵树有n个点; 这棵树的深度t,以及叶子节点的 ...
- 洛谷 P2872 [USACO07DEC]道路建设Building Roads 题解
P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...
随机推荐
- python基础一 day17 作业
# 3.用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sbname=['alex','wupeiqi','yuanhao','nezha']# def func(item):# r ...
- CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第一节
原文链接 第一节 CUDA 让你可以一边使用熟悉的编程概念,一边开发可在GPU上运行的软件. Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Lab ...
- 导航条(Navbar)
1.添加.navbar-fixed-top类可以让导航条固定的页面的顶部,固定的导航条会遮住页面上其它的内容,除非给body元素设置padding,导航条默认高度为50px ,因此可以给body元素设 ...
- C# sizeof运算符
一.C# sizeof运算符 sizeof运算符用于获取值类型的字节数. 二.示例 using System;using System.Collections.Generic;using System ...
- JAVA 优先获取外网Ip,再获取内网Ip
1.获取内网Ip private String getLocalhostIp(){ String hostAddress = ""; try { InetAddress addre ...
- awk截取指定字段
#!/bin/bash #好多地方可以优化,先记录下,便于以后使用 dir="/logs/$1"file="/logs/$1/requests.log"if [ ...
- Android中级教程之Android应用程序的生命周期
Android应用程序的生命周期图 在大部分情况下,每个Android应用都将运行在自己的Linux进程中.当这个应用的某些代码需要执行时,进程就会被创建,并且将保持运行,直到该进程不再需要,而系统需 ...
- python内置函数-排列组合函数
product 笛卡尔积 (有放回抽样排列) permutations 排列 (不放回抽样排列) combinations 组合,没有重复 (不放回抽样组合) combinations_with_re ...
- Oracle redo与undo 第二弹
首先看一下undo与redo的字面意思: undo:撤销,也就是取消之前的操作. redo:重做,重新执行一遍之前的操作. 什么是REDO REDO记录transaction logs,分为o ...
- g++编译器的使用(转载)
关于g++ g++ 是GNU组织开发出的编译器软件集合(GCC)下的一个C++编译器.它是Unix 和 Linux 系统下标配的 基于命令行的 C++编译器.如果你的系统是Windows,可以按照 ...