gpt4 book ai didi

c++ - 查找间接火车连接

转载 作者:太空狗 更新时间:2023-10-29 23:52:15 25 4
gpt4 key购买 nike

我必须找到两个给定城市代码之间的火车旅程,如果没有直达路线,那么我应该通过其他旅程找到一条间接路线。如果我想从 A 到 B,我可能必须从 A 到 C 再到 B。

我的火车路线文件格式为:出发代码目的地代码公司价格时间这着眼于两个城市代码之间的直接路线。

现在我已经将以下循环用于直接连接,并且它有效,我只需要间接连接方面的帮助。

// load file data into v1

string dep, dest;
cout << "\n\tEnter the departure: ";
cin >> dep;
cout << "\n\tEnter the destination: ";
cin >> dest;

for(int i = 0; i < v1.size(); ++i) {
// Departure() and Destination(), return the departure/destination codes
if (v1[i].Departure() == dep && v1[i].Destination() == dest)
// here I find all the direct routes
else
// indirect routes dealt with here
}

我认为对于间接路由,我必须在 else 部分处理它们。但我正在努力看看我会怎么做,我想我必须查看第一次出发的目的地并将其与我给定的目的地相匹配。

最佳答案

那里有一张图表。

有很多方法可以找到路径,有很多方法可以找到最短路径,还有很多方法可以找到最便宜路径。

这不是一个简单的 else 语句,但我建议您阅读以下内容:

http://en.wikipedia.org/wiki/Dijkstra's_algorithm

http://en.wikipedia.org/wiki/Shortest_path_problem

关于c++ - 查找间接火车连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16616761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com