Poj 2749 Building roads【二分+2-Sat----------Tarjan强连通】

来源:互联网 发布:吉诺比利职业生涯数据 编辑:程序博客网 时间:2024/05/21 17:08

Building roads

Time Limit: 2000MS

 

Memory Limit: 65536K

Total Submissions: 7148

 

Accepted: 2434

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

Source

POJ Monthly--2006.01.22,zhucheng

 

题目大意:

有n个农场,有两个中转站s1,2,每个农场都必须连接一个中转站,而且只能连接一个中转站,给出所有点的坐标,对应距离为曼哈顿距离(|xi-xj|+|yi-yj|),求如何建图能够使得其中所有点间距最大值最小。其中s1和s2之间有一条已有的路。

其中给出a个限制条件,表示这a个限制条件中的两个农场,不能连接同一个中转站

其中给出b个限制条件,表示这b个限制条件中的两个农场,必须连接同一个中转站


思路:


1、对应每个农场,要么连接s1,要么连接s2,典型的2-sat标志,那么对应建点:0-n表示点选择连接s1,n+1-2n表示点选择连接s2。


2、对应已有限制条件:

①两个农场不能连接同一个中转站,那么其矛盾边就是两个农场连接了同一个中转站,对应两点(a,b)有矛盾边:

(a,b)那么根据2-sat其建立有向边:(a,!b),(b,!a)

(!a,!b)那么根据2-sat其建立有向边(!a,b),(!b,a)

②两个农场必须连接同一个中转站,那么其矛盾边就是连个农场不连接同一个中转站,对应两点(a,b)有矛盾边:

(a,!b)那么根据2-sat其建立有向边:(a,b),(!b,!a)

(!a,b)那么根据2-sat其建立有向边:(!a,!b),(b,a)


3、然后我们想到暴力枚举这个最大距离,使其最小即可。根据简单分析,其枚举区间比较大,直接暴力搞是一定会超时的,那么二分优化。


4、对于二分出来的当前mid值,对应还有约束条件,找到矛盾边,然后继续建图:

①对应枚举两个点,如果其距离大于了mid,那么这条边就是矛盾边。

②对应枚举两个点,有四种情况:


点i选择s1,点j选择s1---------------if(dis(i,j,sx1,sy1)+dis(i,j,sx1,sy1)>mid)add(i,!j),add(j,!i)

点i选择s1,点j选择s2---------------if(dis(i,j,sx1,sy1)+dis(i,j,sx2,sy2)+dis(sx1,sy1,sx2,sy2)>mid)add(i,j),add(!i,!j)

段i选择s2,点j选择s2----------------if(dis(i,j,sx2,sy2)+dis(i,j,sx2,sy2)>mid)add(!i,j),add(!j,i)

点i选择s2,点j选择s1---------------if(dis(i,j,sx2,sy2)+dis(i,j,sx1,sy1)+dis(sx1,sy1,sx2,sy2)>mid)add(!i,!j),add(j,i)


5、对应建立出来的图,进行2-sat----------tarjan强连通的判断,如果可行,记录当前mid值作为ans,继续二分,直到不能二分为止,输出记录下来的ans值。


Ac代码:


#include<stdio.h>#include<string.h>#include<iostream>using namespace std;struct Edge{    int from,to,next;}e[5000000]; int x1,y1,x2,y2;int stack[50000];int color[50000];int dfn[50000];int low[50000];int vis[50000];int head[5500];int uu[105500];int vv[105500];int uu2[10550];int vv2[10550];int x[50000];int y[50000];int cont,n,sig,cnt,tt,aa,bb;void add(int from,int to){    e[cont].to=to;    e[cont].next=head[from];    head[from]=cont++;}int abs(int a){    if(a<0)return -a;    else return a;}int dis(int x1,int y1,int x2,int y2){    return abs(x1-x2)+abs(y1-y2);}void getmap(int mid){    cont=0;    memset(head,-1,sizeof(head));    for(int i=1;i<=aa;i++)    {        int u,v;        u=uu[i];        v=vv[i];        add(u,v+n);        add(v,u+n);        add(u+n,v);        add(v+n,u);    }    for(int j=1;j<=bb;j++)    {        int u,v;        u=uu2[j];        v=vv2[j];        add(u,v);        add(v+n,u+n);        add(v,u);        add(u+n,v+n);    }    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            int u=i;int v=j;            if(i==j)continue;            if(dis(x1,y1,x[i],y[i])+dis(x1,y1,x[j],y[j])>mid)            {                add(u,v+n);                add(v,u+n);            }            if(dis(x1,y1,x[i],y[i])+dis(x2,y2,x[j],y[j])+dis(x1,y1,x2,y2)>mid)            {                add(u,v);                add(v+n,u+n);            }            if(dis(x2,y2,x[i],y[i])+dis(x2,y2,x[j],y[j])>mid)            {                add(u+n,v);                add(v+n,u);            }            if(dis(x2,y2,x[i],y[i])+dis(x1,y1,x[j],y[j])+dis(x1,y1,x2,y2)>mid)            {                add(u+n,v+n);                add(v,u);            }        }    }}void Tarjan(int u){    vis[u]=1;    dfn[u]=low[u]=cnt++;    stack[++tt]=u;    for(int i=head[u];i!=-1;i=e[i].next)    {        int v=e[i].to;        if(vis[v]==0)Tarjan(v);        if(vis[v]==1)low[u]=min(low[u],low[v]);    }    if(dfn[u]==low[u])    {        sig++;        do        {            color[stack[tt]]=sig;            vis[stack[tt]]=-1;        }        while(stack[tt--]!=u);    }}int Slove(int mid){    getmap(mid);    cnt=1,sig=0,tt=-1;    memset(stack,0,sizeof(stack));    memset(color,0,sizeof(color));    memset(dfn,0,sizeof(dfn));    memset(low,0,sizeof(low));    memset(vis,0,sizeof(vis));    for(int i=1;i<=2*n;i++)    {        if(vis[i]==0)Tarjan(i);    }    for(int i=1;i<=n;i++)    {        if(color[i]==color[i+n])return 0;    }    return 1;}int main(){    while(~scanf("%d%d%d",&n,&aa,&bb))    {        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);        for(int i=1;i<=n;i++)        {            scanf("%d%d",&x[i],&y[i]);        }        for(int i=1;i<=aa;i++)        {            scanf("%d%d",&uu[i],&vv[i]);        }        for(int i=1;i<=bb;i++)        {            scanf("%d%d",&uu2[i],&vv2[i]);        }        int mid;        int l=0;        int r=20000000;        int ans=-1;        while(r-l>=0)        {            mid=(l+r)/2;            if(Slove(mid)==1)            {                r=mid-1;                ans=mid;            }            else            {                l=mid+1;            }        }        printf("%d\n",ans);    }}



0 0
原创粉丝点击