#P6643. [CCO 2020] Mountains and Valleys

[CCO 2020] Mountains and Valleys

Description

There is an undirected graph with NN vertices and MM edges, and each edge has a weight. The graph has no multiple edges and no self-loops. The vertices are numbered starting from 00.

Exactly N1N - 1 edges have weight 11. All other edges have weight N3\ge \left\lceil \frac{N}{3} \right\rceil and N\le N. Also, if we only consider the edges with weight 11, the whole graph forms a tree.

Now you need to traverse every vertex exactly once, while making the total weight of the edges you travel as small as possible.

Find the minimum possible total edge weight.

Reminder:

  • You may go back and forth along an edge.
  • You may choose any vertex to start from.
  • You have only one person.

Input Format

The first line contains two integers N,MN, M.

The next MM lines each contain three integers xi,yi,wix_i, y_i, w_i, indicating that there is an edge between xix_i and yiy_i with weight wiw_i.

Output Format

Output the minimum total edge weight required to traverse every vertex exactly once.

9 10
0 1 1
0 2 1
0 3 1
1 4 1
2 5 1
2 6 1
3 7 1
3 8 1
2 4 5
6 7 3

11

Hint

Sample Explanation

An optimal path is 41025267384\to 1\to 0\to 2\to 5\to 2\to 6\to 7\to 3\to 8, with total edge weight 1+1+1+1+1+1+3+1+1=111+1+1+1+1+1+3+1+1=11.

Subtasks

This problem uses bundled testdata.

  • Subtask 1 (44 points): N6N\le 6, M10M\le 10.
  • Subtask 2 (88 points): N20N\le 20, M40M\le 40.
  • Subtask 3 (88 points): N5×103N\le 5\times 10^3, M105M\le 10^5, and wi=1w_i=1 or N2wiN\left\lceil\frac{N}{2}\right\rceil\le w_i\le N.
  • Subtask 4 (2424 points): N100N\le 100, M200M\le 200.
  • Subtask 5 (88 points): N500N\le 500, M103M\le 10^3.
  • Subtask 6 (1212 points): N5×103N\le 5\times 10^3, M104M\le 10^4.
  • Subtask 7 (2020 points): N8×104N\le 8\times 10^4, M1.6×105M\le 1.6\times 10^5.
  • Subtask 8 (1616 points): No special restrictions.

For 100%100\% of the data, it is guaranteed that 4N5×1054\le N\le 5\times 10^5, N1M2×106N-1 \le M\le 2\times 10^6, 0xi,yiN10\le x_i,y_i\le N-1, and wi=1w_i=1 or N3wiN\left\lceil \frac{N}{3}\right\rceil\le w_i\le N.

Notes

This problem is translated from Canadian Computing Olympiad 2020 Day 1 T3 Mountains and Valleys。

Translated by ChatGPT 5