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 if routes[0] = [1, 5, 7]`, this means that the first bus (0-th indexed) travels in the sequence 1->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 公交线路相关推荐

  1. Leetcode815.公交线路 Bus Routes(Java)

    Leetcode815.公交线路 Bus Routes(Java) ##BFS## 广度优先搜索 2021-03 阿里春招题目 该题应该为图论题,边有权值的情况下寻找最短路径,采用多源BFS解决 示例 ...

  2. 线性代数三之状压DP的矩阵加速——Quad Tiling,Bus公交线路

    状压与矩阵加速的藕断丝连 Quad Tiling description solution code [Hnoi2010]Bus 公交线路 description solution code Quad ...

  3. 【BZOJ2004】[Hnoi2010]Bus 公交线路 状压+矩阵乘法

    [BZOJ2004][Hnoi2010]Bus 公交线路 Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1 ...

  4. 查询中国城市公交线路

    <!--      获取省份城市API接口:http://v.juhe.cn/postcode/pcd?key=e30b66c95726d5f9d7bf4bf9019b5e1f  --> ...

  5. 基于android公交车线路查询论文文献,本科毕业论文---基于android的手机公交线路查询系统.doc...

    毕 业 设 计( 论 文 ) 题目手机公交线路查询系统作者学院专业学号指导教师 摘 要 关键词: Abstract With the level of people's life improving, ...

  6. java实现找一条转乘次数最少的公交线路?,基于最优换乘次数的城市公交查询算法...

    摘要:城市公交查询系统是一个城市非常重要的基础设施,也是城市文明的一个重要标志.该文探讨城市公交查询系统中最优换乘次数的查询算法.算法以图论中邻接矩阵为基础,结合矩阵算术运算的特点和公交查询系统的要求 ...

  7. 从高德地图获取城市公交线路+站点

    公交线路+站点 在研究城市公共交通问题上,地面的公交网络研究算是很重要的一块,因此获取整个城市的公交线路及其站点的所有数据是非常重要的!但令人高兴的是,有关这些数据的获取其实是很方便快捷的,因为目前的 ...

  8. 课程设计:公交线路管理系统

    #include <iostream> #include <string> #include<iomanip> #include <fstream> u ...

  9. 公交线路管理 数据结构课程设计

    /************************************************* *版权所有 : 张志康 * *文件名称:bus.h,bus.cpp,show.cpp,main.c ...

最新文章

  1. 【Python】实现将testlink上的用例指定格式保存至Excel,用于修改上传
  2. C语言字符char和整型int的关系
  3. Linux常用命令全解
  4. 「Apollo」protobuf报错No module named ‘google‘
  5. P3402-[模板]可持久化并查集【主席树】
  6. IE6不兼容 CSS 改CSS没效果 原因
  7. JavaScript截断/切片/修剪掉字符串中的最后一个字符
  8. 快播之父卷土重来 发布升级版快播,首个区块链视频Bliface
  9. 使用poedit汉化wordpress的插件
  10. 多线程批量检测未注册域名
  11. 二维码怎么做到微信防封_分享在微信内做推广微信链接被屏蔽怎么解决,微信域名防封的技术分析
  12. 关于微信防撤回(文本、图片、语音、视频、名片等...)的Python学习教程
  13. redis命令之string类型incr/decr命令用法详情
  14. 如何卸载mysql教程(完全卸载)
  15. 14 Tornado - XSRF
  16. 你这一生还能陪妈妈几天?来看看
  17. jmeter 跳板机_jmeter本地通过跳板机连接塔内数据库
  18. Ancient Message (古埃及象形文字识别 Uva 1103)
  19. Python数学中裂项相消的计算-求前N项和
  20. linux rpm安装openssl,[CentOS]离线安装OpenSSL

热门文章

  1. boost::iostreams::newline用法的测试程序
  2. boost::dynamic_bitset模块boost::hash的测试程序
  3. boost::gregorian模块实现使用公历精确地推进一个月的测试程序
  4. boost::container实现多态双端队列的测试程序
  5. ITK:使用连通性细分具有相似统计信息的像素
  6. VTK:网格之TableBasedClipDataSetWithPolyData
  7. Qt Creator设置调试器
  8. C++打印0到N的Catalan数卡特兰数(附完整源码)
  9. C语言指针与函数传参
  10. 07_设置坐标轴刻度、设置刻度标签(Setting Tick Labels)、调整刻度标签 (Adjusting the ticklabels)