shortest path with one edge turn to zero

廉价感情. 提交于 2019-12-03 17:16:29

You can solve this by using Djikstra's algorithm on an augmented graph of twice the size.

Suppose you have vertices 1...n.

Define a new graph such that for each edge a->b with weight w in the original, define edges a->b with weight w, a->b+n with weight 0, and a+n->b+n with weight w.

The idea is that the vertices n+1..n+n are duplicates containing a copy of the original graph. Moving from the original to the duplicate represents using your special ability of turning an edge to 0. Note that once you are in the duplicate there is no way of returning to the original graph so this special ability can only be used once.

Therefore you just need to solve the problem on the augmented graph of going from start to end+n to find the shortest path including your ability to set a single weight to 0.

Dijkstra's algorithm is commonly used to solve these types of problems. Also, this sounds a bit like the TSP problem, I could be wrong on that part though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!