UVa 1395 (minimum spanning tree) Slim Span

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.