简体   繁体   English

如何使用 Dijkstra 算法在地铁中找到具有换乘时间的最短路径

[英]How to find Shortest Path in subway with transfer time using Dijkstra's Algorithm

I am facing a problem with a little trick.我面临一个小技巧的问题。 I need help!我需要帮助!

The problem is to find the shortest path in a subway from the start point to the destination.问题是在地铁中找到从起点到目的地的最短路径。 Subway data provides time taken between each node and its lines.地铁数据提供了每个节点与其线路之间的时间。 Every time you transfer(change line), it takes 5 minutes.每次换乘(换线)需要5分钟。

I tried to code this algorithm using Dijkstra's algorithm.我尝试使用 Dijkstra 算法编写此算法。 The major difference between Dijkstra's and this problem is that this problem has the possibility of changing the shortest path among those nodes already calculated(put them in set S). Dijkstra 和这个问题之间的主要区别在于,这个问题有可能改变那些已经计算好的节点之间的最短路径(把它们放在集合 S 中)。

For instance, there are A, B, C, D, E noded.例如,有 A、B、C、D、E 节点。 I want to find the shortest path from A to E.我想找到从 A 到 E 的最短路径。

Let's say A: line 1, line 2 B: line 1 C: line 1, line 2 D: line 2 E: line 1假设 A:第 1 行,第 2 行 B:第 1 行 C:第 1 行,第 2 行 D:第 2 行 E:第 1 行

A -> B: 2 minutes
B -> C: 2 minutes
A -> D: 1 minutes
D -> C: 2 minutes
C -> E: 2 minutes

In this case, the Dijkstra's algorithm will take in set S(calculated) node A first, and then node D, and then node B, and then node C, and then node E.在这种情况下,Dijkstra 算法将首先进入集合 S(计算)节点 A,然后是节点 D,然后是节点 B,然后是节点 C,然后是节点 E。

That is, the Dijkstra's algorithm will show A -> D -> C -> E as the shortest path with 10 minutes, since at node C, 5 minutes is required for transfer.也就是说,Dijkstra 的算法将显示 A -> D -> C -> E 为 10 分钟的最短路径,因为在节点 C 处,需要 5 分钟进行传输。 However, the actual shortest path is A->B->C->E with 6 minutes, since it does not require transfer time!然而,实际的最短路径是 A->B->C->E,用时 6 分钟,因为它不需要转机时间!

That is, the Dijkstra's path A->D->C should be modified to A->B->C after E is taken in the set S.即在集合 S 中取 E 后,Dijkstra 的路径 A->D->C 应修改为 A->B->C。

However, I don't know how to implement this idea to programming code.但是,我不知道如何将这个想法实现到编程代码中。 I'm using JAVA.我正在使用 JAVA。

Please help anyone!请帮助任何人! Give me any ideas!!给我任何想法!

Thank you谢谢

Dijkstra works just fine, just modify edge weights a bit, when there's an edge from node u to v and line[u] != line[v] we just need to increase edge length by 5 (transfer time) Dijkstra 工作得很好,只需稍微修改边权重,当从节点uvline[u] != line[v]有一条边时,我们只需将边长增加5 (transfer time)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM