You are given a graph with nodes and
edges. Find the path with minimum length that goes through every node exactly once, then returns to the starting node.
Solve this problem for test cases.
Constraints
is equal across all test cases
.
The sum of across all test cases
is at most
.
Input Specification
The first line will contain a single integer .
There will then be test cases. Each test case will contain two space-separated integers
and
on the first line, followed by
lines each containing three space-separated integers
,
, and
, denoting an edge of length
from
to
.
Output Specification
For each test case, output a single integer denoting the minimum length of a path that traverses every node then returns to the starting node. If no such path exists, print -1
.
Sample Input
1
5 10
1 5 2
3 3 1
4 5 3
4 4 5
3 2 1
4 2 1
5 5 2
1 3 4
5 4 4
2 4 3
Sample Output
11
Explanation for Sample
The shortest path that fulfills the requirement has length . Note that this sample is provided for clarity, and the actual test data will have
.
Comments