


Consider If N = 2 , M = 4, Time = [[0, 2], [3, 0]] and Toll = [[0, 10], [ 5, 0]]]
The optimal path to reach node 1 from node 0 is 0 -> 1. The time required in the path is 2, and the minimum toll needed within the given time constraints is 10. Hence the answer is 10.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, denoting the number of states and given time limit.
The next ‘N’ line contains ‘N’ space-separated integers denoting the elements of the array ‘Time’ where ‘Time[i][j]’ represents the time needed to traverse from state ‘i’ to state ‘j’.
The next ‘N’ line contains ‘N’ space-separated integers denoting the elements of the array ‘Toll’ where ‘Toll[i][j]’ represents the toll needed to traverse from state ‘i’ to state ‘j’.
For each test case, print the minimum toll needed to reach the market from the port. If we cannot reach the market within the given time limit, then print -1.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
2 <= N <= 50
1 <= M <= 1000
0 <= Toll[i] <= 10^8
0 <= Time[i] <= 1000
Time limit: 1 sec
In this approach, we have to make a recursive function that returns the minimum toll needed to reach the N-1 node within the given time limit. We have to create a visited array of size N to maintain which vertices are already visited.
We will define a function dfs(N, M, Time, Toll, currToll, currIndex, visited) where N is the number of states, M is the time limit, Time is the time matrix, Toll is the toll matrix, currToll is the toll value calculated till now, currIndex is the current index, and visited is the array to check which indices are visited.
In this approach, we have to maintain a 2d array dist in which dist[i][j] stores the minimum Toll needed to reach index i within the j time limit. We have to make a queue q that we will use to fill this dist array. The queue q stores the array consisting of 3 elements denoting the vertex, time, and toll required to travel to that vertex. Initially insert [0, 0, 0] in the queue and set dist[0][0] as 0. We will iterate till the queue is not empty, and in each iteration, we will traverse through each vertex. We have to maintain a result variable that stores our actual answer. In the end, we will return the variable result.