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

HDU 2833 WuKong 最短路径+DP

2017年11月22日 ⁄ 综合 ⁄ 共 2665字 ⁄ 字号 评论关闭

WuKong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1375    Accepted Submission(s): 496

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
/*
HDU 2833 最短路径+DP 
用Floyd求出任意两点间的最短路径 
两个定理: 
1.所求的路径一定是一断连续的路径 
2.如果路径(x,y)是a->b的最短路径中的一段,
	则min(a,b) = min(a,x) + min(x,y) + min(y,b) 
最后只需找到同时在两条最短路径上,且距离最长的那一段 
*/  
#include<iostream>
#include<stdio.h>
using namespace std;
#define N 305
#define MAX 999999999
int map[N][N];//记录最短路的长度  
int num[N][N];//同样长度的最短路的最长的跳数  
int n,m;

void init()
{
	int i,j;
	memset(num,0,sizeof(num));
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			map[i][j]=i==j?0:MAX;				
}

void Floyd()
{
	int k,i,j;
	for(k=1;k<=n;k++)
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
			{
				if(map[i][j]>map[i][k]+map[k][j])
				{
					map[i][j]=map[i][k]+map[k][j];
					num[i][j]=num[i][k]+num[k][j];
				}
				else if(map[i][j]==map[i][k]+map[k][j]&&num[i][j]<num[i][k]+num[k][j])
				{
					num[i][j]=num[i][k]+num[k][j];
				}
			}
}

int main()
{
	int i,j,a,b,c,d,v,ans;
//	freopen("test.txt","r",stdin);
	while(scanf("%d%d",&n,&m),n+m)
	{
		init();
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&a,&b,&v);	
			if(map[a][b]<v)
				continue;
			map[a][b]=map[b][a]=v;
			num[a][b]=num[b][a]=1;
		}
		Floyd();
		scanf("%d%d%d%d",&a,&b,&c,&d);
		ans=-1;
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				if(num[i][j]>ans&&(map[a][b]==map[a][i]+map[i][j]+map[j][b])&&(map[c][d]==map[c][i]+map[i][j]+map[j][d]))
					ans=num[i][j];
		printf("%d\n",ans+1);
	}
	return 0;
}

抱歉!评论已关闭.