HDU 2833 WuKong 最短路径+DP
来源:互联网 发布:sqlserver 进阶 编辑:程序博客网 时间:2024/05/21 17:06
WuKong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1375 Accepted Submission(s): 496
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.
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.
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
3
/*HDU 2833 最短路径+DP 用Floyd求出任意两点间的最短路径 两个定理: 1.所求的路径一定是一断连续的路径 2.如果路径(x,y)是a->b的最短路径中的一段,则min(a,b) = min(a,x) + min(x,y) + min(y,b) 最后只需找到同时在两条最短路径上,且距离最长的那一段 */ #include<iostream>#include<stdio.h>using namespace std;#define N 305#define MAX 999999999int map[N][N];//记录最短路的长度 int num[N][N];//同样长度的最短路的最长的跳数 int n,m;void init(){int i,j;memset(num,0,sizeof(num));for(i=1;i<=n;i++)for(j=1;j<=n;j++)map[i][j]=i==j?0:MAX;}void Floyd(){int k,i,j;for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++){if(map[i][j]>map[i][k]+map[k][j]){map[i][j]=map[i][k]+map[k][j];num[i][j]=num[i][k]+num[k][j];}else if(map[i][j]==map[i][k]+map[k][j]&&num[i][j]<num[i][k]+num[k][j]){num[i][j]=num[i][k]+num[k][j];}}}int main(){int i,j,a,b,c,d,v,ans;//freopen("test.txt","r",stdin);while(scanf("%d%d",&n,&m),n+m){init();for(i=0;i<m;i++){scanf("%d%d%d",&a,&b,&v);if(map[a][b]<v)continue;map[a][b]=map[b][a]=v;num[a][b]=num[b][a]=1;}Floyd();scanf("%d%d%d%d",&a,&b,&c,&d);ans=-1;for(i=1;i<=n;i++)for(j=1;j<=n;j++)if(num[i][j]>ans&&(map[a][b]==map[a][i]+map[i][j]+map[j][b])&&(map[c][d]==map[c][i]+map[i][j]+map[j][d]))ans=num[i][j];printf("%d\n",ans+1);}return 0;}
0 0
- HDU 2833 WuKong 最短路径+DP
- hdu 2833 WuKong(最短路径+记忆化搜索)
- WuKong 最短路加DP HDU 2833
- hdu 2833 WuKong(最短路 + dp)
- 【HDU】2833 WuKong floyd+DP
- HDU 2833 WuKong floyd+dp
- HDU 2833 WuKong 最短路
- HDU 2833 WuKong(floyd+dp)
- hdu 2833 WuKong【Floyd+dp】【好题】
- HDU 2833 WuKong(floyd最短路)
- hdu 最短路径
- hdu 最短路径
- HDU 3339 In Action (最短路径+DP(01背包))
- HDU 3790 最短路径问题 (双重权值) dp
- hdu3339(dp+最短路径)
- hdu 2833 WuKong
- hdu 2833 WuKong
- HDU 2833 WuKong
- redis集群
- for loop initial declaration used outside C99 mode
- 二路归并排序
- android 多线程断点续传下载 一
- app banner设计
- HDU 2833 WuKong 最短路径+DP
- CentOS 64位 安装32位运行库
- hdoj 2544 最短路 【最短路径模板题】
- Fireshell的使用说明
- android 多线程断点续传下载 二
- 如何修改[!--show.listpage--]在列表页中的展示?
- Codeforces Round #277 (Div. 2)
- Longest Consecutive Sequence(leetcode)
- 开通第一天