


Consider if ‘N’ = 3 and edges are
[[0, 1, 2],
[1, 2, 4]]
'P' = [2, 3, 4], and we have to reach from junction 0 to 2.
The time consumed from junction 0 to 1 is 2. We have to wait for 1 for the next green flash at junction 1. The time consumed from junction 1 to 2 is 4. The path 0 -> 1 -> 2 takes time 2 + 1 (wait till 3) + 4 = 7. Hence, the answer is 7.
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 junctions and roads.
The next line contains ‘N’ space-separated integers denoting the elements of the array ‘P’ elements where ‘P[i]’ is the time needed at junction ‘i’ for light to turn green.
The next ‘M’ lines of each test case contain three space-separated integers denoting the road between ‘Edges[i][0]’ and ‘Edges[i][1]’ junctions with ‘Edges[i][2]’ denoting the time to travel the road.
The last line of each test case contains two space-separated integers, 'src' and 'des', denoting the source and destination junctions.
For each test case, print a single integer that denotes the minimum time needed from the junction ‘src’ to ‘des’. Print ‘-1’ in case we cannot reach the ‘des’ junction.
Print the output of each test case in a separate line.
1 <= T <= 5
2 <= N <= 10^5
1 <= M <= 10^5
1 <= P[i] <= 10^7
0 <= Edges[i][0] , Edges[i][1] < N
1 <= Edges[i][2] <= 10^7
0 <= src, des < ‘N’
Time limit: 1 sec
This approach will use DFS(Depth First Search) to make a recursive function in which we pass the currCost, which is the current cost. If the light is not green when we reach that junction, we have to add the waiting time in currCost. After that, we will call the recursive function on all the adjacent unvisited junctions by incrementing the currCost with the weight of the edge. We will maintain a variable minVal to store the minimum cost that is returned from the recursive calls. In the end, we will return the variable minVal.
In this approach, we will use the Dijkstra Algorithm to solve the problem. We will make a visited array and a min priority queue of pairs of integers where the first element in the pair is cost and the second element is vertex number. So initially, we insert a pair of 0 and src in our min priority queue pq, then run a loop until pq is not empty. We delete the front pair in the loop and then insert the adjacent vertices in the queue pq. If the front pair has des as its vertex means we reach the des, then return the first element of the queue’s front pair, which is the minimum cost to reach des from src.