hdu 2833(Floyd + dp)

来源:互联网 发布:浴巾 知乎 编辑:程序博客网 时间:2024/05/23 20:13

WuKong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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 61 2 12 3 13 4 14 5 11 5 24 6 31 6 2 40 0
 

Sample Output
3Hint: 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.
 

题意:求两条最短路之间公共点的个数。
解题思路:这道题如果考虑的是普通的单源最短路,那会变得很难处理,我之前就是这么想的,无奈解不出来。这道题让我见识到了Floyd的强大,Floyd算法不仅要记住它的三层循环,更要理解算法的思路。这里定义的dp[i][j]为i->j的最短路上最多有多少个点。如何更新dp[i][j]是一个问题,但如果熟悉Floyd算法思想的话,那么它可以直接用Floyd更新即可。接下来是如何两条路的最多公共点,这里采用的是这样的策略:如果说map[s][i] + map[i][j] + map[j][e] == map[s][e],这意味着s->e的最短路必定经过i->j的最短路,那我们只需要枚举两条路的公共最短路即可。这个题确实是好题,熟悉Floyd算法的核心思想是关键。

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 305;const int inf = 0x3f3f3f3f;int n,m,dp[maxn][maxn],map[maxn][maxn];void floyd(){for(int k = 1; k <= n; k++)for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++){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 res = 0;for(int i = 1; i <= n; i++)for(int 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(){int u,v,w,s1,e1,s2,e2;while(scanf("%d%d",&n,&m),m+n){for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){map[i][j] = inf;dp[i][j] = 2;}dp[i][i] = 1;map[i][i] = 0;}for(int i = 1; i <= m; i++){scanf("%d%d%d",&u,&v,&w);map[u][v] = map[v][u] = 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;}


0 0
原创粉丝点击