#P4412. [SHOI2004] 最小生成树
[SHOI2004] 最小生成树
Description
Given a simple graph , where is the set of vertices, is the set of edges (no multiple edges; i.e., there is at most one edge between any two vertices), and is an integer-valued weight function defined on . A spanning tree on is given. Modify with the minimum total cost so that becomes a minimum spanning tree on (a graph may have multiple minimum spanning trees; it suffices that the sum of edge weights of is minimum). For any edge , the modification rules are:
- Increase the weight of : set , and the cost to modify this edge is .
- Decrease the weight of : set , and the cost to modify this edge is .
- Do not change the weight of : set , with modification cost .
Note: The modified weight function also takes integer values.
The total modification cost is .
Input Format
The first line contains , where is the number of vertices and is the number of edges. Vertices are numbered .
The next lines each contain three integers , indicating there is an edge between vertices and with weight .
Each edge appears exactly once in the input. Then, the next lines each contain two integers , indicating that the edge between and is an edge of .
Output Format
Output the minimum .
6 9
1 2 2
1 3 2
2 3 3
3 4 3
1 5 1
2 6 3
4 5 4
4 6 7
5 6 6
1 3
2 3
3 4
4 5
4 6
8
Hint
Edge has its weight changed from to , with cost ;
edge has its weight changed from to , with cost ;
edge has its weight changed from to , with cost .
Therefore, the total cost is .
Constraints: , , .
Translated by ChatGPT 5
京公网安备 11011102002149号