#P3959. [NOIP 2017 提高组] 宝藏

    ID: 2189 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心2017NOIp 提高组枚举,暴力状态压缩,状压

[NOIP 2017 提高组] 宝藏

Description

Xiaoming, who is taking part in an archaeological dig, obtained a treasure map. The map marks nn treasure chambers buried deep underground, and gives mm developable roads between these nn 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 L×KL \times K, where LL is the road’s length, and KK 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 n,mn,m, the number of treasure chambers and the number of roads.

The next mm lines each contain three space-separated positive integers: the indices of two chambers connected by a road (numbered 1n1-n), and the length vv 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 11]

Xiaoming chooses chamber 11 to be opened by the sponsor. Xiaoming opens road 121 \to 2 and excavates chamber 22. He opens road 141 \to 4 and excavates chamber 44. He also opens road 434 \to 3 and excavates chamber 33.

The total cost is 1×1+1×1+1×2=41 \times 1 + 1 \times 1 + 1 \times 2 = 4.

[Sample explanation 22]

Xiaoming chooses chamber 11 to be opened by the sponsor. Xiaoming opens road 121 \to 2 and excavates chamber 22. He opens road 131 \to 3 and excavates chamber 33. He also opens road 141 \to 4 and excavates chamber 44.

The total cost is 1×1+3×1+1×1=51 \times 1 + 3 \times 1 + 1 \times 1 = 5.

Constraints

  • For 20%20\% of the testdata: the input is guaranteed to be a tree, 1n81 \le n \le 8, v5×103v \le 5 \times 10^3, and all vv are equal.
  • For 40%40\% of the testdata: 1n81 \le n \le 8, 0m1030 \le m \le 10^3, v5×103v \le 5 \times 10^3, and all vv are equal.
  • For 70%70\% of the testdata: 1n81 \le n \le 8, 0m1030 \le m \le 10^3, v5×103v \le 5 \times 10^3.
  • For 100%100\% of the testdata: 1n121 \le n \le 12, 0m1030 \le m \le 10^3, v5×105v \le 5 \times 10^5.

upd 2022.7.27\text{upd 2022.7.27}: Added 5050 groups of hack testdata.

Translated by ChatGPT 5