Slim Span
Description 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), (c) 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. Input The input consists of multiple datasets, followed by a line containing two zeros separated by a space. Each dataset has the following format.
Every input item in a dataset is a non-negative integer. Items in a line are separated by a space. n is the number of the vertices and m the number of the edges. You can assume 2 ≤ n ≤ 100 and 0 ≤ m ≤ n(n − 1)/2.ak and bk (k = 1, …, m) are positive integers less than or equal to n, which represent the two vertices vak and vbk connected by the kth edge ek. wk is a positive integer less than or equal to 10000, which indicates the weight of ek. You can assume that the graph G = (V, E) is simple, that is, there are no self-loops (that connect the same vertex) nor parallel edges (that are two or more edges whose both ends are the same two vertices). Output For each dataset, if the graph has spanning trees, the smallest slimness among them should be printed. Otherwise, −1 should be printed. An output should not contain extra characters. Sample Input 4 5 1 2 3 1 3 5 1 4 6 2 4 6 3 4 7 4 6 1 2 10 1 3 100 1 4 90 2 3 20 2 4 80 3 4 40 2 1 1 2 1 3 0 3 1 1 2 1 3 3 1 2 2 2 3 5 1 3 6 5 10 1 2 110 1 3 120 1 4 130 1 5 120 2 3 110 2 4 120 2 5 130 3 4 120 3 5 110 4 5 120 5 10 1 2 9384 1 3 887 1 4 2778 1 5 6916 2 3 7794 2 4 8336 2 5 5387 3 4 493 3 5 6650 4 5 1422 5 8 1 2 1 2 3 100 3 4 100 4 5 100 1 5 50 2 5 50 3 5 50 4 1 150 0 0 Sample Output 1 20 0 -1 -1 1 0 1686 50 Source |
这道题简单的来说就是求一棵生成树使最大的边和最小的边差值最小。
换个角度想就是用n-1条(n个点)数值相差不多的边,组成一棵生成树。
在生成树的prim和kruskal两个算法中很容易就会觉得kruskal的贪心思想会更加适合这道题。
kruskal算法一开始会对边进行排序,然后枚举最小的边。
比如第一个样例:
4 5
1 2 3 (1)
1 3 5 (2)
1 4 6 (3)
2 4 6 (4)
3 4 7 (5)
因为样例已经按边长排好序
第一次kruskal从(1)边开始直到组成一颗生成树,求出最大最小边差值。
第二次把(1)边扔掉,kruskal从(2)边开始直到组成一颗生成树,再求出最大最小边差值。
以此类推。(注意不可能剩下的边比n-1小)
题意:求最大边与最小边差值最小的生成树
解题分析:
最小生成树有一个很重要的性质:在构造生成树时有可能选择不同的边,但最小生成树的权是唯一的!所以在用kruskal算法时第一次加入的必然是最小生成树的最小边权值,最小边确定后,最小生成树的最大边的权值是所以生成树中最小的,于是只要枚举最小边,然后求最小生成树,就可以得到最大边,只要每次更新最优解就行了。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int VM=110; const int INF=0x3f3f3f3f; struct Edge{ int u,v; int cap; }edge[VM*VM]; int n,m,ans,father[VM]; void makeSet(){ for(int i=1;i<=n;i++) father[i]=i; } int findSet(int x){ if(x!=father[x]){ father[x]=findSet(father[x]); } return father[x]; } int cmp(Edge a,Edge b){ return a.cap<b.cap; } void Kruskal(){ sort(edge,edge+m,cmp); for(int i=0;i<m;i++){ makeSet(); int cnt=0,tmp=INF; for(int j=i;j<m;j++){ int fx=findSet(edge[j].u); int fy=findSet(edge[j].v); if(fx!=fy){ father[fy]=fx; cnt++; if(cnt==n-1){ tmp=edge[j].cap-edge[i].cap; break; } } } if(tmp<ans) ans=tmp; } } int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){ if(n==0 && m==0) break; for(int i=0;i<m;i++) scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].cap); ans=INF; Kruskal(); if(ans==INF) ans=-1; printf("%d\n",ans); } return 0; }