[Leetcode] Bus Routes 公交线路
Bus Routes
详细解题思路请访问:https://yanjia.me/zh/2018/11/...
We have a list of bus routes. Each
routes[i]
is a bus route that the i-th bus repeats forever. For example ifroutes[0] = [1, 5, 7]
`, this means that the first bus (0-th indexed) travels in the sequence1->5->7->1->5->7->1->...
forever.We start at bus stop S (initially not on a bus), and we want to go to bus stop T. Travelling by buses only, what is the least number of buses we must take to reach our destination? Return -1 if it is not possible.
Example:
Input:
routes = [[1, 2, 7], [3, 6, 7]]
S = 1
T = 6
Output: 2
Explanation:
The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
Note:
>1 <= routes.length <= 500.
1 <= routes[i].length <= 500.
0 <= routes[i][j] < 10 ^ 6.
代码
func searchRoute2(routes [][]int, graph map[int]map[int]bool, src, dst int) int {queue := []int{}for routeNum := range graph[src] {queue = append(queue, routeNum)}visited := map[int]bool{}dstRoutes := map[int]bool{}// once one of the route in this map get hit, we find the solutionfor routeNum := range graph[dst] {dstRoutes[routeNum] = true}times := 1// start BFSfor len(queue) != 0 {newQueue := []int{}for _, routeNum := range queue {if _, ok := dstRoutes[routeNum]; ok {return times}for _, stop := range routes[routeNum] {nextRouteNums := graph[stop]for nextRouteNum := range nextRouteNums {// only add route that has been visited before to avoid cycleif _, ok := visited[nextRouteNum]; !ok {newQueue = append(newQueue, nextRouteNum)visited[nextRouteNum] = true}}}}queue = newQueuetimes++}return -1
}// map bus stop number to bus route numbers
func buildGraph2(routes [][]int) map[int]map[int]bool {// use a map of map because route could be like 1->2->1->2graph := map[int]map[int]bool{}for i, route := range routes {for _, stop := range route {if _, ok := graph[stop]; ok {graph[stop][i] = true} else {graph[stop] = map[int]bool{i: true,}}}}return graph
}func numBusesToDestination(routes [][]int, S int, T int) int {if S == T {return 0}graph := buildGraph2(routes)return searchRoute2(routes, graph, S, T)
}
[Leetcode] Bus Routes 公交线路相关推荐
- Leetcode815.公交线路 Bus Routes(Java)
Leetcode815.公交线路 Bus Routes(Java) ##BFS## 广度优先搜索 2021-03 阿里春招题目 该题应该为图论题,边有权值的情况下寻找最短路径,采用多源BFS解决 示例 ...
- 线性代数三之状压DP的矩阵加速——Quad Tiling,Bus公交线路
状压与矩阵加速的藕断丝连 Quad Tiling description solution code [Hnoi2010]Bus 公交线路 description solution code Quad ...
- 【BZOJ2004】[Hnoi2010]Bus 公交线路 状压+矩阵乘法
[BZOJ2004][Hnoi2010]Bus 公交线路 Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1 ...
- 查询中国城市公交线路
<!-- 获取省份城市API接口:http://v.juhe.cn/postcode/pcd?key=e30b66c95726d5f9d7bf4bf9019b5e1f --> ...
- 基于android公交车线路查询论文文献,本科毕业论文---基于android的手机公交线路查询系统.doc...
毕 业 设 计( 论 文 ) 题目手机公交线路查询系统作者学院专业学号指导教师 摘 要 关键词: Abstract With the level of people's life improving, ...
- java实现找一条转乘次数最少的公交线路?,基于最优换乘次数的城市公交查询算法...
摘要:城市公交查询系统是一个城市非常重要的基础设施,也是城市文明的一个重要标志.该文探讨城市公交查询系统中最优换乘次数的查询算法.算法以图论中邻接矩阵为基础,结合矩阵算术运算的特点和公交查询系统的要求 ...
- 从高德地图获取城市公交线路+站点
公交线路+站点 在研究城市公共交通问题上,地面的公交网络研究算是很重要的一块,因此获取整个城市的公交线路及其站点的所有数据是非常重要的!但令人高兴的是,有关这些数据的获取其实是很方便快捷的,因为目前的 ...
- 课程设计:公交线路管理系统
#include <iostream> #include <string> #include<iomanip> #include <fstream> u ...
- 公交线路管理 数据结构课程设计
/************************************************* *版权所有 : 张志康 * *文件名称:bus.h,bus.cpp,show.cpp,main.c ...
最新文章
- 【Python】实现将testlink上的用例指定格式保存至Excel,用于修改上传
- C语言字符char和整型int的关系
- Linux常用命令全解
- 「Apollo」protobuf报错No module named ‘google‘
- P3402-[模板]可持久化并查集【主席树】
- IE6不兼容 CSS 改CSS没效果 原因
- JavaScript截断/切片/修剪掉字符串中的最后一个字符
- 快播之父卷土重来 发布升级版快播,首个区块链视频Bliface
- 使用poedit汉化wordpress的插件
- 多线程批量检测未注册域名
- 二维码怎么做到微信防封_分享在微信内做推广微信链接被屏蔽怎么解决,微信域名防封的技术分析
- 关于微信防撤回(文本、图片、语音、视频、名片等...)的Python学习教程
- redis命令之string类型incr/decr命令用法详情
- 如何卸载mysql教程(完全卸载)
- 14 Tornado - XSRF
- 你这一生还能陪妈妈几天?来看看
- jmeter 跳板机_jmeter本地通过跳板机连接塔内数据库
- Ancient Message (古埃及象形文字识别 Uva 1103)
- Python数学中裂项相消的计算-求前N项和
- linux rpm安装openssl,[CentOS]离线安装OpenSSL
热门文章
- boost::iostreams::newline用法的测试程序
- boost::dynamic_bitset模块boost::hash的测试程序
- boost::gregorian模块实现使用公历精确地推进一个月的测试程序
- boost::container实现多态双端队列的测试程序
- ITK:使用连通性细分具有相似统计信息的像素
- VTK:网格之TableBasedClipDataSetWithPolyData
- Qt Creator设置调试器
- C++打印0到N的Catalan数卡特兰数(附完整源码)
- C语言指针与函数传参
- 07_设置坐标轴刻度、设置刻度标签(Setting Tick Labels)、调整刻度标签 (Adjusting the ticklabels)