There are ‘N’ cities numbered from 0 to ‘N’ - 1 and ‘M’ river connecting two cities in a Starlink Kingdom. The only way to travel from one city to another is by swimming through a river. Ninja is on a vacational journey and wants to do ‘Q’ travel. The energy required to swim against the river is ‘w’ and with the flow of river is zero, it means if the flow of river is from city ‘u’ to ‘v’ then the energy required to swim from ‘v’ to ‘u’ is ‘w’. Ninja has special power to reverse the direction of flow of water in the river. Ninja wants to minimize his energy consumption for this journey. Help Ninja to find the minimum energy required to complete his journey using his special power. He can reverse the direction of flow of any river any number of times before starting his journey( he may not use his power ).
Note : Energy consumption of one journey is equal to the sum of energy of each trip. There can be more than one river between any two cities.
The first line contains one positive integer ‘T’, denoting the number of test cases, then ‘T’ test cases follows
The first line of each test case contains three integers ‘N’ ‘M’ and ‘Q’, denoting the number of cities, the number of rivers and number of travel.
The next ‘M’ lines of each test case contains three space-separated integers ’u’ , ‘v’ and ‘w’, denoting connection between cities ‘u’ and ‘v’ where river flow from ‘u’ to ‘v’ and energy required against flow is ‘w’.
The next ‘Q’ lines of each test case contain two integers ‘x’ and ‘y’, denoting each travel from ‘x’ to ‘y’.
Output Format:
The first and the only line of each test case contains one integer ‘X’, denoting the minimum energy required to complete his journey using his special power ( he may not use his special power).
Output of each test case will be printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N, M, Q <= 1000
0 <= u, v <= N - 1, u != v
0 <= ‘x’, ‘y’ <= N - 1
Time Limit: 1 sec.
2
6 6 2
0 1 10
1 2 5
3 0 7
3 5 4
5 4 11
4 3 5
4 0
1 5
4 4 2
0 1 3
1 2 3
2 3 4
3 0 5
0 2
2 0
7
0
In the first test case, the Starlink Kingdom looks like this:

We can change the direction of the river flow between 0 and 1.
On the first trip we can go from city 4 to 0 without using any energy.
On the second trip we swam against the river 0 - 3. So the minimum energy required is 7.
In the second test case, the Starlink Kingdom looks like this:

We can change the direction of the river flow between (0 4) and (4 3).
On the first trip we can go from city 0 -> 1 -> 3 without using any energy.
On the second trip we can go from city 3 -> 4 -> 0 without using any energy.
So the total required is 0.
2
2 2 2
0 1 10
1 0 10
0 1
1 0
3 3 2
1 2 10
1 2 15
0 1 10
2 0
0 2
0
10
Can you think of something from the bridge tree? Count the number of up and down travel along any edge. Take help from lca.
From now onward we will call cities as vertices and rivers as edges.
If you work out with some examples, you will find that our answer depends upon the number of up moves and down moves through any bridge edge.
Let us first try to solve the same problem on a rooted tree.
If we do this in a straightforward fashion, then complexity would be O(n) per query, which is not fast. So we need to optimize it.
Extending the idea to trees, we define:
Up[edge] = ∑upHelper[vertex]
down[edge] = ∑ downHelper[vertex]
for all vertices in the subtree below that edge.
But our question is on graphs. Can we convert it to a tree ?
Yes, we can.
Note: If the graph was not connected, then instead of a single bridge tree, we would have had a forest of bridge trees, one for each connected component in the original graph.
The steps are as follows :
Let the name of the function be ‘minmumEnergy(n, m, q, rivers, travels)’ that returns the minimum energy of the whole journey. It accepts parameters ‘n’, ‘m’, ‘q’, ‘rivers’ and ‘travels’ denoting the number of vertices(cities), number of edges(rivers), number of travels, ‘rivers’ 2D array and ‘travels’ 2D array.
O(N(logN) +M), where ‘N’ is the number of cities and ‘M’ is the number of rivers.
The main work we are doing is creating graph that takes O(M), marking bridges that takes O(N + M), making bridge tree takes O(N + M), preprocessing to calculate LCA takes O(NlogN) and for each trave we are finding LCA take O(logN) and at last we are calculating our answer through standard dfs over tree takes O(N + M). Hence the overall time complexity is 3 * O(N + M) + O(M) + O(NlogN) +O(logN) = O(N(logN) +M), where ‘N’ is the number of cities and ‘M’ is the number of rivers.
O(M + N), where ‘N’ is the number of cities and ‘M’ is the number of rivers.
Mainly, we are using space in creating a graph, which takes O(N + M) space, visited array, ‘low’ array, ‘disc’ array for marking bridges which takes O(3 * N) space, creating Bridges may take O(N + M), preprocessing of LCA take O(N * 19). Hence, the overall space complexity is 2 * O(N + M) + 4 * O(N) = O(N + M).