Problem
Given an undirected weighted graph G, you should find one of spanning trees specified as follows.
The graph G is an ordered pair (V, E), where V is a set of vertices {v1, v2, …, vn} and E is a set of undirected edges {e1, e2, …, em}. Each edge e ∈ E has its weight w(e).
A spanning tree T is a tree (a connected subgraph without cycles) which connects all the n vertices with n − 1 edges. The slimness of a spanning tree T is defined as the difference between the largest weight and the smallest weight among the n − 1 edges of T.
Figure 5: A graph G and the weights of the edges
For example, a graph G in Figure 5(a) has four vertices {v1, v2, v3, v4} and five undirected edges {e1, e2, e3, e4, e5}. The weights of the edges are w(e1) = 3, w(e2) = 5, w(e3) = 6, w(e4) = 6, w(e5) = 7 as shown in Figure 5(b).
Figure 6: Examples of the spanning trees of G
There are several spanning trees for G. Four of them are depicted in Figure 6(a)~(d). The spanning tree Ta in Figure 6(a) has three edges whose weights are 3, 6 and 7. The largest weight is 7 and the smallest weight is 3 so that the slimness of the tree Ta is 4. The slimnesses of spanning trees Tb, Tc and Td shown in Figure 6(b), © and (d) are 3, 2 and 1, respectively. You can easily see the slimness of any other spanning tree is greater than or equal to 1, thus the spanning tree Td in Figure 6(d) is one of the slimmest spanning trees whose slimness is 1.
Your job is to write a program that computes the smallest slimness.
题目大意:求最大边和最小边差值最小的生成树。
Solution
对于这道题,我们可以枚举每一个生成树的最小边,再去寻找最大边。
再枚举完最小边时,此时我们希望最大边最小,自然就转化为了求以当前边为最小边所形成的瓶颈生成树的最大边。
而我们知道,最小生成树一定是瓶颈生成树(证明),所以只需要求一个最小生成树即可。
在求最小生成树的时候,显然使用Kruscal来做的;其基于边的算法十分适合这题的枚举。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
#include<cmath>
#include<limits.h>
#include<algorithm>
#define Read(a) scanf("%d",&a)
#define Readln(a,b) scanf("%d%d",&a,&b)
#define write(a) printf("%d",a)
#define writeln(a) printf("%d\n",a)
#define Mp make_pair
#define Mem(a,b) memset(a,b,sizeof(a))
#define father for(int i=0;i<=n;++i)fa[i]=i
using namespace std;
int n,m,x,y,v;
int fa[100000];
int ans=INT_MAX;
struct edge
{
int u;
int v;
int val;
};
edge a[100000];
bool cmp(edge p1,edge p2) {
return p1.val<p2.val;
}
int get(int x)
{
if (fa[x] == x) return x;
else return fa[x]=get(fa[x]);
}
bool check()
{
int num=get(1);
for (int i=2;i<=n;++i)
if (get(i)!=num)
return false;
return true;
}
void work()
{
int tot=0;
Mem(a,0);
ans=INT_MAX;
for (int i=1;i<=m;++i)
{
Read(x),Read(y),Read(v);
a[++tot]=edge{x,y,v};
}
sort(a+1,a+tot+1,cmp);
father;
for (int _=1;_<=tot;++_)
{
father;
bool flag=0;
int sum=0,cnt=0;
int Max=-INT_MAX,Min=INT_MAX;
for (int i=_;i<=tot;++i)
{
int fu=get(a[i].u);
int fv=get(a[i].v);
int val=a[i].val;
if (fu == fv) continue;
Min=min(Min,val);
Max=max(Max,val);
fa[fu]=fv;
if (++cnt == n-1) { flag=1; break; }
}
if (check() == 1 && flag) ans=min(ans,Max-Min);
}
if (ans == INT_MAX) printf("-1\n");
else writeln(ans);
return;
}
int main(void)
{
while (Readln(n,m) && (n || m)) work();
return 0;
}