
The secret underground paths cannot be used to travel both ways, you can’t travel from destination to source.
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.
The first line of each test case contains four space-separated integers, ‘N’, ‘K’, ‘S’ and ‘P’ denoting the number of colonies, number of houses in each colony, the number of secret underground paths and the maximum number of underground paths in a colony respectively.
The next line contains four space-separated integers “sourceHouse”, “sourceColony”, “destinationHouse” and “destinationColony” denoting the house number and colony number of Ninja’s initial position and house number and colony number of the destination house respectively.
The next ‘S’ line contains four space-separated integers each denoting the arrays “secretPaths[i]”.
For each test case, return an integer denoting the minimum time Ninja takes.
Output for each test case will be printed in a new line.
You do not need to print anything; it has already been taken care of. Just implement the given functions.
1 <= T <= 100
1 <= N <= 100
1 <= K <= 10^9
1 <= P <= 50
0 <= S <= (P * N) / 2
Where ‘T’ denotes the number of test cases,
‘N’ denotes the number of colonies,
‘K’ denotes the number of houses in each colony,
‘P’ denotes the maximum number of underground paths in a colony and
‘S’ denotes the number of underground paths.
Time Limit: 1 sec
We can initially create a graph with 2 nodes from each colony i.e. the first and last house of each colony would be included in the graph. We also create an edge from the node representing the last node of each colony to the node representing the first node of the next colony, and vice versa.
Now, we add into the graph each house that has an outgoing or incoming secret path. We also make edges from the source of the secret underground path to the destination of the secret underground path.
For nodes within the same colony, we add an edge between each pair of nodes.
Now that we have a graph ready, we can simply use BFS(Breadth First Search) to find the minimum distance between the nodes representing the source house and the destination house.
Algorithm: