首页 > 图论 > 连通性问题 > HDU 1815 Building roads-图论-[解题报告] C++
2013
12-23

HDU 1815 Building roads-图论-[解题报告] C++

Building roads

问题描述 :

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

输入:

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

输出:

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.

样例输入:

4 1 1
12750 28546 15361 32055
6706 3887
10754 8166
12668 19380
15788 16059
3 4
2 3

样例输出:

53246

每个牛棚跟一个中转站相连,求最小的任意两牛棚间的距离中的最大值

 

又是2—SAT+二分验证求最小的最大值,

2—SAT:如果a与b矛盾,建边a—>b‘;

枚举任意两牛棚之间的距离,如果大于最大距离,就要相反的方式连接,

刚开始没有考虑两个中转站的距离,结果一直不对,还以为算法有bug,郁闷了半天。

如果两牛棚连接在两个不同的中转站,两点间的距离要加上中转站的距离

stdio.h>
#include<stack>
#include<string.h>
#define N 2000
#define M 1000
using namespace std;
struct edage
{
	int ed;
	int next;
}E[M*M];
struct op
{
	int x,y;
}P[N],P0,P1;
int n,low[N],dfs[N],ins[N],map[N*2],idx,ans,first[N],rfirst[N],belong[N],num,rnum,len;
stack<int>Q;
void addeage(int x,int y)
{
	E[num].ed=y;
	E[num].next=first[x];
	first[x]=num++;
}
int dis(op a,op b)
{
	return abs(a.x-b.x)+abs(a.y-b.y);
}
void Tarjan(int x)
{
	int p,v;
	low[x]=dfs[x]=idx++;
	ins[x]=1;
	Q.push(x);
	for(p=first[x];p!=-1;p=E[p].next)
	{
		v=E[p].ed;
		if(dfs[v]==-1)
		{
			Tarjan(v);
			low[x]=low[x]>low[v]?low[v]:low[x];
		}
		else if(ins[v]==1)
		{
			low[x]=low[x]>dfs[v]?dfs[v]:low[x];
		}
	}
	if(dfs[x]==low[x])
	{
		do
		{
			v=Q.top();
			Q.pop();
			ins[v]=0;
			belong[v]=ans;
		}while(v!=x);
		ans++;
	}
}
int sum=0;
int judge(int d,int min,int max)
{
	int i,j;
	memcpy(first,rfirst,sizeof(first));
	num=rnum;
	for(i=1;i<=n;i++)//枚举两点之间的距离
		for(j=i+1;j<=n;j++)
		{
			if(map[i]+map[j]>d)//连接相同同中转站不符合
			{
				addeage(i,j+n);
				addeage(j,i+n);
			}
			if(map[i]+map[j+n]+len>d)//连接不同的中转站不符合
			{
				addeage(i,j);
				addeage(j+n,i+n);
			}
			if(map[i+n]+map[j]+len>d)//连接不同的中转站不符合
			{
				addeage(i+n,j+n);
				addeage(j,i);
			}
			if(map[i+n]+map[j+n]>d)//连接相同同中转站不符合
			{
				addeage(i+n,j);
				addeage(j+n,i);
			}
		}
		memset(low,0,sizeof(low));
		memset(ins,0,sizeof(ins));
		memset(dfs,-1,sizeof(dfs));
		memset(belong,0,sizeof(belong));
		idx=ans=1;
		for(i=1;i<=n*2;i++)
		{
			if(dfs[i]==-1)
				Tarjan(i);
		}
		for(i=1;i<=n;i++)
			if(belong[i]==belong[i+n])
				return 0;
			return 1;
}
int main()
{
	int i,m,k,min,max,mid,flag,x,y,mmax;
	while(scanf("%d%d%d",&n,&m,&k)!=-1)
	{
		memset(first,-1,sizeof(first));
		num=0;
		scanf("%d%d%d%d",&P0.x,&P0.y,&P1.x,&P1.y);
		for(i=1;i<=n;i++)
			scanf("%d%d",&P[i].x,&P[i].y);
		min=999999999;
		max=-1;
		for(i=1;i<=n;i++)
		{
           map[i]=dis(P[i],P0);
		   map[i+n]=dis(P[i],P1);
		   if(min>map[i])
			   min=map[i];
		   if(min>map[i+n])
			   min=map[i+n];
		   if(max<map[i])
			   max=map[i];
		   if(max<map[i+n])
			   max=map[i+n];
		}
		len=dis(P0,P1);
		for(i=0;i<m;i++)
		{
			scanf("%d%d",&x,&y);
			addeage(x,y+n);
			addeage(y,x+n);
			addeage(x+n,y);
			addeage(y+n,x);
		}
		for(i=0;i<k;i++)
		{
			scanf("%d%d",&x,&y);
			addeage(x,y);
			addeage(x+n,y+n);
			addeage(y,x);
			addeage(y+n,x+n);
		}
		memcpy(rfirst,first,sizeof(first));
		rnum=num;
		 min=min*2,max=max*2+len;
		mmax=-1;flag=0;
		while(min<=max)
		{
			mid=(min+max)/2;
			if(judge(mid,min,max))
			{
				max=mid-1;
					mmax=mid;
			}
			else min=mid+1;
		}
		printf("%d\n",mmax);
	}
	return 0;
}

解题报告转自:http://blog.csdn.net/aixiaoling1314/article/details/9377397


  1. /*
    * =====================================================================================
    *
    * Filename: 1366.cc
    *
    * Description:
    *
    * Version: 1.0
    * Created: 2014年01月06日 14时52分14秒
    * Revision: none
    * Compiler: gcc
    *
    * Author: Wenxian Ni (Hello World~), [email protected]
    * Organization: AMS/ICT
    *
    * =====================================================================================
    */

    #include
    #include

    using namespace std;

    int main()
    {
    stack st;
    int n,i,j;
    int test;
    int a[100001];
    int b[100001];
    while(cin>>n)
    {
    for(i=1;i>a[i];
    for(i=1;i>b[i];
    //st.clear();
    while(!st.empty())
    st.pop();
    i = 1;
    j = 1;

    while(in)
    break;
    }
    while(!st.empty()&&st.top()==b[j])
    {
    st.pop();
    j++;
    }
    }
    if(st.empty())
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;
    }
    return 0;
    }

  2. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的