HDU2833:WuKong(Floyd 最大共同公共点)

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.




求两天最短路中共同的公共点的最大个数

用pa[i][j] 代表从i 与 j 的最短路之间上最多有多少个点

map[v0][i]+map[i][j]+map[j][u0]=map[v0][u0] 表示 v0到u0的最短路一种写法,

map[v0][i]表示起点到i的部分,map[i][j]表示公共部分(后面代码会说明),map[j][u0]表示j到终点的部分 


#include<stdio.h>
#include<string>
#define inf 9999999
int map[310][310];
int pa[310][310];//pa数组表示某一路径中的结点数目
int n;

void Floyd()
{
	int i,j,k,ans=0;
	for(k=1;k<=n;k++)
		for(j=1;j<=n;j++)
		{
			if(j==k||map[k][j]==inf) continue;
			for(i=1;i<=n;i++)
			{
				if(map[j][i]>map[j][k]+map[k][i])
				{
					map[j][i]=map[j][k]+map[k][i];//某两点之间的最短路
					pa[j][i]=pa[j][k]+pa[k][i]-1;//某两点之间的最短路中的结点数
				}
				//存在两条距离相等的最短路时,改路中的结点数目最多最好(求最大公共点的个数)
				if(map[j][i]==map[j][k]+map[k][i]&&pa[j][i]<pa[j][k]+pa[k][i]-1)
					pa[j][i]=pa[j][k]+pa[k][i]-1;
			}
		}
}
int solve(int s1,int e1,int s2,int e2)
{
	if(map[s1][e1]>=inf||map[s2][e2]>=inf)
		return 0;
	int ans=0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			//遍历两条最短路的公共部分,分别两条最短各自不同的相加起来,仍是原来的最短长度
			//则遍历的公共部分是存在的,保留此时的pa[i][j]存放的i与j之间的结点数目
			//随着遍历取最大值
			if(map[s1][i]+map[i][j]+map[j][e1]==map[s1][e1]
			&&map[s2][i]+map[i][j]+map[j][e2]==map[s2][e2])
				ans=ans>pa[i][j]?ans:pa[i][j];
	return ans;
}

int main()
{
	int m,i,j;
	while(scanf("%d%d",&n,&m),n+m)
	{
		for(i=1;i<=n;i++)
		{
			map[i][i]=0,pa[i][i]=1;
			for(j=i+1;j<=n;j++)
			{
				map[j][i]=map[i][j]=inf;
				pa[j][i]=pa[i][j]=2;
			}
		}
		int u,v,w;
		while(m--)
		{
			scanf("%d%d%d",&u,&v,&w);
			if(w<map[u][v])
				map[u][v]=map[v][u]=w;
		}
		int s1,e1,s2,e2;
		scanf("%d%d%d%d",&s1,&e1,&s2,&e2);
		Floyd();
		printf("%d\n",solve(s1,e1,s2,e2));
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值