现在的位置: 首页 > 综合 > 正文

POJ2749 Building roads 2-SAT+二分

2013年11月04日 ⁄ 综合 ⁄ 共 5144字 ⁄ 字号 评论关闭

二分枚举最长距离。用2SAT判断可行与否。最后输出答案,如果没有,那么输出-1

条件1   i,j  相互讨厌,  <i,j+n>  <i+n,j>  <j,i+n>  <j+n,i>

条件2   i,j   关系好       <i,j> <j,i> <j+n,i+n> <i+n,j+n>

条件3

     1:dis(i,s1) + dis(j,s1)>m     <i,j+n>  <j,i+n>

     2:i j都连s2的时候与上面类似

     3:dis(i,s1)+dis(s1,s2)+dis(s2,j)>m   <i,j>  <j+n,i+n>

     4:i连s2 j连s1条件与上面类似

 

2SAT的六题做完几年一下!!

 

Building roads
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5509   Accepted: 1831

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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<cmath>

using namespace std;

#define MAXN 10000

struct node
{
	int to,next;
};

node edge[99000010],edge2[99000010];
int head[MAXN],en,n,a,b;


void add(int a,int b)
{
	edge[en].to=b;
	edge[en].next=head[a];
	head[a]=en++;
}

int low[MAXN],dfn[MAXN];
int stack[MAXN],top,set[MAXN],col,num;
bool vis[MAXN],instack[MAXN];

void tarjan(int u)
{
	vis[u]=1;
	dfn[u]=low[u]=++num;
	instack[u]=true;
	stack[++top]=u;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(!vis[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else
			if(instack[v])
				low[u]=min(dfn[v],low[u]);
	}
	if (dfn[u]==low[u])
	{
		int j;
		col++;
		do
		{
			j=stack[top--];
			instack[j]=false;
			set[j]=col;
		}
		while (j!=u);
	}
}

void solve()
{
	int i;
	top=col=num=0;
	memset(instack,0,sizeof(instack));
	memset(vis,0,sizeof(vis));
	memset(set,-1,sizeof(set));
	for (i=0;i<2*n;i++)
		if (!vis[i])
			tarjan(i);
}

bool twosat()
{
    solve();
    for(int i=0;i<n;i++)
        if(set[i]==set[i+n]) return false;
    return true;
}

struct point{int x,y;};
point s1,s2,tmp;
int dis[2600][2],A[2005][2],B[2005][2],ss;

int getdis(point p1,point p2)
{
    return abs(p1.x-p2.x)+abs(p1.y-p2.y);
}

void build(int x)
{
    //i s1 i+n s2
    memset(head,-1,sizeof(head)),en=0;
    for(int i=0;i<a;i++)
    {
        add(A[i][0]-1,A[i][1]-1+n);
        add(A[i][1]-1,A[i][0]-1+n);
        add(A[i][0]-1+n,A[i][1]-1);
        add(A[i][1]-1+n,A[i][0]-1);
    }
    for(int i=0;i<b;i++)
    {
        add(B[i][0]-1,B[i][1]-1);
        add(B[i][1]-1,B[i][0]-1);
        add(B[i][0]-1+n,B[i][1]-1+n);
        add(B[i][1]-1+n,B[i][0]-1+n);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            //if(i==j) continue;
            if(dis[i][0]+dis[j][0]>x)
                add(i,j+n),add(j,i+n);
            if(dis[i][1]+dis[j][1]>x)
                add(i+n,j),add(j+n,i);
            if(dis[i][0]+dis[j][1]+ss>x)
                add(i,j),add(j+n,i+n);
            if(dis[i][1]+dis[j][0]+ss>x)
                add(i+n,j+n),add(j,i);
        }
    }
}

int main()
{
    while(~scanf("%d%d%d",&n,&a,&b))
    {
        scanf("%d%d%d%d",&s1.x,&s1.y,&s2.x,&s2.y);
        ss=getdis(s1,s2);
        int l,r,m,ans=-1;
        l=0,r=4000000;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&tmp.x,&tmp.y);
            dis[i][0]=getdis(tmp,s1);
            dis[i][1]=getdis(tmp,s2);

        }
        for(int i=0;i<a;i++)
            scanf("%d%d",&A[i][0],&A[i][1]);
        for(int i=0;i<b;i++)
            scanf("%d%d",&B[i][0],&B[i][1]);
        while(l<=r)
        {
            m=(l+r)/2;
            build(m);
            if(twosat())
                r=m-1,ans=m;
            else
                l=m+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

抱歉!评论已关闭.