UVA 1395 Slim Span

Source: Internet
Author: User

Test instructions

What is required is the smallest of the largest and smallest edge differences in all spanning trees.

Analysis:

You can actually use the minimum bottleneck to generate the tree, which is the nature of the minimum spanning tree, enumerate the smallest edges of the original, and then find the maximum edge of the corresponding spanning tree.

Code:

 

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<vector>UsingNamespace Std;Constint MAXN=110;Constint INF=10000000000;struct Edge{int xY;int D;booloperator< (ConstEdge &a)Const{Return D<a. D;}}e[MAXN*maxn];int FA[MAXN];int nM;int Maxt;IntFind(int x){Return FA[x]==x? x: FA[x]=Find(FA[x]);}IntKruskal(int x){Maxt=-1;int cnt=0;int I;For(I=0; I<=n; I+ +) FA[I]=i;For(I=x; I<m; I++){int PA=e[I].x;int PB=e[I].y;int D=e[I].d;int X=Find(PA);int Y=Find(PB);If(X!=y){FA[X]=y; Maxt=Max(d, Maxt);If(++cnt==n-1)Return1;}}Return0;}IntMain(){While(scanf("%d%d", &n, &m)){If(n==0&&m==0)Break;int I;For(I=0; I<m; I++)scanf("%d%d%d", &e[I].x, &e[I].y, &e[I].d);Sort(EE+m);int ans=inf;For(I=0; I<m; I++){If(Kruskal(I)==1){ans=Min(ans, maxt-e[i].d); } } if(ans==inf) printf("-1\ n"); Else printf("%d\ n", ans); }}< /c9> 

  

UVA 1395 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.