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

HDU2388 WuKong

2013年09月17日 ⁄ 综合 ⁄ 共 2586字 ⁄ 字号 评论关闭
Problem Description
 
Liyuan wanted to rewrite the famous book “Journey to the West” (“Xi You Ji” in Chinese pinyin). In the original book, the Monkey King Sun Wukong was trapped by the Buddha for 500 years, then he was rescued by Tang Monk, and began
his journey to the west. Liyuan thought it is too brutal for the monkey, so he changed the story:

One day, Wukong left his home - Mountain of Flower and Fruit, to the Dragon   King’s party, at the same time, Tang Monk left Baima Temple to the Lingyin Temple to deliver a lecture. They are both busy, so they will choose the shortest path. However, there may
be several different shortest paths between two places. Now the Buddha wants them to encounter on the road. To increase the possibility of their meeting, the Buddha wants to arrange the two routes to make their common places as many as possible. Of course,
the two routines should still be the shortest paths.

Unfortunately, the Buddha is not good at algorithm, so he ask you for help.

 

Input
 
There are several test cases in the input. The first line of each case contains the number of places N (1 <= N <= 300) and the number of roads M (1 <= M <= N*N), separated by a space. Then M lines follow, each of which contains three
integers a b c, indicating there is a road between place a and b, whose length is c. Please note the roads are undirected. The last line contains four integers A B C D, separated by spaces, indicating the start and end points of Wukong, and the start and end
points of Tang Monk respectively.

The input are ended with N=M=0, which should not be processed.

 

Output
 
Output one line for each case, indicating the maximum common points of the two shortest paths.
 

Sample Input
6 6 1 2 1 2 3 1 3 4 1 4 5 1 1 5 2 4 6 3 1 6 2 4 0 0
 

Sample Output
3 Hint: One possible arrangement is (1-2-3-4-6) for Wukong and (2-3-4) for Tang Monk. The number of common points are 3.
 

Floyd的应用 

用Floyd求出任意两点间的最短路径 

两个定理: 

1 .所求的路径一定是一断连续的路径 

2.如果路径(x,y)是a->b的最短路径中的一段,则min(a,b) = min(a,x) + min(x,y) + min(y,b) 最后只需找到同时在两条最短路径上,且距离最长的那一段 

代码:

 
#include<iostream>
using namespace std;
#define INF 200000000
int map[305][305],M[350][305],A[305][305];
int n,m;
void Floyd()
{
	int i,j,k;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
			A[i][j]=map[i][j];
	}
	for(k=1;k<=n;k++)
	{
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(A[i][j]>A[i][k]+A[k][j])
				{
					A[i][j]=A[i][k]+A[k][j];
					M[i][j]=M[i][k]+M[k][j];
				}
				else if(A[i][j]==A[i][k]+A[k][j]&&M[i][j]<M[i][k]+M[k][j])
				{
					M[i][j]=M[i][k]+M[k][j];
				}
			}
		}
	}
}
int main()
{
	int i,j;
	while(scanf("%d%d",&n,&m),m||n)
	{
		if(n==0&&m==0)
			break;
		memset(M,0,sizeof(M));
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=n;j++)
				map[i][j]=(i==j)?0:INF;
		}
		int a,b,w;
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&a,&b,&w);
			if(map[a][b]<w)
				continue;
				map[a][b]=w;
				map[b][a]=w;
				M[a][b]=1;
				M[b][a]=1;
		}
		Floyd();
		int A1,B,C,D;
		scanf("%d%d%d%d",&A1,&B,&C,&D);
		int ans=-1;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(M[i][j]>ans&&(A[A1][B]==A[A1][i]+A[i][j]+A[j][B])&&(A[C][D]==A[C][i]+A[i][j]+A[j][D]))
					ans=M[i][j];
			}
		}
		printf("%d\n",ans+1);
	}
	return 0;
}

抱歉!评论已关闭.