poj2749 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|. 

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

问你满足条件的两个牛棚之间最大距离 ,注意牛棚必须先连接两个连接点中的一个,这两个连接点分别设为s1,s2,可以用2-sat,与s1连设为1,与s2连设为0,先把在同一连接点的边加上,把不在同一连接点的边加上,再二分距离,枚举所有点,如果i到s1的距离加上j到s1的距离大于mid,说明i,j都连s1这种情况矛盾,如果i到s1距离加上j到s2距离加上s1到s2距离大于mid,说明这种情况矛盾,还有两种情况类似分析

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define maxn 10005
#define inf 0x3f3f3f3f
using namespace std;
int n,m,k;
int t;

int a[maxn][2];
int b[maxn][2];
int dis1[maxn];
int dis2[maxn];
int dis;
int l,r;
struct twosat
{
    int n;
    vector<int>g[maxn];
    bool mark[maxn*2];
    int s[maxn*2];
    int c;
    void init(int n)
    {
        this->n=n;
        for(int i=0;i<2*n;i++)
            g[i].clear();
        memset(mark,0,sizeof(mark));
    }
    void addedge(int x,int xval,int y,int yval)
    {
        x=x*2+xval;
        y=y*2+yval;
        g[x].push_back(y);

    }
    bool dfs(int x)
    {
        if(mark[x^1])
            return false;
        if(mark[x])
            return true;
        mark[x]=true;
        s[c++]=x;
        for(int i=0;i<g[x].size();i++)
            if(!dfs(g[x][i]))
            return false;
        return true;
    }
    bool solve()
    {
        for(int i=0;i<2*n;i+=2)
            if(!mark[i]&&!mark[i+1])
        {
            c=0;
        if(!dfs(i))
           {
            while(c>0)
           mark[s[--c]]=false;
           if(!dfs(i+1))
            return false;
        }
        }
        return true;
    }
}TS;
int len(int x1,int y1,int x2,int y2)
{
    return abs(x1-x2)+abs(y1-y2);
    }
bool check(int mid)
{TS.init(n);
   for(int i=0;i<m;i++)
   {
       TS.addedge(a[i][0],0,a[i][1],1);
       TS.addedge(a[i][0],1,a[i][1],0);
       TS.addedge(a[i][1],0,a[i][0],1);
       TS.addedge(a[i][1],1,a[i][0],0);
   }
   for(int i=0;i<k;i++)
   {
       TS.addedge(b[i][0],0,b[i][1],0);
       TS.addedge(b[i][0],1,b[i][1],1);
       TS.addedge(b[i][1],0,b[i][0],0);
       TS.addedge(b[i][1],1,b[i][0],1);
   }
   for(int i=0;i<n;i++)
    for(int j=i+1;j<n;j++)
   {
       if(dis1[i]+dis1[j]>mid)
       {TS.addedge(i,0,j,1);
       TS.addedge(j,0,i,1);
   }
   if(dis2[i]+dis2[j]>mid)
   {
       TS.addedge(i,1,j,0);
       TS.addedge(j,1,i,0);
   }
    if(dis1[i]+dis2[j]+dis>mid)
    {
        TS.addedge(i,0,j,0);
        TS.addedge(j,1,i,1);
    }
    if(dis2[i]+dis1[j]+dis>mid)
    {
        TS.addedge(i,1,j,1);
        TS.addedge(j,0,i,0);
    }
   }
    return TS.solve();

}
int main()
{      while(~scanf("%d%d%d",&n,&m,&k))
   {
       l=inf;
    r=-inf;
    memset(dis1,0,sizeof(dis1));
    memset(dis2,0,sizeof(dis2));

        int sx1,sy1,sx2,sy2;
     scanf("%d%d%d%d",&sx1,&sy1,&sx2,&sy2);
     dis=len(sx1,sy1,sx2,sy2);
     for(int i=0;i<n;i++)
     {int x,y;
         scanf("%d%d",&x,&y);
        dis1[i]=len(x,y,sx1,sy1);
        dis2[i]=len(x,y,sx2,sy2);
        l=min(l,min(dis1[i]*2,dis2[i]*2));
        r=max(r,max(dis1[i]*2+dis,dis2[i]*2+dis));
     }
     for(int i=0;i<m;i++)
     {
         scanf("%d%d",&a[i][0],&a[i][1]);
         a[i][0]--;
         a[i][1]--;
     }
     for(int i=0;i<k;i++)
     {
         scanf("%d%d",&b[i][0],&b[i][1]);
         b[i][0]--;
         b[i][1]--;
     }
     if(!check(r))
     {
         printf("-1\n");
     }
     else
       {while(l<r)
       {
           int mid=l+(r-l)/2;
           if(check(mid))
            r=mid;
           else
            l=mid+1;
       }
       printf("%d\n",r);
    }
   }

return 0;

}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/88087654