复习图--WuKong
来源:互联网 发布:农村人口老龄化数据 编辑:程序博客网 时间:2024/05/17 22:51
E - WuKong
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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
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.
要求最多的点重合,也就是说要在相同的最短路内尽量用更多的点,定义数组dis[i][j]表示i到j的最短距离,定义mm[i][j]表示i到j的最短距离需要几个点。相同的最短距离要求最多的点,弗洛伊德算法,求解每个点的最短路和需要的点数,遍历路线(i,j)使得 dis[a][b] = dis[a][i] + dis[i][j] + dis[j][b] ;满足条件的路线选取更多的点。
#include <cstdio>#include <cstring>#define INF 0x3f3f3f3f#include <algorithm>using namespace std;int dis[310][310] , mm[310][310] ;int main(){ int i , j , k , n , m , a , b , c , d ; while(~scanf("%d %d", &n, &m)) { if(n == 0 && m == 0) break; memset(dis,INF,sizeof(dis)); memset(mm,0,sizeof(mm)); while(m--) { scanf("%d %d %d", &i, &j, &k); if( dis[i][j] > k ) { dis[i][j] = dis[j][i] = k ; mm[i][j] = mm[j][i] = 1 ; } } scanf("%d %d %d %d", &a, &b, &c, &d); for(i = 1 ; i <= n ; i++) { dis[i][i] = 0 ; mm[i][i] = 0 ; } for(k = 1 ; k <= n ; k++) for(i = 1 ; i <= n ; i++) for(j = 1 ; j <= n ; j++) if( dis[i][j] > dis[i][k] + dis[k][j] || ( dis[i][j] == dis[i][k] + dis[k][j] && mm[i][j] < mm[i][k] + mm[k][j] ) ) { dis[i][j] = dis[i][k] + dis[k][j] ; mm[i][j] = mm[i][k] + mm[k][j] ; } int ans = -1 ; for(i = 1 ; i <= n ; i++) for(j = 1 ; j <= n ; j++) if( mm[i][j] > ans && dis[a][b] == dis[a][i] + dis[i][j] + dis[j][b] && dis[c][d] == dis[c][i] + dis[i][j] + dis[j][d] ) ans = mm[i][j] ; printf("%d\n", ans+1); } return 0;}
0 0
- 复习图--WuKong
- HDU2388 WuKong
- hdu2833 WuKong
- WuKong-HDU2833
- HDU2833 WuKong Floyd
- hdu 2833 WuKong
- hdu 2833 WuKong
- HDU 2833 WuKong
- hdu 2833 WuKong【floyd】
- DP WuKong hdu2833
- 【HDU 2833】WuKong 【Floyd】
- HDU-2833-WuKong
- HDU 2833 WuKong
- WuKong (Floyd + dp)
- HDU 2833——wukong
- 【HDU】2833 WuKong floyd+DP
- HDU 2833 WuKong floyd+dp
- HDU 2833 WuKong 最短路
- CodeForces 20C
- hdu3999 二叉树前序
- SSH免密码登陆步骤&jDK在Linux的安装
- 判断点是否在矩形内
- Java 获取硬件信息
- 复习图--WuKong
- zoj 1025 Wooden Sticks
- noip 2011 选择客栈
- java垃圾回收算法
- 1.8 C语言的文件系统
- WINCE--流设备驱动简介及往BSP添加
- Microsoft Interview: In binary tree, find two nodes' nearest common father
- 最小堆实现
- 2.3-删除链表中间节点