简体   繁体   中英

K negative edges - single source shortest path

I've managed to solve the problem finding all single source shortest paths when there's exactly one negative edge using dijkstra.

Now i'm trying to face a new problem, how to find all shortest paths from a given source when there are exactly K negative edges using dijkstra only (Not bellman ford). (k is known).

Can't really think of a good way to do it.

any suggestions?

If it is a nondirectional graph, there is no single shortest path because even with a single negative edge, you could just go back and forth on that negative edge and have an infinite number of paths of negative infinity.

However, assuming a directional graph with no negative cycles, you could use Breadth First Search and keep track of both the negative edges you've already hit and the shortest path to each node you've discovered so far. If you see a node you've already visited, you only go there again if it would be better than the previous path you took to get there.

Since there are no negative cycles the algorithm must terminate. After the algorithm terminates the target node should have the best path that was used to get there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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