POJ 2749 Building roads(二分+2-SAT)

Building roads

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8539   Accepted: 2908

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

题意:二维平面,给你n(n<=500)个点的坐标,再给你两个原点s1,s2的坐标,每个点连且只能连s1或s2中的一个。距离均为曼哈顿距离。然后是A(A<=1000)条信息,每条信息给出两个点的编号,表示这两个点不能连同一个原点。然后是B(B<=1000)条信息,每条信息给出两个点的编号,表示这两个点必须连同一个原点。每个点只能先到原点,再到另一个原点,再到其他点。

问你任意两个点的最大距离的最小值是多少。如果不存在答案输出-1。

思路:与HDU 3622 Bomb Game非常类似。可以说是非常经典的二分+2-SAT题目了。

预处理出每个点连s1、s2到其他点的距离。然后二分答案,对矛盾的连法进行相应的建边,每次都要对A、B条信息重新建边。

注意二分的上下界,搞不好会T。

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=2010;
int op[maxn],vis[maxn],low[maxn],dfn[maxn];
int pt[maxn],stk[maxn],color[maxn],pos[maxn],deg[maxn];
vector<int>vc[maxn],vc2[maxn];
int n,m,sig,cnt,tot,cont;
int sx1,sx2,sy1,sy2;
int A,B,mi,ma;
int dxy[maxn][2],dis[505][505][4];
struct node
{
    int x,y;
}e1[maxn*4],e2[maxn*4];
void add(int x,int y){
    vc[x].push_back(y);
}
void top(){
    memset(pt,0,sizeof(pt));
    queue<int>s;
    for(int i=1;i<=sig;i++){
        if(deg[i]==0) s.push(i);
    }
    while(!s.empty()){
        int u=s.front();
        if(pt[u]==0){
            pt[u]=1;
            pt[pos[u]]=2;
        }
        s.pop();
        for(int i=0;i<vc2[u].size();i++){
            int v=vc2[u][i];
            deg[v]--;
            if(deg[v]==0) s.push(v);
        }
    }
    cont=0;
    for(int i=1;i<=n;i++) {
        if(pt[color[i]]==1) op[cont++]=i;
    }
}
void tarjan(int u){
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stk[++tot]=u;
    for(int i=0;i<vc[u].size();i++){
        int v=vc[u][i];
        if(vis[v]==0) tarjan(v);
        if(vis[v]==1) low[u]=min(low[u],low[v]);
    }
    if(low[u]==dfn[u]) {
        sig++;
        do{
            vis[stk[tot]]=-1;
            color[stk[tot]]=sig;
        }while(stk[tot--]!=u);
    }
}
void init(){
    sig=0;cnt=1;tot=-1;
    memset(deg,0,sizeof(deg));
    memset(stk,0,sizeof(stk));
    memset(dfn,0,sizeof(dfn));
    memset(vis,0,sizeof(vis));
    memset(color,0,sizeof(color));
    for(int i=0;i<maxn;i++) {
        vc[i].clear();
        vc2[i].clear();
    }
}
int solve(){
    for(int i=1;i<=n*2;i++) {
        if(vis[i]==0) tarjan(i);
    }
    for(int i=1;i<=n;i++) {
        if(color[i]==color[i+n]) return 0;
        pos[color[i]]=color[i+n];
        pos[color[i+n]]=color[i];
    }
    for(int i=1;i<=n*2;i++){
        for(int j=0;j<vc[i].size();j++){
            int v=vc[i][j];
            if(color[i]!=color[v]){
                deg[color[i]]++;
                vc2[color[v]].push_back(color[i]);
            }
        }
    }
    top();
    return 1;
}
bool jud(int mid)
{
    init();
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(mid<dis[i][j][0])
            {
                add(i,j+n);
                add(j,i+n);
            }
            if(mid<dis[i][j][1])
            {
                add(i,j);
                add(j+n,i+n);
            }
            if(mid<dis[i][j][2])
            {
                add(i+n,j+n);
                add(j,i);
            }
            if(mid<dis[i][j][3])
            {
                add(i+n,j);
                add(j+n,i);
            }
        }
    }
    for(int i=0;i<A;i++)
    {
        add(e1[i].x,e1[i].y+n);
        add(e1[i].y,e1[i].x+n);
        add(e1[i].y+n,e1[i].x);
        add(e1[i].x+n,e1[i].y);
    }
    for(int i=0;i<B;i++)
    {
        add(e2[i].x,e2[i].y);
        add(e2[i].y,e2[i].x);
        add(e2[i].y+n,e2[i].x+n);
        add(e2[i].x+n,e2[i].y+n);
    }
    int ans=solve();
    if(ans==1) return 1;
    return 0;
}
int cal(int x,int y,int xx,int yy)
{
    return abs(x-xx)+abs(y-yy);
}
int main(){
    int T,cas=1;
    while(scanf("%d%d%d",&n,&A,&B)!=EOF){
        mi=inf;
        ma=-inf;
        scanf("%d%d%d%d",&sx1,&sy1,&sx2,&sy2);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&dxy[i][0],&dxy[i][1]);
        }
        for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
        {
            dis[i][j][0]=cal(dxy[i][0],dxy[i][1],sx1,sy1)+cal(sx1,sy1,dxy[j][0],dxy[j][1]);
            dis[i][j][3]=cal(dxy[i][0],dxy[i][1],sx2,sy2)+cal(sx2,sy2,dxy[j][0],dxy[j][1]);
            dis[i][j][1]=cal(dxy[i][0],dxy[i][1],sx1,sy1)+cal(sx2,sy2,dxy[j][0],dxy[j][1])+cal(sx1,sy1,sx2,sy2);
            dis[i][j][2]=cal(dxy[i][0],dxy[i][1],sx2,sy2)+cal(sx1,sy1,dxy[j][0],dxy[j][1])+cal(sx1,sy1,sx2,sy2);
            for(int k=0;k<4;k++)
            {
                mi=min(mi,dis[i][j][k]);
                ma=max(ma,dis[i][j][k]);
            }
        }
        for(int i=0;i<A;i++)
        {
            scanf("%d%d",&e1[i].x,&e1[i].y);
        }
        for(int i=0;i<B;i++)
        {
            scanf("%d%d",&e2[i].x,&e2[i].y);
        }
        int l=mi,r=ma+1;
        int ans=0;
        while(l<r)
        {
            int mid=(l+r)/2;
            if(jud(mid)) r=mid;
            else l=mid+1;
        }
        if(!jud(r)) {puts("-1");continue;}
        printf("%d\n",r);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LSD20164388/article/details/88072417