#P10669. BZOJ3118 Orz the MST

BZOJ3118 Orz the MST

Problem Description

You are given a connected undirected weighted graph. For each edge ii, based on its original weight, increasing its weight by 11 costs aia_i, and decreasing its weight by 11 costs bib_i.

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 n,mn, m, representing the number of vertices and edges in the graph. The vertices are numbered from 11 to nn.

The next mm lines each contain 66 positive integers ui,vi,wi,ffi,ai,biu_i, v_i, w_i, \textit{ff}_i, a_i, b_i, describing an edge (ui,vi)(u_i, v_i) with weight wiw_i. Increasing the original weight by 11 costs aia_i, and decreasing the original weight by 11 costs bib_i. If ffi\textit{ff}_i is 11, the edge is in the specified spanning tree; if ffi\textit{ff}_i is 00, it is not. The data guarantees that the edges with ffi=1\textit{ff}_i = 1 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 (1,4)(1,4) by 22, cost 66.
  • Increase the weight of edge (3,5)(3,5) by 33, cost 33.
  • Increase the weight of edge (3,6)(3,6) by 44, cost 88.
  • Decrease the weight of edge (4,5)(4,5) by 22, cost 44.

The total cost is 2121.

Constraints

For 100%100\% of the testdata, 1n3001 \leq n \leq 300, 1m,wi,ai,bi10001 \leq m, w_i, a_i, b_i \leq 1000.

Translated by ChatGPT 5