本文主要是介绍复习图--WuKong,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Description
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
The input are ended with N=M=0, which should not be processed.
Output
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.
要求最多的点重合,也就是说要在相同的最短路内尽量用更多的点,定义数组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;
}
这篇关于复习图--WuKong的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!