Test instructions
A spanning tree is defined as the difference between the maximum weight and the minimum weight. A graph of the N-vertex m-edge is given, and a spanning tree with the smallest degree of thinness is obtained.
Analysis:
By ordering the weights of the edges, enumerate the left edge of the contiguous interval [L, R] of the edge set, and if these intervals meet exactly one spanning tree, there is a spanning tree with a slender degree not exceeding w[r]-w[l].
In other words, the code uses the STL vector, a lot slower.
1#include <cstdio>2#include <vector>3#include <algorithm>4 using namespacestd;5 6 Const intINF =1000000000;7 Const intMAXN = -+Ten;8 intPA[MAXN];9 Ten intFindset (intx) {returnx = = Pa[x]? X:PA[X] =Findset (pa[x]);} One A structEdge - { - intu, V, W; theEdge (intUintVintW): U (U), V (v), W (w) {} - BOOL operator< (Constedge& RHS)Const - { - returnW <RHS.W; + } - }; + AVector<edge>G; at - intMain () - { - //freopen ("In.txt", "R", stdin); - intN, M; - while(SCANF ("%d%d", &n, &m) = =2&&N) in { - g.clear (); to intu, V, W; + for(inti =0; I < m; ++i) - { thescanf"%d%d%d", &u, &v, &W); * G.push_back (Edge (U, V, W)); $ }Panax Notoginseng sort (G.begin (), G.end ()); - intAns =INF; the for(intL =0; L <= M-n +1; ++L) + { A for(inti =1; I <= N; ++i) Pa[i] =i; the intCNT =N; + for(intR = L; R < M; ++R) - { $ intU = Findset (g[r].u), V =Findset (G[R].V); $ if(U! =v) - { -Pa[u] =v; the if(--cnt = =1) {ans = min (ans, g[r].w-G[L].W); } - }Wuyi } the } - if(ans = = INF) ans =-1; Wu -printf"%d\n", ans); About } $ - return 0; -}code June
UVa 1395 (minimum spanning tree) Slim Span