The city of Chittagong can be considered a weighted directed acyclic graph (DAG) of nodes and edges. There are friends who want to visit the city. But visiting all the nodes for each friend can be too much of a hassle. So, we consider a node is visited when at least one of the friends visited it.
The friends can start from any node they want. That is, any friend can start in any node and multiple friends can also start in a single node too.
Now, they will start moving. At each moment,
A friend can move from node to if, the friend is currently in node and there is an edge from to . The cost of this operation is the weight of the edge .
A friend can stay in his current node. The cost of this operation is .
The total cost is the sum of cost of all operations. Help the friends to find the minimum total cost to visit all the nodes or output , if it is impossible to visit all the nodes by friends.
The first line of the input contains a single integer — the number of test cases.
The first line of each test case contains three integers and — the number of nodes, the number of edges and the number of friends.
The following lines describe the edges. Each contains three integers — the directed edge and the weight of the edge.
It is guaranteed that, the given graph is directed acyclic and there are no multiple edges.
For each test case, if it is impossible to visit all the nodes output . Otherwise, output the minimum total cost to visit all the nodes.
Input | Output |
---|---|
3 6 5 2 1 3 3 2 3 2 3 4 5 4 5 4 4 6 7 6 5 3 1 3 3 2 3 2 3 4 5 4 5 4 4 6 7 6 5 1 1 3 3 2 3 2 3 4 5 4 5 4 4 6 7 | 26 11 -1 |
Test case : The following can be the optimal paths. Friend : , Cost: . Friend : , Cost: . All the nodes are visited. Total Cost: . Test case : The following can be the optimal paths. Friend : , Cost: . Friend : , Cost: . Friend : , Cost: . All the nodes are visited. Total Cost: . Test case : It is impossible to visit all the nodes with just friend. |
Input | Output |
---|---|
3 5 4 2 1 2 2 2 3 1 3 4 1 4 5 5 6 0 6 6 0 5 | 4 0 -1 |