
Suppose our query array is [ [ 0, 1, 2 ], [ 0, 2, 5 ] ] so we return False for the first query as there is no edge with a distance less than ‘2’ from ‘0’ to ‘1’.
For the second query, we return ‘True’ as we can indirectly i.e from ‘0’ to ‘1’ which has the weight ‘2’ less than ‘5’ and then ‘1’ to ‘2’ which has the weight ‘4’ less than ‘5’.
The first line of input contains a ‘T’ number of test cases.
The first line of each test case contains a number ‘N’ and ‘M’ representing the number of vertices and rows in array ‘EDGELIST’ respectively. Then, ‘M’ lines follow.
Each line contains ‘3’ space-separated integers denoting the row values where the first value is the starting point, the second value is the ending point and the third value is the weight of the edge.
After ‘M’ lines contain an integer value ‘Q’ denoting the number of queries then ‘Q lines follow
Each line contains ‘3’ space-separated integers denoting the row values where the first value is the starting point, the second value is the ending point and the third value is the weight of the edge.
For each test case, print a single line containing the answer for each query, denoted by ‘True’ if it is possible to go else return ‘False’.
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 <= 100
2 <= N <= 1000
2 <= | EDGELIST | , | QUERY | <= 1000
0 <= EDGELIST[i][j], QUERY[i][j] <= N - 1
Where ‘T’ represents the number of test cases, ‘N’ represents the number of vertices and query length represents the size of the ‘QUERY’ array and ‘EDGELIST’ length represents the size of the ‘EDGELIST’ array.
Time Limit: 1 seC.
The idea here is to use dfs and for each query, we have to check that from a given node in query is it possible to reach a given other node in the query by using limit on the weight of edges.
In this approach, we are using a union data structure for making the combination so now there is no need of checking edges, again and again, we just take the union of all edges which satisfies the limit.