


Input:
5 6
0 1 10
0 2 10
0 3 40
1 4 10
2 4 20
4 3 5
0 3 40

There are three ways to reach station ‘3’ from station ‘0’. The first path is 0->1->4->3, which will cost us 10+ 10+ 5= 25 and the second path 0->2->4->3 will cost us, 10+ 20+ 5 = 35, and the third path 0->3 will cost us 40.
We can’t take the first path since K = 1 and in this path, we are taking 2 stops, at station ‘1’ and station ‘4’.
Similarly, there are 2 stops in the second path. Hence we’ll finally choose the 3rd path i.e. 0->3, which is colored in blue.
The first line contains two space-separated integers, 'N' and 'M', the number of stations and the number of trains, respectively.
The next ‘M’ line has three space-separated integers, the source stations, the destination station, and the ticket price for traveling from this source to this destination.
The final line contains three space-separated integers, the source station, the destination station, and ‘K’, the maximum number of stations at which Ninja can stop.
The only line that contains the cheapest price from the given ‘source’ to ‘destination’ with up to ‘K’ stops or -1 if there is no such route.
You do not need to print anything, it has already been taken care of. Just implement the given function.
Depth-First Search is a graph traversal algorithm where we start from the root node and visits as far as possible along each branch before backtracking. We can modify our Dfs algorithm to break cycles i.e to avoid preprocessing of already preprocessed nodes.
We can generate the shortest path tree where the root will be our source station and the best way to do this is to use Dijkstra’s Algorithm for finding the shortest path between nodes in a graph.
We can see that for a node, we are finding the ticket price from that node to the destination multiple times, in order to avoid repeated calculations, we store the result in our ‘dp’ matrix.