
You are given the following graph. S = 1 and D = 3:

Friend 1 will visit location 1 in 1 unit time and then go to location 3 in 6 units of time. The total time is 7.
Friend 2 will go from 1 to 2 in 4 units time, then visited 2 in 1 unit time and go to 3 in 2 units. The total time is 7.
Friend 3 will go from 1 to 3 in 6 units and visit 3 in 1 unit. The total time is 7.
Hence everyone will reach location 3 in a maximum of 7 units of time. Therefore 7 is the answer.
The first line of input contains the single integer ‘T’ representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, representing the number of nodes and edges in the graph.
The second line of each test case contains two integers, ‘S’ and ‘D’, representing the starting point and destination.
The next M lines of each test case contain three space-separated integers ‘A’ ‘B’ and ‘W’, representing an edge from the node ‘A’ to the node ‘B’ whose weight is ‘W.’
For each test case, print a single integer representing the minimum time it will take such that one friend in the group will visit one location each and meet at location ‘D.’
For each test case, print a separate line.
1 <= T <= 10
1 <= N, M <= 10^5
1 <= A, B <= N
1 <= S, D <= N
1 <= W <= 10^7
Time Limit: 1 sec
In this approach, we will find the minimum distance between the source and every other node. Then we will reverse all the edges of the graph given and find the shortest distance between the destination and every other node. Then we will add the distances from both source and destination and add 1 to it. Then will be the maximum value of all the distances sum + 1.
We will create a function bellmanFord(edges, n, source). The parameter edge is the edge list, n is the number of nodes, and the source is the starting node. This function will return the list of distances of all nodes from the source.
Algorithm:
In this approach, we will do the same thing as the last approach, but we will use Dijkstra’s algorithm to find the distance between one node to every other node. Which is more efficient
We will create a function dijkstra(source ,graph). The parameter graph is the adjacency list, and the source is the starting node. This function will return the list of distances of all nodes from the source.
Algorithm: