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;
}
上一篇:bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads


下一篇:bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树