Description
An undirected Weighted Connected Graph with black or white edges. Let you find a generative tree with a need white edge that has the minimum weight.
The question must be resolved.
Input
The first line V, E, and need indicate the number of points, the number of edges, and the number of required white edges.
Next, line E, each line of S, T, C, Col indicates the endpoint (vertex starts from 0), Edge Weight, color (0 white 1 black ).
Output
A row indicates the Edge Weight of the desired spanning tree.
V <= 50000, e <= 100000, and all data edge weights are positive integers in [1,100.
Sample Input
2 2 1
0 1 1 1
0 1 2 0
Sample output
2
Run the Minimum Spanning Tree first and find that the number of white edges selected is different from that of need.
It is right to move the white edge up or down
Binary Offset Check white edge selection
* White edges are preferred.
# Include <iostream> # include <cstdio> # include <algorithm> using namespace STD; int I, m, n, J, K, need, L =-150, R = 150, TMP, F [100001]; struct VV {int X, Y, Z, C;} A [1000001]; bool CMP (VV a, vv B) {return. z = B. z? A. c <B. c:. z <B. z;} int find (int x) {If (F [x] = x) return X; F [x] = find (F [x]); return f [X];} int check (int x) {int ans = 0; k = 0; For (INT I = 1; I <= m; I ++) if (! A [I]. c) A [I]. Z + = x; For (INT I = 0; I <= N; I ++) f [I] = I; sort (a + 1, A + 1 + m, CMP); For (INT I = 1; I <= m; I ++) {If (find (A [I]. x )! = Find (A [I]. y) {k + = A [I]. Z; If (! A [I]. c) ans + = 1; F [f [A [I]. x] = f [A [I]. y] ;}}for (INT I = 1; I <= m; I ++) if (! A [I]. c) A [I]. z-= x; return ans;} int main () {scanf ("% d", & N, & M, & need ); for (I = 1; I <= m; I ++) scanf ("% d", & A [I]. x, & A [I]. y, & A [I]. z, & A [I]. c); While (L <= r) {int mid = (L + r)> 1; if (check (MID)> = need) TMP = mid, L = Mid + 1; else r = mid-1;} Check (TMP); printf ("% d", K-TMP * Need );}
p2619 [National Training Team 2] tree I