So, what are you trying to optimize in your algorithm, this is the longest distance that you travel between two cities. Because how big your gas bottle should be. This is the shortest path option because you are trying to optimize the enire path length.
I think you could get away from this:
make a list of edges. (distance between each pair of cities)
remove the longest edge from the list if this does not make the destination inaccessible.
as soon as you can no longer remove the longest path, this means that this is your limiting factor when moving to your destination. The rest of the route no longer matters.
Then, at the end, you should have a list of edges that make up the path between the source and destination.
I have not proved that this solution is optimal, so there are no guarantees. But consider the following: if you delete the longest path, there are only shorter paths, so the maximum distance between the legs will not increase.
About complexity, time complexity O(n log n) , because you need to sort the edges.
Memory Complexity - O (n ^ 2)
This is probably not the most efficient algorithm, since it is a graph algorithm and does not use the fact that cities are on the Euclidean plane. There is probably some kind of optimization ...
source share