Test instructions: Give a graph, find a spanning tree, it satisfies: maximum weight-minimum weight = minimum. Simple graphs, not necessarily connected, may be all the same weights.
Idea: The number of points is small. According to kruscal each pick is the minimum weight of the edge, then the slender degree must also be minimal. But spanning tree has many trees, slender nature also has many, the poor lift all spanning tree, knew the result. According to "as long as the starting edge is different, the spanning tree must be different" to lift the starting edge.
And found a possible pit!! I think Long_max is the positive maximum value of int, that is, 2147483647=2^31-1, in my machine may be so, in OJ not necessarily, with Long_max to int will be different, pay attention to.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intn= -+5;4 Const intinf=0x7f7f7f7f;5 intG[n][n];6 intN, M;7 intPre[n];8vector< pair<int,int> >Vect;9InlineintCMP (pair<int,int> a,pair<int,int>b)Ten { One returnG[a.first][a.second]<g[b.first][b.second]?true:false; A } - - intFindintx) the { - returnPre[x]==x? x:pre[x]=find (Pre[x]); - } - voidJointintAintb) + { -A=find (a); +b=find (b); A if(a!=b) pre[a]=b; at } - - - intKruscal (inti) - { - intq=Vect[i].first; in intp=Vect[i].second; - intCnt=0; to for(intI=0; i<=n; i++) pre[i]=i; + - for(intJ=i; j<m; J + +) the { * intA=Vect[j].first; $ intb=Vect[j].second;Panax Notoginseng if(Find (a)! =find (b)) - { thecnt++; + if(cnt==n-1)returng[a][b]-G[q][p]; A Joint (A, b); the } + } - returnINF; $ } $ - intcal () - { the if(m<n-1|| Kruscal (0) ==inf)return-1;//Not Connected - Wuyi sort (Vect.begin (), Vect.end (), CMP); the intans=INF; - for(intI=0; i<m; i++) Wu { - intq=kruscal (i); About if(Q==inf)Continue; $ans=min (ans,q); - } - returnans; - } A + intMain () the { - //freopen ("Input.txt", "R", stdin); $ intA, B, W; the while(SCANF ("%d%d", &n,&m), n+m) the { theMemset (G,0,sizeof(g)); the vect.clear (); - for(intI=0; i<m; i++) in { thescanf"%d%d%d",&a,&b,&W); theg[a][b]=g[b][a]=W; About Vect.push_back (Make_pair (B,a)); the } theCout<<cal () <<Endl; the } + return 0; -}AC Code
UVA 1395 Slim Span (minimum spanning tree, mst,kruscal)