#P10669. BZOJ3118 Orz the MST
BZOJ3118 Orz the MST
Problem Description
You are given a connected undirected weighted graph. For each edge , based on its original weight, increasing its weight by costs , and decreasing its weight by costs .
Now a spanning tree of the graph is specified. Find the minimum total cost needed to modify edge weights so that this spanning tree becomes a minimum spanning tree of the graph.
Input Format
The first line contains two positive integers , representing the number of vertices and edges in the graph. The vertices are numbered from to .
The next lines each contain positive integers , describing an edge with weight . Increasing the original weight by costs , and decreasing the original weight by costs . If is , the edge is in the specified spanning tree; if is , it is not. The data guarantees that the edges with form exactly a spanning tree of the original graph. There may be multiple different edges between two vertices, but there are no self-loops.
Output Format
Output a positive integer, the minimum total cost required.
6 8
1 2 3 1 4 2
1 4 2 0 3 4
2 3 5 1 2 1
2 4 4 1 3 5
3 5 2 0 1 3
3 6 1 0 2 4
4 5 7 1 3 2
5 6 5 1 5 4
21
Hint
Sample Explanation
The optimal plan is:
- Increase the weight of edge by , cost .
- Increase the weight of edge by , cost .
- Increase the weight of edge by , cost .
- Decrease the weight of edge by , cost .
The total cost is .
Constraints
For of the testdata, , .
Translated by ChatGPT 5