hdu 2833(Floyd + dp)
生活随笔
收集整理的這篇文章主要介紹了
hdu 2833(Floyd + dp)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 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 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; }
總結
以上是生活随笔為你收集整理的hdu 2833(Floyd + dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eclipse 默认设置的换行长度
- 下一篇: VM虚拟机中Linux扩展磁盘空间的方法