编程介的小学生 2019-03-03 09:19 采纳率: 20.5%
浏览 371

计算最大公共点的两条最小路径的问题,是如何运用C语言的编程技术的

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
3

  • 写回答

1条回答 默认 最新

  • TRZNDP_Z 2019-03-03 12:12
    关注

    首先可以知道最短路一堆是一条链,当两条最短路有公共点时,公共点一定在一条连续的链上才是最优的(如果公共点不连续则中间不连续的点肯定能找到点使他们重合)。
    在知道了这个的情况之下,我们可以用floyd算法处理出图上任意两点的距离(记为dis[i][j])以及任意两点间最短路上经过的最多的点数(记为nump[i][j])
    预处理之后根据最上面的结论,我们可以枚举公共链使得这条链在最短路ab和cd上并且不断更新答案即可
    以下是主要转移方程
    for(i = 1 ; i <= n ; i++)
    for(j = 1 ; j <= n ; j++)
    if( num[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 = num[i][j] ;

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!