POJ2749 Building roads【2-SAT】

题意:有很多仓库,必须连接两个中转站中的一个(S1,S2),以至于可以与任意的仓库连接。有些仓库不能连相同的点,有些必须连相同的点,问所有方案中,最大距离的最小值


思路:二分最大距离,枚举每两个仓库,如果它们的某一种连接方法距离 大于 二分的距离的话,这种连接法矛盾,建边,SCC是否可行。一开始来一次SCC,来判断是否有方案。


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 5005;
const int maxm = 8e6+5;
struct edge
{
	int to,next;
}ed[maxm];

struct data
{
	int x,y;
}node[maxn],s1,s2,bian[maxn];

int head[maxn], tot; 
int Low[maxn], DFN[maxn], Stack[maxn];  
int Index, top;
bool Instack[maxn]; 
int block,belong[maxn];  
int zhong,cnt;
void init()
{
	memset(head, -1, sizeof(head));  
    memset(DFN, 0, sizeof(DFN));   
    memset(Instack, false, sizeof(Instack));   
    block = tot = 0;
    Index = top = 0;
}

void Tarjan(int u,int pre)
{
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u];i != -1;i = ed[i].next)
    {
        v = ed[i].to;
        if( !DFN[v] )
        {
            Tarjan(v,u);
            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])
    {
        block++;
        do
        {
            v = Stack[--top];
            Instack[v] = false;
            belong[v] = block;
        }
        while( v!=u );
    }
}

void add(int x,int y)
{
	ed[tot].to = y;
	ed[tot].next = head[x];
	head[x] = tot++;
}
void SCC(int n)
{  
    for(int i = 1; i <= n; i++)  
        if(!DFN[i]) 
            Tarjan(i,-1);  
}
int cal(int a,int b)
{
	if(b == 1)
		return abs(node[a].x-s1.x) + abs(node[a].y-s1.y);
	else
		return abs(node[a].x-s2.x) + abs(node[a].y-s2.y);
}

int check(int dis,int n)
{
	init();
	for(int i = 0; i < cnt; i++)
		add(bian[i].x,bian[i].y);
	for(int i = 1; i <= n; i++)
	{
		for(int j = i+1; j <= n; j++)
		{
			if(cal(i,1) + cal(j,2)+zhong > dis)
			{
				add(i,j);
				add(j+n,i+n);
			}
			if(cal(i,2) + cal(j,1)+zhong > dis)
			{
				add(i+n,j+n);
				add(j,i);
			}
			if(cal(i,1) + cal(j,1) > dis)
			{
				add(i,j+n);
				add(j,i+n);
			}
			if(cal(i,2) + cal(j,2) > dis)
			{
				add(i+n,j);
				add(j+n,i);
			}
		}
	}
	SCC(2*n);
	int flag = 1;
	for(int i = 1; i <= n; i++)
	{
		if(belong[i] == belong[i+n])
		{
			flag = 0;
			break;
		}
	}
	return flag;
}

int main(void)
{
	int n,m,a,b,c,d;
	while(scanf("%d%d%d",&n,&c,&d)!=EOF && n+m)
	{
		init();
		scanf("%d%d%d%d",&s1.x,&s1.y,&s2.x,&s2.y);
		for(int i = 1; i <= n; i++)
			scanf("%d%d",&node[i].x,&node[i].y);
		cnt = 0;
		while(c--)
		{
			scanf("%d%d",&a,&b);
			add(a,b+n);
			add(b,a+n);
			add(a+n,b);
			add(b+n,a);
			bian[cnt].x = a;
			bian[cnt++].y = b+n;
			bian[cnt].x = b;
			bian[cnt++].y = a+n;
			bian[cnt].x = a+n;
			bian[cnt++].y = b;
			bian[cnt].x = b+n;
			bian[cnt++].y = a;
		}
		while(d--)
		{
			scanf("%d%d",&a,&b);
			add(a,b);
			add(b+n,a+n);
			add(a+n,b+n);
			add(b,a);
			bian[cnt].x = a;
			bian[cnt++].y = b;
			bian[cnt].x = b+n;
			bian[cnt++].y = a+n;
			bian[cnt].x = a+n;
			bian[cnt++].y = b+n;
			bian[cnt].x = b;
			bian[cnt++].y = a;
		}
		SCC(2*n);
		int flag = 1;
		for(int i = 1; i <= n; i++)
		{
			if(belong[i] == belong[i+n])
			{
				flag = 0;
				break;
			}
		}
		if(!flag)
		{
			printf("-1\n");
			continue;
		}
		else
		{
			zhong = abs(s1.x - s2.x) + abs(s1.y - s2.y);
			int l = 0,r = 9e6+5,mid;
			while(l < r)
			{
				int mid = (l+r) / 2;
				if(check(mid,n))
					r = mid;
				else
					l = mid + 1;
			}
			printf("%d\n",l);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值