Shortest path in matrix with obstacles with cheat paths

前端 未结 2 1169
挽巷
挽巷 2021-01-24 18:51

First of all this is an assingment and I am not looking for direct answers but instead the complexity of the best solution as you might be thinking it .

This is the know

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-24 19:22

    You can just build a new graph with nodes labeled by (N, w) where N is a node in the original graph (so a position in your matrix), and w=0 or 1 is whether you're carrying a weight. It's then quite easy to add all possible edges in this graph

    This new graph is of size 2*V, not V^2 (and the number of edges is around 4*V+number(B)).

    Then you can use a shortest path algorithm, for instance Dijkstra's algorithm: complexity O(E + V log(V)) which is O(V log(V)) in your case.

提交回复
热议问题