DP Finite Automata with Compression: Treasure Hunt

Bessie, who participated in the archaeological excavation, got a treasure map, and the treasure map marked n under ground treasure houses. There are total m possible roads available for development.

Bessie is determined to go personally to dig out the treasures in all the treasure houses.

However, each treasure house is very far from the ground, that is to say, it is very difficult to open a road from the ground to a certain treasure house, and it is relatively easy to develop a road between the treasure houses.

Bessie’s determination moved the sponsor of the archaeological excavation. The sponsor decided to sponsor her to open up a passage from the ground to a certain treasure house for free. Bessie can choose any of the n places to open a passage from the ground to that treasure house.

Based on this, Bessie also needs to consider how to dig the road between the treasure houses.

The roads that have been excavated can be used at will without cost.

Every time a new road is excavated, Bessie will work with the archaeological team to unearth the treasures of the treasure house that can be reached by that road.

In addition, Bessie does not want to develop useless roads, that is, the road between two treasure houses that have been excavated does not need to be developed.

The cost of developing a new road is:

The length of the road × The number of treasure houses that have passed from the treasure house that the sponsor helped her to open to the treasure house at the beginning of the road (including the treasure house that the sponsor helped her open and the treasure house at the beginning of the road).

Please write a program for Bessie to select the treasure house opened by the sponsor and the road to be excavated afterwards, so as to minimize the total cost of the project, and output this minimum value.

Input format

Two positive integers separated by spaces on the first line n with m, Represents the number of treasure houses and the number of roads.

Next m Line, each line has three positive integers separated by spaces, which are the numbers of the two treasure houses connected by a road (the numbers are 1 ∼ n ), and the length of the road v.

Output format The output is a line, a positive integer, which represents the smallest total cost.

data range 1 ≤ n ≤ 12, 0 ≤ m ≤ 1000, v ≤ 5 ∗ 105

Sample Input:

4 5
1 2 1
1 3 3
1 4 1
2 3 4
3 4 1


Sample Output:

4


Solution

Basically this question is to find a MST for all treasure houses and make the minimal cost to build MST.

• We use a bit vector to represent a subset of currently connected treasure houses.
• A 1 bit in position i means the i-th treasure house is included in the subsets
• dp[i][k] represents the minimal cost to build MST for subset i (bit vector), and the maximal depth of the MST is j.
• State transition:
for j in subset(i):
get the remaining set of j from i: remain = i ^ j
get the minimal cost to expand to all nodes in remain from any node in set j
dp[i][k] = min (dp[i][k], dp[j][k - 1] + min_cost_to_remain * k)