简体   繁体   English

找到最短路径或最快路径

[英]Find the shortest path or the fastest path

I'm given a file with 600000 roads that are connected with junctions. 我得到了一个包含600000条与路口连接的道路的文件。 I have to make a program that finds the shortest path between 2 junctions. 我必须编写一个程序,以找到2个交叉点之间的最短路径。 But it also has to be possible to find the fastest path between 2 other junctions. 但是,还必须找到其他两个路口之间的最快路径。 I was thinking of using the BFS-algorithm to solve this problem, but I don't know if that's the right way to go. 我当时正在考虑使用BFS算法来解决此问题,但我不知道这是否是正确的方法。 I also found some other links that use Dijkstra's algorithm to solve this problem. 我还发现了一些其他的链接使用Dijkstra算法来解决这个问题。

I'm not looking for a complete code mashup, but I just want to know if I'm heading in the right direction. 我不是在寻找完整的代码混搭,而是想知道我是否朝着正确的方向前进。

PS: The input files look like this: PS:输入文件如下所示:

4370 6701 3079 2019 60
32840 9113 17817 7483 30
40758 13107 3445 2505 30
3074 11089 19209 2960 40

... ...

In the following format: Roadnumber - Start junction - End junction - Distance (in meters) - Speed limit (in km/h) 采用以下格式:道路编号-起点路口-终点路口-距离(以米为单位)-速度限制(以km / h为单位)

EDIT (for future reference): I decided to use Dijkstra's algorithm to solve this problem and it worked like a charm. 编辑(以供将来参考):我决定使用Dijkstra的算法来解决此问题,它的工作原理很吸引人。 Thank you very much everyone! 非常感谢大家! Turns out it only takes a few seconds to find the shortest path between 2 nodes in a graph of over 600000 nodes. 原来,仅需几秒钟即可在超过60万个节点的图中找到2个节点之间的最短路径。

Dijkstra is the good way to go here. Dijkstra是前往这里的好方法。 You just need to select a proper weight for the edges to represent distance and time...but that is really simple. 您只需要为边缘选择合适的权重即可代表距离和时间...但这确实很简单。

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

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