【HDU 2833】WuKong 【Floyd】

来源:互联网 发布:手机端ping软件 编辑:程序博客网 时间:2024/04/29 04:49

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.

题意


给定一个无向图,和两对起点终点,求两条最短路上的最多公共交点数。

题解


这里用到了一个性质,最短路的子路径也是最短路。
d(s,v)=dis(s,u)+dis(u,v) u属于S-V最短路径上的点。
所以,如果(s1,t1),(s2,t2)的最短路径上有交点则,设交点为p1,从p1开始到p2这段连续的路上都是2者的公共路径,之后就不再有公共路径。
简述为 两条最短路径里面公共的那部分子路径一定是连续的


一个数组维护最短路,一个数组维护该最短路最多的点。


题很神,数据规模很小,做法很蠢。

#include<bits/stdc++.h>using namespace std;#define rep(i,s,t) for(int i=(s);i<=t;i++)#define INF (1<<28)#define N 310int n,m,f[N][N],g[N][N];void Floyd(){    rep(k,1,n)rep(i,1,n)    rep(j,1,n){        if(f[i][k]+f[k][j]<f[i][j]){            f[i][j] = f[i][k]+f[k][j];            g[i][j] = g[i][k]+g[k][j]-1;        }        else if(f[i][k]+f[k][j]==f[i][j]&&g[i][k]+g[k][j]-1>g[i][j])            g[i][j] = g[i][k]+g[k][j]-1;                        }               }int solve(int s1,int t1,int s2,int t2){    if(f[s1][t1]==INF||f[s2][t2]==INF)return 0;    int ret = 0;    rep(i,1,n)rep(j,1,n)        if(f[s1][i]+f[i][j]+f[j][t1]==f[s1][t1]        &&f[s2][i]+f[i][j]+f[j][t2]==f[s2][t2])            ret = max(ret,g[i][j]);     return ret;}int main(){    int a,b,c,s1,s2,t1,t2;    while(scanf("%d%d",&n,&m),n+m){        rep(i,1,n)rep(j,1,n){            if(i!=j)f[i][j]=INF,g[i][j]=2;            else f[i][j]=0,g[i][j]=1;        }        rep(i,1,m){            scanf("%d%d%d",&a,&b,&c);            c<f[a][b]?f[a][b]=f[b][a]=c:1;        }        Floyd();        scanf("%d%d%d%d",&s1,&t1,&s2,&t2);        printf("%d\n",solve(s1,t1,s2,t2));    }       return 0;}
0 0