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