复习图--WuKong

2024-08-25 01:32
文章标签 复习 wukong

本文主要是介绍复习图--WuKong,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

E - WuKong
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit   Status

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

     
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.
题目大意,n个点m条路,然后是每条路连接的点以及长度,然后是四个数,由a到b 和由c到d,问在这两个路均是最短路的条件下,最多有几个点可以重合

要求最多的点重合,也就是说要在相同的最短路内尽量用更多的点,定义数组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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1104174

相关文章

【408数据结构】散列 (哈希)知识点集合复习考点题目

苏泽  “弃工从研”的路上很孤独,于是我记下了些许笔记相伴,希望能够帮助到大家    知识点 1. 散列查找 散列查找是一种高效的查找方法,它通过散列函数将关键字映射到数组的一个位置,从而实现快速查找。这种方法的时间复杂度平均为(

计算机基础知识复习9.6

点对点链路:两个相邻节点通过一个链路相连,没有第三者 应用:PPP协议,常用于广域网 广播式链路:所有主机共享通信介质 应用:早期的总线以太网,无线局域网,常用于局域网 典型拓扑结构:总线型 星型(逻辑总线型) 介质访问控制  静态划分信道 信道划分介质访问控制 频分多路复用FDM 时分多路复用TDM 波分多路复用WDM 码分多路复用CDM 动态分配信道 轮询访问介质访问控

【抽代复习笔记】28-群(二十二):四道子群例题

例1:证明,循环群的子群是循环群。 证:设G = (a),H ≤ G。 (1)若H = {e},则H是一阶循环群; (2)设H至少包含2个元素,即设H = {...,a^(-k),a^(-j),a^(-i),a^0,a^i,a^j,a^k,...}, 其中a^i是H中正指数最小的元素,0<i<j<k, 下证a^i是H的生成元: 对任意的a^t∈H(t∈Z),存在q∈Z,使得t = qi

西方社会学理论教程复习重点

一.名词解释 1.社会静力学:旨在揭示人类社会的基本秩序。它从社会的横断面,静态的考察人类社会的结构和制度,寻找确立和维护人类社会的共存和秩序的原则。 2.社会动力学:纵观人类理性和人类社会发展的先后必要阶段,所叙述的是这一基本秩序在达到实证主义这一最终阶段之前所经过的曲折历程。 3.社会事实:一切行为方式,不论它是固定的还是不固定的,凡是能从外部给予个人以约束的,或者说是普遍存在于该社会各

完整版自考西方文论选复习笔记资料

西方文论选读复习资料 1.柏拉图:古希腊哲学家,苏格拉底的学生。公园前387年在雅典城外建立学园开始授徒讲学,撰写对话。柏拉图的作品即《柏拉图文艺对话集》中讨论美学和文艺理论问题较多的有:《大希庇阿斯》、《伊安》、《高吉阿斯》、《会饮》、《斐德若》、《理想国》、《斐利布斯》、《法律》等。 ▲柏拉图《伊安》和《斐若德》内容:主要阐述了"迷狂说"和"灵魂回忆说":柏拉图认为,高明的诗人都是凭灵

ia复习笔记

HCIA 常用配置以及快捷键:! 查看时间:display clock;修改时间:clock datetime 11:11:11 2023-1-1 查看设备当前的配置:display current-configuration;查看已保存的配置:display saved-configuration;保存配置:save;查看历史的十条命令:display history-command;

android kotlin复习 Anonymous function 匿名函数

1、还是先上个图,新建kt: 2、代码: package com.jstonesoft.myapplication.testfun main(){val count = "helloworld".count()println(count);println("------------------------")var count2 = "helloworld".count(){it ==

C++复习day05

类和对象 1. 面向对象和面向过程的区别是什么?(开放性问题) 1. **抽象级别**:- **面向对象**:以对象(数据和方法的集合)为中心,强调的是数据和行为的封装。- **面向过程**:以过程(函数或子程序)为中心,强调的是步骤和顺序。2. **数据和方法的关系**:- **面向对象**:数据和处理数据的方法封装在对象中,对象可以包含数据和操作数据的方法。- **面向过程**:数据和处理

java复习第十课,方法的本质,形参和实参(很重要)

java的方法类似于其他语言的函数,是一段用来完成特定功能的代码片段,声明格式: 修饰符[public]  修饰符2[static]  返回值类型[int、String等]  方法名 (形参列表){ java语句列表..... } 形式参数:在方法被调用时用于接受外界输入的数据 实参:调用方法时实际传递给方法的数据 返回值:方法在执行完毕后返还给调用它的环境的数据 返回值类型:事先约

java复习第九课,break和continue语句

break:终止整个循环。在任何循环语句的主题部分,均可用break控制循环流程。break用于强行推出循环,不执行循环剩下的语句。 比如:当我循环打印1到10的数字,在数字8后加break,遇到break语句后,强行跳出循环体,不在往下执行。 continue:终止当次循环,执行下一次。语句用在循环语句体重,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环