# WuKong

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

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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 500
#define INF 9999999
int dp[N][N],d[N][N];
int n,m;
void init()
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
d[i][j]= i==j?0:INF;
for(int i=1; i<=n; i++) dp[i][i]=1;
}
void floyd()
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(d[i][j]>d[i][k]+d[k][j])
{
dp[i][j]=dp[i][k]+dp[k][j]-1;
d[i][j]=d[i][k]+d[k][j];
}
else if(d[i][j]==d[i][k]+d[k][j])
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]-1);
}
}
int solve(int ws,int we,int ts,int te)
{
int ans=0;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(d[ws][i]+d[i][j]+d[j][we]==d[ws][we]&&d[ts][i]+d[i][j]+d[j][te]==d[ts][te])
ans=max(ans,dp[i][j]);
return ans;
}
int main()
{
int u,v,w;
int ws,we,ts,te;
while(~scanf("%d %d",&n,&m)&&n)
{
init();
for(int i=0; i<m; i++)
{
scanf("%d %d %d",&u,&v,&w);
if(u==v) continue;
if(w<d[u][v]) d[u][v]=d[v][u]=w;
dp[u][v]=dp[v][u]=2;
}
scanf("%d %d %d %d",&ws,&we,&ts,&te);
floyd();
printf("%d\n",solve(ws,we,ts,te));
}
return 0;
}


#### HDU2833 WuKong（floyd + dp）经典

2015-04-18 19:55:07

#### 【HDU 2833】WuKong 【Floyd】

2016-11-08 11:37:01

#### hdu 2833 WuKong【floyd】

2015-05-25 20:27:41

#### HDU 2833 WuKong(floyd最短路）

2014-08-02 09:28:58

#### HDU：2112 HDU Today（floyd求最短路径+变形题）

2016-08-04 21:50:33

#### HDU 2833 WuKong floyd+dp

2016-02-29 16:59:12

#### HDU 2833 WuKong

2014-09-22 11:08:02

#### hdu 2833 WuKong

2012-08-12 13:43:16

#### HDU 2833 WuKong 【floyd最短路 + dp】

2018-01-31 17:13:37

#### HDU 2833 WuKong 最短路

2016-10-17 10:58:34

## 不良信息举报

hdu 2833 WuKong（floyd变形）