首页 > 代码库 > 复习图--WuKong
复习图--WuKong
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 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.
要求最多的点重合,也就是说要在相同的最短路内尽量用更多的点,定义数组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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。