Let's say i have a directed graph G(V,E) with positive integer weights on it's edges.What i need to do is find the minimum distance between some of it's nodes.In this minimum distance path i can use at most K reverse edges. For example, for this input:
6 (nodes)
9 (edges)
2 (positive integer K)
10 (number of edge pairs i need to find the minimum distance)
2 1 2 (edge for 2->1 with weight 2)
3 2 7 (edge for 3->2 with weight 7)
4 5 6
1 3 8
1 4 4
5 2 8
5 6 10 (edge 7)
1 5 5 (edge 8)
4 2 5 (edge 9)
1 6 1 (question 1:find minimum distance from 1->6 using 1 reverse edge)
3 5 0 (question 2:find minimum distance from 3->5 using 0 reverse edge)
1 2 0
3 5 1
1 2 1
4 3 1
6 4 0
2 6 2
6 4 1
6 4 2 (question 10)
The different parts of the input can be separated using the number of edges(9,first 9 lines containing 3 numbers) and numbers of questions asked(10,the 10 lines following after the edges)
Output has to be:
15 (Answer to question 1:minimum distance from 1->6 using 1 reverse edge is 15)
14
9
13
2
12
IMPOSSIBLE (there is no path between these two edges using 0 reverse edges)
17
24
16
I thought about running Dijkstra for each question and for each edge instead of just calculating the single minimum distance from source use reverse edges too and updating the value as much as possible without using more than K reverse edges.(The label would be something like (node number,minimum distance from source,reverse edges used).But dijkstra finds shortest paths from source to all other nodes and i think this is probably an overkill for my instance.Could this somehow be done more efficiently?