#P3959. [NOIP 2017 提高组] 宝藏
[NOIP 2017 提高组] 宝藏
Description
Xiaoming, who is taking part in an archaeological dig, obtained a treasure map. The map marks treasure chambers buried deep underground, and gives developable roads between these chambers together with their lengths.
Xiaoming resolves to excavate the treasure in all chambers. However, every chamber is far from the surface. That is, opening a road from the surface to some chamber is difficult, whereas opening roads between chambers is much easier.
Moved by Xiaoming’s determination, the sponsor decides to open for free exactly one passage from the surface to a chamber, and Xiaoming can choose which chamber it leads to.
After that, Xiaoming must decide how to open roads between chambers. Any already opened road can be traversed arbitrarily at no cost. Each time a new road is opened, Xiaoming and the team excavate the treasure chambers that become reachable via that road. Moreover, Xiaoming does not want to open useless roads; that is, a road between two already excavated chambers need not be opened.
The cost to open a new road is , where is the road’s length, and is the number of chambers along the path from the sponsored chamber to the starting chamber of this road (including both the sponsored chamber and this road’s starting chamber).
Choose the sponsored chamber and the subsequent roads to open so that the total cost is minimized, and output this minimum.
Input Format
The first line contains two space-separated positive integers , the number of treasure chambers and the number of roads.
The next lines each contain three space-separated positive integers: the indices of two chambers connected by a road (numbered ), and the length of that road.
Output Format
Output a single positive integer, the minimum total cost.
4 5
1 2 1
1 3 3
1 4 1
2 3 4
3 4 1
4
4 5
1 2 1
1 3 3
1 4 1
2 3 4
3 4 2
5
Hint

[Sample explanation ]
Xiaoming chooses chamber to be opened by the sponsor. Xiaoming opens road and excavates chamber . He opens road and excavates chamber . He also opens road and excavates chamber .
The total cost is .
[Sample explanation ]
Xiaoming chooses chamber to be opened by the sponsor. Xiaoming opens road and excavates chamber . He opens road and excavates chamber . He also opens road and excavates chamber .
The total cost is .
Constraints
- For of the testdata: the input is guaranteed to be a tree, , , and all are equal.
- For of the testdata: , , , and all are equal.
- For of the testdata: , , .
- For of the testdata: , , .
: Added groups of hack testdata.
Translated by ChatGPT 5
京公网安备 11011102002149号