Spy Tree
View as PDFAnya is playing a spy game! The map that she is currently on is a weighted connected graph with  nodes and 
 edges, where the weight of the 
 edge is 
. It is guaranteed that the graph has no duplicate edges or self-loops.
To conquer the map and move on to the next level, Anya must complete  levels. The 
 level will consist of exactly 
 nodes, each of which contains an enemy attacker that Anya must take out. Even though Anya is a spy that can take out any opponent, the one thing she cannot do is teleport instantly, so she needs your help to help her calculate some costs.
More specifically, since Anya always wants to be prepared, she wants you to find the maximum cost between two distinct nodes in the level. The cost between two nodes is defined as the minimum number of edges required to travel from one node to the other plus the sum of the costs of the edges on the path. Help Anya find out the answer for each of the queries!
Constraints
The sum of all  will not exceed 
.
Subtask 1 [30%]
Subtask 2 [69%]
Subtask 3 [1%]
No additional constraints.
Input Specification
The first line of input will contain  and 
 separated by a single space. The next 
 lines will each contain three integers 
 denoting the two endpoints and the weight of the 
 edge. The final 
 lines of input will each contain the integer 
 followed by 
 integers denoting the enemy nodes for the query.
Output Specification
Output  lines, the 
 of which is the answer to the 
 query.
Sample Input
6 2
1 2 3
2 3 5
1 4 1
4 5 2
2 6 5
3 3 5 6
3 1 2 6
Sample Output
15
10
Explanation
In the first query, the cost of the pair  is equal to 
. This is because the minimum number of roads required to travel from 
 to 
 is 
 and the sum of costs on these roads is equal to 
. It can be proven that there are no better pairs, so the answer is equal to 
.
The optimal pairing for the second query is .
Comments