HDU 1815 二分+2SAT

Building roads

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 445    Accepted Submission(s): 114


Problem 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

题意:不解释。

思路:二分答案,然后重新建图,tarjin缩点判断,是否合法。

    PS:忘记考虑答案输出-1的情况,debug好久,白白的wa好多次,好搓。。。

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
const int maxn=3000;
const int maxm=9000000;
struct point
{
        int x,y;
}p[maxn],hate[maxn],like[maxn];
int dis(point a,point b)
{
        return abs(a.x-b.x)+abs(a.y-b.y);
}
int head[maxn],low[maxn],dfn[maxn],tol,Stack[maxn],instack[maxn],belong[maxn],dist[maxn][maxn],indexx,scc,top,n,A,B;
struct node
{
        int to,next;
}edge[maxm];
void add(int u,int v)
{
        edge[tol].next=head[u];
        edge[tol].to=v;
        head[u]=tol++;
}
void tarjin(int u)
{
        low[u]=dfn[u]=++indexx;
        Stack[top++]=u;instack[u]=1;
        int i,v;
        for(i=head[u];i!=-1;i=edge[i].next)
        {
                v=edge[i].to;
                if(!dfn[v])
                {
                        tarjin(v);
                        if(low[u]>low[v])low[u]=low[v];
                }
                else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
        }
        if(low[u]==dfn[u])
        {
                scc++;
                do
                {
                        v=Stack[--top];
                        instack[v]=0;
                        belong[v]=scc;
                }while(u!=v);
        }
}

int solve(int x)
{
        memset(head,-1,sizeof(head));tol=0;
        int i,j;
       // cout<<A<<" "<<B<<endl;
        for(i=1;i<=A;i++)
        {
                add(2*hate[i].x,  2*hate[i].y+1);
                add(2*hate[i].x+1,2*hate[i].y);
                add(2*hate[i].y,  2*hate[i].x+1);
                add(2*hate[i].y+1,2*hate[i].x);
        }
        for(i=1;i<=B;i++)
        {
                add(like[i].x*2,  like[i].y*2);
                add(like[i].x*2+1,like[i].y*2+1);
                add(like[i].y*2,  like[i].x*2);
                add(like[i].y*2+1,like[i].x*2+1);
        }
        for(i=0;i<n;i++)
        {
                for(j=i+1;j<n;j++)
                {
                        if(dist[n][i]+dist[n][j]>x)
                        {
                                add(2*i,2*j+1);
                                add(2*j,2*i+1);
                        }
                        if(dist[n+1][i]+dist[n+1][j]>x)
                        {
                                add(2*i+1,2*j);
                                add(2*j+1,2*i);
                        }
                        if(dist[n][i]+dist[n+1][j]+dist[n][n+1]>x)
                        {
                                add(2*i,2*j);
                                add(2*j+1,2*i+1);
                        }
                        if(dist[n+1][i]+dist[n][j]+dist[n][n+1]>x)
                        {
                                add(2*i+1,2*j+1);
                                add(2*j,2*i);
                        }
                }
        }
        //cout<<tol<<endl;
        memset(instack,0,sizeof(instack));
        memset(dfn,0,sizeof(dfn));
        scc=indexx=top=0;
        memset(belong,0,sizeof(belong));
        for(i=0;i<2*n;i++)if(!dfn[i])tarjin(i);
        for(i=0;i<n;i++)if(belong[2*i]==belong[2*i+1])return 0;
        return 1;
}
int main()
{
        int i,j,k,m;
        point a,b;
        while(~scanf("%d%d%d",&n,&A,&B))
        {
                scanf("%d%d%d%d",&a.x,&a.y,&b.x,&b.y);
                dist[n][n+1]=dis(a,b);
                for(i=0;i<n;i++)
                {
                        scanf("%d%d",&p[i].x,&p[i].y);
                        dist[n][i]=dis(a,p[i]);
                        dist[n+1][i]=dis(b,p[i]);
                }
                for(i=1;i<=A;i++)scanf("%d%d",&hate[i].x,&hate[i].y),hate[i].x--,hate[i].y--;
                for(i=1;i<=B;i++)scanf("%d%d",&like[i].x,&like[i].y),like[i].x--,like[i].y--;
               // cout<<A<<" "<<B<<endl;
                int left=0,right=1e8,mid;
                while(left<right)
                {
                        mid=(left+right)>>1;
                        if(solve(mid))right=mid;
                        else left=mid+1;
                }
               // if(!solve(left))left++;
               
//if(solve(left-1))left--;
               if(left==1e8)left=-1;
                printf("%d\n",left);
                //cout<<solve(53246)<<endl;
        }
        return 0;
}


 

posted @ 2013-09-03 06:52  线性无关  阅读(120)  评论(0编辑  收藏  举报