QOJ.ac

QOJ

时间限制: 1 s 内存限制: 256 MB 总分: 100 难度: [显示] 可 Hack ✓

#13847. Treasure

统计

Xiao Ming, who is involved in archaeology, has obtained a treasure map. The map marks $n$ treasure houses buried underground and provides $m$ roads that can be developed between these $n$ treasure houses, along with their lengths.

Xiao Ming is determined to visit the treasure houses in person to retrieve the treasures. However, each treasure house is very far from the surface, which means it is very difficult to build a road from the surface to a single treasure house, while developing roads between treasure houses is relatively much easier.

Moved by Xiao Ming's determination, an archaeological sponsor has decided to sponsor him for free to build one passage from the surface to a treasure house of his choice.

On this basis, Xiao Ming also needs to consider how to excavate roads between the treasure houses. Roads that have already been excavated can be traversed freely at no cost. When a new road is excavated, Xiao Ming will go out with the archaeological team to reach the treasure house at the end of that road. Additionally, Xiao Ming does not want to develop redundant roads, meaning that roads between two treasure houses that have already been connected do not need to be developed again.

The cost of developing a road is: The length of this road $\times$ the number of treasure houses passed through from the treasure house sponsored by the sponsor to the starting point of this road (including the treasure house sponsored by the sponsor and the starting point of this road).

Please write a program to help Xiao Ming select the treasure house to be sponsored and the subsequent roads to be excavated, such that the total cost of the project is minimized, and output this minimum value.

Input

The input file is named treasure.in. The first line contains two space-separated integers $n$ and $m$, representing the number of treasure houses and the number of roads. The next $m$ lines each contain three space-separated integers, representing the two treasure house IDs connected by a road (IDs are $1 \sim n$) and the length $v$ of this road.

Output

The output file is named treasure.out. Output a single line containing one integer, representing the minimum total cost for Xiao Ming.

Examples

Input 1

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

Output 1

4

Note 1

Xiao Ming chose to have the sponsor connect to treasure house 1. Xiao Ming developed road $1 \to 2$, excavating treasure house 2. He developed road $1 \to 4$, excavating treasure house 4. He also developed road $4 \to 3$, excavating treasure house 3. The total project cost is: $1 \times 1 + 1 \times 1 + 1 \times 2 = 4$ $(1 \to 2) \quad (1 \to 4) \quad (4 \to 3)$

Input 2

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

Output 2

5

Note 2

Xiao Ming chose to have the sponsor connect to treasure house 1. Xiao Ming developed road $1 \to 2$, excavating treasure house 2. He developed road $1 \to 3$, excavating treasure house 3. He also developed road $1 \to 4$, excavating treasure house 4. The total project cost is: $1 \times 1 + 3 \times 1 + 1 \times 1 = 5$ $(1 \to 2) \quad (1 \to 3) \quad (1 \to 4)$

Input 3

(See treasure/treasure3.in)

Output 3

(See treasure/treasure3.out)

Constraints

For 20% of the data: Guaranteed that the input is a tree, $1 \le n \le 8$, $v \le 5000$, and all $v$ are equal.

For 40% of the data: $1 \le n \le 8$, $0 \le m \le 1000$, $v \le 5000$, and all $v$ are equal.

For 70% of the data: $1 \le n \le 8$, $0 \le m \le 1000$, $v \le 5000$.

For 100% of the data: $1 \le n \le 12$, $0 \le m \le 1000$, $v \le 500000$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.