


Choose two nodes ‘X’ and ‘Y’, such that there exists an edge from ‘X’ to ‘Y’.
Delete edge ‘X’ to ‘Y’.
Add edge ‘Y’ to ‘X’.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’ where ‘N’ denotes the total number of nodes and ‘M’ denotes the total number of edges in the graph.
The second line of each test case contains two space-separated integers ‘X’ and ‘Y’ where ‘X’ denotes the starting node of the path and ‘Y’ denotes the ending node of the path.
The next ‘M’ lines contain two space-separated integers ‘U’ and ‘V’ where ‘U’ and ‘V’ represent the nodes that share an edge from ‘U’ to ‘V’.
For each test case, print the minimum number of operations required to make at least one valid path from ‘A’ to ‘B’.
The output of each test case will be printed 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 <= 5
1 <= N <= 3000
1 <= M <= min(3000, N*(N-1)/2)
0 <= X, Y, U, V <= N - 1
Time Limit : 1 sec
The idea here is to make the graph undirected by adding all edges in the reverse direction. We will assign weight zero to all edges that are not reversed and weight 1 to all edges that are reversed. Then we will use the Dijkstra algorithm to find the minimum weight path from starting node to the ending node to get the minimum number of operations.
For example:
Below left side directed graph will be converted into right side undirected graph
Then we will find the minimum weight path in the right-side graph using the Dijkstra algorithm.
Algorithm: