每日一题(12)——计算Slim Span(并查集)

来源:互联网 发布:网易广州 知乎 编辑:程序博客网 时间:2024/06/03 17:20

首先总结一下并查集:

其中按秩合并分为:按树的深度和树的大小合并,效果都相同。

 

 

 

 

 

问题ID:POJ3522

Slim Span
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 5098 Accepted: 2671

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} andE is a set of undirected edges {e1,e2, …,em}. Each edgeeE has its weightw(e).

A spanning tree T is a tree (a connected subgraph without cycles) which connects all the n vertices withn − 1 edges. The slimness of a spanning treeT is defined as the difference between the largest weight and the smallest weight among then − 1 edges ofT.


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 arew(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 treeTa 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 treeTa is 4. The slimnesses of spanning treesTb,Tc andTd 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.

nm a1b1w1 ⋮ ambmwm

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 ≤mn(n − 1)/2.ak andbk (k = 1, …,m) are positive integers less than or equal ton, which represent the two verticesvak andvbk connected by thekth edgeek.wk is a positive integer less than or equal to 10000, which indicates the weight ofek. You can assume that the graphG = (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 51 2 31 3 51 4 62 4 63 4 74 61 2 101 3 1001 4 902 3 202 4 803 4 402 11 2 13 03 11 2 13 31 2 22 3 51 3 65 101 2 1101 3 1201 4 1301 5 1202 3 1102 4 1202 5 1303 4 1203 5 1104 5 1205 101 2 93841 3 8871 4 27781 5 69162 3 77942 4 83362 5 53873 4 4933 5 66504 5 14225 81 2 12 3 1003 4 1004 5 1001 5 502 5 503 5 504 1 1500 0

Sample Output

1200-1-110168650

 

 

 

此题对kruskal算法做了变形,不是求最小生成树,而是求最大边权值与最小边权值之差最小的生成树,同样可以用kruskal算法的实现方法,采用并查集。如果求最小生成树要将边加入到堆中,并且不需要遍历所有的生成树情况

//此题对kruskal算法做了变形,不是求最小生成树//而是求最大边权值与最小边权值之差最小的生成树//同样可以用kruskal算法的实现方法,采用并查集#include <iostream>#include <stdlib.h>#include <algorithm>#define inf 10000000//边权值的上界;using namespace std;int m,n;//p数组用于记录父节点,r数组用于统计以r为根的子//树中间有多少个点,其实r数组可以取消,因为//直接由根节点的绝对值就可以这个集合中的元素个数;int p[105];int r[105];struct edge{int u;int v;int w;}e[5000];bool cmp(const edge & e1,const edge & e2){return e1.w < e2.w;}void set_init(){//并查集初始化;for (int i = 1;i <= n;i++){p[i] = i;r[i] = 1;//初始状态每个的根是自己,集合个数为1}}// int set_find(int x){//查找包含x的集合树的根// while (p[x] >= 0)// {// x = p[x];// }// return x;// }//以上为非递归实现,递归实现如下int set_find(int x){if(p[x] == x) return x;else  p[x] = set_find(p[x]);return p[x];}// void set_union(int root1,int root2){//将两个集合合并// p[root1]+=p[root2];// p[root2] = root1;//用根root2连接到root1下面// }//为防止树的退化,改进版的union操作如下void set_union(int x,int y){//将两个集合合并if(r[x] <= r[y]){p[x] = y;r[y]+=r[x];//y作为根}else {p[y] = x;r[x]+=r[y];}}int main(){int i,j,k,ans,px,py,a,b,temp;while (cin>>n>>m){if (n == 0 && m == 0)break;for (i = 0;i < m;i++){cin>>e[i].u>>e[i].v>>e[i].w;}if ( n == 2 && m == 1){cout<<"0"<<endl;//2点1边的情况单独处理continue;}sort(e,e+m,cmp);ans = inf;for (i = 0; i<m;i++){k = 0;set_init();for (j = i;j<m;j++){px = set_find(e[j].u);py = set_find(e[j].v);if (px != py){set_union(px,py);//注意这里只是求生成树,不一定是mst,只要不在同一个集合中就可以选这条边,两个端点变成同一个集合内k++;if(k == 1){//这是最小的边a = e[j].w;}else if (k == n-1)//这是最大的边也是最后一条边{b = e[j].w;temp = b - a;if (temp < ans){ans = temp;}break;}}}}if (ans == inf){cout<<"-1"<<endl;}else cout<<ans<<endl;}return 0;}


 

 

 

原创粉丝点击