POJ2749 Building roads

POJ2749 Building roads
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4522   Accepted: 1485

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个牧场要连在一起,首先给定s1和s2的坐标。所有的牧场都连在s1上或者s2上,但是有些特殊的原因导致某些牧场不能同时连到同一
个s点上,而又有些牧场必须同时连到同一个s点上。还给出了所有牧场的坐标,求满足上述要求后,两个牧场的最大距离的最小值。
解体思路:2-sat+二分答案。
建图有点小麻烦,想了挺久:令i牧场如果连到s1这个点记为ki,连到s2这个点记为ki' 。
首先,不能同时连到一个点和必须同时连到一个点,这个建图还是很容易的。
然后,在二分答案的时候,必须枚举一下点对,如果ki和kj之间的距离大于你二分的那个答案,就需要加一条边从ki到kj'理所当然的也需要加一条边
从kj到ki',这样处理就能完美地二分答案了。
#include <stdio.h>

#include <string.h>

#include <vector>

#define N 2005

#define M 1000005

using namespace std;



int n,p,q,le,ri,mid;

int x1,x2,y1,y2,td;

int dis[N],gra[N][N];

int ht[N][2],ft[N][2];

int htop,ftop;

int eid;

int head[N],ed[M],nxt[M];

int id,now;

int dfn[N],low[N];

int ins[N],gid[N];

int stack[N],top;



int aabs(int a){return a>0?a:-a;}



void addedge(int s,int e)

{

    ed[eid]=e;

    nxt[eid]=head[s];

    head[s]=eid++;

}



void tarjan(int s)

{

    dfn[s]=low[s]=++now;

    ins[s]=1;

    stack[++top]=s;

    for(int i=head[s];~i;i=nxt[i])

    {

        int t=ed[i];

        if(!dfn[t])

        {

            tarjan(t);

            low[s]=min(low[s],low[t]);

        }

        else if(ins[t])

            low[s]=min(low[s],dfn[t]);

    }

    if(dfn[s]==low[s])

    {

        id++;

        while(top)

        {

            int t=stack[top--];

            ins[t]=0;

            gid[t]=id;

            if(t==s)break;

        }

    }

}



int ok(int s)

{

    eid=0;

    memset(head,-1,sizeof(head));

    for(int i=1;i<=htop;i++)

    {

        int a=ht[i][0],b=ht[i][1];

        addedge(a*2,b*2+1);

        addedge(a*2+1,b*2);

        addedge(b*2,a*2+1);

        addedge(b*2+1,a*2);

    }

    for(int i=1;i<=ftop;i++)

    {

        int a=ft[i][0],b=ft[i][1];

        addedge(a*2,b*2);

        addedge(a*2+1,b*2+1);

        addedge(b*2,a*2);

        addedge(b*2+1,a*2+1);

    }

    for(int i=0;i<2*n;i++)

        for(int j=0;j<2*n;j++)

            if(i/2!=j/2&&gra[i][j]>s)

                addedge(i,j^1);

    id=now=top=0;

    memset(dfn,0,sizeof(dfn));

    memset(ins,0,sizeof(ins));

    for(int i=0;i<2*n;i++)

        if(!dfn[i])

            tarjan(i);

    for(int i=0;i<2*n;i+=2)

        if(gid[i]==gid[i+1])

            return 0;

    return 1;

}



void re(void)

{

    le=0x3f3f3f3f;

    ri=0;

    scanf("%d%d%d",&n,&p,&q);

    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

    td=aabs(x1-x2)+aabs(y1-y2);

    for(int i=0;i<n;i++)

    {

        int a,b;

        scanf("%d%d",&a,&b);

        dis[i*2]=aabs(a-x1)+aabs(b-y1);

        dis[i*2+1]=aabs(a-x2)+aabs(b-y2);

    }

    memset(gra,0,sizeof(gra));

    for(int i=0;i<2*n;i++)

        for(int j=0;j<2*n;j++)

            if(i/2!=j/2)

            {

                if(!((i&1)^(j&1)))

                    gra[i][j]=dis[i]+dis[j];

                else

                    gra[i][j]=dis[i]+dis[j]+td;

                le=min(le,gra[i][j]);

                ri=max(ri,gra[i][j]);

            }

    ftop=htop=0;

    for(int i=0;i<p;i++)

    {

        int a,b;

        scanf("%d%d",&a,&b);

        a--;b--;

        ht[++htop][0]=a;

        ht[htop][1]=b;

    }

    for(int i=0;i<q;i++)

    {

        int a,b;

        scanf("%d%d",&a,&b);

        a--;b--;

        ft[++ftop][0]=a;

        ft[ftop][1]=b;

    }

}



void run(void)

{

    int k=ri+1;

    ri++;

    while(mid=(ri+le)/2,ri>le)

    {

        if(ok(mid))ri=mid;

        else le=mid+1;

    }

    if(ri!=k)

        printf("%d\n",ri);

    else

        printf("-1\n");

}



int main()

{

    //freopen("/home/fatedayt/in","r",stdin);

    re();

    run();

    return 0;

}

 
  

 


你可能感兴趣的:(Build)