2014
02-17

# WuKong

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.

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.

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.

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

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.

/*

给定一个无向图，和两对起点终点，求两条最短路上的最多公共交点数。
反证法容易验证相交公共点比连续！！
那么我们假设存在2组数据 s1,e1,s2,e2;
我们用dp[i][j] 代表 从点i到点j最短路上最多有多少个点！
那么 map[s1][i]+map[i][j]+map[j][e1]=map[s1][e1] 不就表示i到j的最短路为 s1到e1最短路的子路嘛；
我们只需更新dp[i][j]中的最大值即可
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=502;
const int INF=1<<30;
int dp[maxn][maxn],map[maxn][maxn],ans,n,m;
void floyd()
{
int i,j,k;
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
if(map[i][k]!=INF&&i!=k)
for(j=1;j<=n;j++)
{
if(i==j||j==k)continue;
if(map[i][j]>map[i][k]+map[k][j])
{
map[i][j]=map[i][k]+map[k][j];
dp[i][j]=dp[i][k]+dp[k][j]-1;
}
else if(map[i][j]==map[i][k]+map[k][j]&&dp[i][j]<dp[i][k]+dp[k][j])
{
dp[i][j]=dp[i][k]+dp[k][j]-1;
}
}
}
}
}
int solve(int s1,int e1,int s2,int e2)
{
int i,j,res=0;
if(map[s1][e1]>=INF||map[s2][e2]>=INF)return 0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;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])
{
res=max(res,dp[i][j]);
}
}
}
return res;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
{
int i,j,u,v,w,s1,e1,s2,e2;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
map[i][j]=INF;
dp[i][j]=2;
}
dp[i][i]=1;
map[i][i]=0;
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
map[v][u]=map[u][v]=min(map[u][v],w);
}
floyd();
scanf("%d%d%d%d",&s1,&e1,&s2,&e2);
printf("%d\n",solve(s1,e1,s2,e2));
}
return 0;
}

1. 差距还要大，当初用粉碎机打过一次白菜馅。。。。如果说切丝和刮丝口感差距是3的话，粉碎机粉碎的白菜和手工的差距至少有10.。。根本不是一种东西了。。