#P8483. 「HGOI-1」Build
「HGOI-1」Build
Background
On a trip, arrived at a strange small town.
Problem Description
This town, together with the surrounding towns, plans to build a transportation network for a total of towns, with highways, that can connect all towns. Each highway will connect two different towns, meaning there will be no highway whose start and end are the same town.
However, building highways is expensive, so the mayors decided to share the cost. They want the total cost to be minimal.
Also, because different towns have different infrastructure, the construction cost differs by town. After negotiation, each highway will be built jointly by the two towns it connects, with each town responsible for half of the road. To finish the whole construction process at the same time, it is clear that a town may work on multiple roads at once, and the more roads it builds, the more money it needs to pay.
After calculation, the construction cost of each town can be described by a function. For town , there are three parameters , , . For town , when building its -th highway, the cost required for this highway is .
Now, the mayors want to provide a construction plan that meets the requirements and makes the total cost minimal.
Of course, passed this problem on to you.
Input Format
The first line contains two integers , .
The next lines each contain three integers , , .
Output Format
There are lines in total.
The first line contains one integer, the minimal cost.
The next lines each contain two integers , , representing an edge in your plan.
4 4
1 2 3
2 3 4
3 4 5
4 5 6
114
1 2
1 2
1 3
3 4
Hint
Constraints
This problem uses bundled testdata. There are in total, and the final score is the sum of the scores of all .
$$\def\arraystretch{1.5} \begin{array}{|c|c|c|}\hline \textbf{Task} & \textbf{Score} & \textbf{Special Constraints} \cr\hline 1 & 10 & n,m\le 500 \cr\hline 2 & 20 & n,m\le 5\times 10^3 \cr\hline 3 & 10 & \text{All towns have the same function}\cr\hline 4 & 20 & a_i=0 \cr\hline 5 & 20 & m=n-1 \cr\hline 6 & 20 & \cr\hline \end{array}$$For of the data, , , , , .
The data guarantees that the minimal cost fits in .
Notes
This problem has an . If the cost is correct, you can get of the score. For each , the score is the minimum among all test points in that .
If you do not know how to construct a plan, after outputting the minimal cost, you should still output lines, each with two integers in the range , to prevent the from failing.
Hack data has been added as . This does not count toward the score, but it will affect whether you get .
Translated by ChatGPT 5