Poj 2749 Building roads (Binary + split point + 2-sat)

Source: Internet
Author: User

Building roads
Time Limit:2000 MS   Memory Limit:65536 K
Total Submissions:6229   Accepted:2093

Description

Farmer John's farm has N barns, and there are some cows that live in each barn. the cows like to drop around und, 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 shoshould connect.

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 shoshould note that all the coordinates are in the range [-1000000,100 0000].

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 112750 28546 15361 320556706 388710754 816612668 1938015788 160593 42 3

Sample Output

53246

Source

POJ Monthly -- 2006.01.22, zhucheng


Question:

There are N columns, and now they are connected through a channel (s1, s2). There are some constraints between them, and some Columns cannot be connected to the same point, some columns must be connected to the same point. Now, I am wondering if they can be connected and all constraints are met. If possible, I will output the minimum distance between the two columns.


Ideas:

The longest distance of the binary enumeration. Use 2SAT to determine whether it is feasible or not. Finally, output the answer. If not, output-1.
Condition 1 I and j hate each other. <I, j + n> <I + n, j> <j, I + n> <j + n, I>
Condition 2 I and j have a good relationship <I, j> <j, I> <j + n, I + n> <I + n, j + n>
Condition 3
1: dis (I, s1) + dis (j, s1)> m <I, j + n> <j, I + n>
2: When I j is connected to s2, it is similar to the above.
3: dis (I, s1) + dis (s1, s2) + dis (s2, j)> m <I, j> <j + n, I + n>
4: The s1 connection condition of I-connected s2 j is similar to the preceding one.


Code:

#include <cstdio>#include <cstring>#define INF 0x3f3f3f3f#define maxn 1005#define MAXN 4000005using namespace std;int n,m1,m2,num,flag,ans,tot;int head[maxn],X[2005],Y[2005],dist1[maxn],dist2[maxn];int scc[maxn];int vis[maxn];int stack1[maxn];int stack2[maxn];struct edge{    int v,next;} g[MAXN];void init(){    memset(head,0,sizeof(head));    memset(vis,0,sizeof(vis));    memset(scc,0,sizeof(scc));    stack1[0] = stack2[0] = num = 0;    flag = 1;}void addedge(int u,int v){    num++;    g[num].v = v;    g[num].next = head[u];    head[u] = num;}int abs(int x){    if(x>=0) return x;    return -x;}int caldist(int x1,int y1,int x2,int y2){    return abs(x1-x2)+abs(y1-y2);}void dfs(int cur,int &sig,int &cnt){    if(!flag) return;    vis[cur] = ++sig;    stack1[++stack1[0]] = cur;    stack2[++stack2[0]] = cur;    for(int i = head[cur]; i; i = g[i].next)    {        if(!vis[g[i].v]) dfs(g[i].v,sig,cnt);        else        {            if(!scc[g[i].v])            {                while(vis[stack2[stack2[0]]] > vis[g[i].v])                    stack2[0] --;            }        }    }    if(stack2[stack2[0]] == cur)    {        stack2[0] --;        ++cnt;        do        {            scc[stack1[stack1[0]]] = cnt;            int tmp = stack1[stack1[0]];            if((tmp >= n && scc[tmp - n] == cnt) || (tmp < n && scc[tmp + n] == cnt))            {                flag = false;                return;            }        }        while(stack1[stack1[0] --] != cur);    }}void Twosat(){    int i,sig,cnt;    sig = cnt = 0;    for(i=0; i<n+n&&flag; i++)    {        if(!vis[i]) dfs(i,sig,cnt);    }}void solve(){    int i,j,u,v,t,le=0,ri=4000000,mid;    ans=-1;    while(le<=ri)    {        mid=(le+ri)>>1;        init();        num=0;        for(i=1;i<=m1;i++)        {            u=X[i],v=Y[i];            addedge(u,v+n);            addedge(u+n,v);            addedge(v,u+n);            addedge(v+n,u);        }        for(i=m1+1;i<=m1+m2;i++)        {            u=X[i],v=Y[i];            addedge(u,v);            addedge(v,u);            addedge(u+n,v+n);            addedge(v+n,u+n);        }        for(i=0;i<n;i++)        {            for(j=0;j<n;j++)            {                if(i==j) continue ;                if(dist1[i]+dist1[j]>mid) addedge(i,j+n);                if(dist2[i]+dist2[j]>mid) addedge(i+n,j);                if(dist1[i]+dist2[j]+tot>mid) addedge(i,j);                if(dist2[i]+dist1[j]+tot>mid) addedge(i+n,j+n);            }        }        Twosat();        if(flag)        {            ans=mid;            ri=mid-1;        }        else le=mid+1;    }}int main(){    int i,j,t,x,y,x1,y1,x2,y2;    while(~scanf("%d%d%d",&n,&m1,&m2))    {        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);        tot=caldist(x1,y1,x2,y2);        for(i=0; i<n; i++)        {            scanf("%d%d",&x,&y);            dist1[i]=caldist(x,y,x1,y1);            dist2[i]=caldist(x,y,x2,y2);        }        for(i=1;i<=m1+m2;i++)        {            scanf("%d%d",&X[i],&Y[i]);            X[i]--; Y[i]--;        }        solve();        printf("%d\n",ans);    }    return 0;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.