题意:
求出最小生成树中最大边与最小边差距的最小值。
分析:
排序,枚举最小边, 用最小边构造最小生成树, 没法构造了就退出
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <cmath>
#include <iomanip>
#define rep(i,a,b) for(int i = a; i < b;i++)
#define _rep(i,a,b) for(int i = a; i <= b;i++)
using namespace std;
const int inf = 1e9 + ;
const int maxn = + ;
int n , m, cnt;
struct edge{
int u, to , d;
bool operator < (const edge& a) const {
return d < a.d;
}
}G[maxn * maxn];
int f[maxn];
void init(){
_rep(i,,n) f[i] = i;
}
int get(int x){
if(f[x] == x){
return x;
}else{
f[x] = get(f[x]);
return f[x];
}
}
void merge(int a, int b){
int t1 = get(a), t2 = get(b);
if(t1 != t2){
f[t2] = t1;
}
}
int Kruskal(int st){
int picked = ;
int min_d = inf, max_d = -inf;
rep(i,st,m){
int u = G[i].u, v = G[i].to, d = G[i].d;
if(get(u) != get(v)){
merge(u,v);
picked++;
min_d = min(min_d, d), max_d = max(max_d , d);
}
if(picked == n - ) return max_d - min_d;//已经构造出最小生成树, 返回结果
}
return -;//已经无法构造生成树了, 结束枚举
}
int main(){
// freopen("1.txt","r", stdin);
while(cin >> n >> m && n){
cnt = ;
rep(i,,m){
int u , v , d;
cin >> u >> v >> d;
G[cnt].u = u , G[cnt].to = v, G[cnt].d = d;
cnt++;
}
sort(G, G + cnt);
int ans = inf;
for(int i = ; i < m - n + ; i++){ //由于至少要n-1条边, 所以枚举到m - n + 1
init();
int t = Kruskal(i);
if(t != -){
ans = min(ans, t);
}else break;
}
printf("%d\n", ans == inf ? - : ans);
}
}