【kruscal】【最小生成树】poj3522 Slim Span

求一个生成树,使得最大边权和最小边权之差最小。由于数据太小,暴力枚举下界,求出相应的上界。最后取min即可。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 int n,m,fa[101],rank[101];
 6 void clear(){for(int i=1;i<=n;i++) fa[i]=i; memset(rank,0,sizeof(rank));}
 7 int findroot(int x)
 8 {
 9     if(fa[x]==x) return x;
10     int rt=findroot(fa[x]);
11     fa[x]=rt;
12     return rt;
13 }
14 void Union(int U,int V)
15 {
16     if(rank[U]<rank[V]) fa[U]=V;
17     else
18       {
19         fa[V]=U;
20         if(rank[U]==rank[V]) rank[U]++;
21       }
22 }
23 struct Edge{int u,v,w;};
24 bool cmp(const Edge &a,const Edge &b){return a.w<b.w;}
25 Edge edges[5001];
26 int tot,ans,maxv;
27 int main()
28 {
29     while(1)
30       {
31           scanf("%d%d",&n,&m);
32           if(!n) break;
33           for(int i=1;i<=m;i++) scanf("%d%d%d",&edges[i].u,&edges[i].v,&edges[i].w);
34           sort(edges+1,edges+m+1,cmp); ans=2147483647;
35           for(int j=1;j<=m;j++)
36             {
37                 tot=0; clear();
38                 for(int i=j;i<=m;i++)
39                 {
40                     int f1=findroot(edges[i].u),f2=findroot(edges[i].v);
41                     if(f1!=f2) {Union(f1,f2); tot++; if(tot==n-1) {maxv=edges[i].w; goto WIN;}}
42                 }
43               continue;
44               WIN:ans=min(ans,maxv-edges[j].w);
45             }
46           printf("%d\n",ans!=2147483647 ? ans : -1);
47       }
48     return 0;
49 }
时间: 2024-08-01 21:38:20

【kruscal】【最小生成树】poj3522 Slim Span的相关文章

POJ-3522 Slim Span(最小生成树)

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8633   Accepted: 4608 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 

POJ3522 Slim Span

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7462   Accepted: 3959 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 

Uva1395 POJ3522 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

POJ 3522 ——Slim Span——————【最小生成树、最大边与最小边最小】

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7102   Accepted: 3761 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 

POJ 3522 Slim Span【枚举+克鲁斯卡尔求最小生成树】

Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7365 Accepted: 3909 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

[2016-01-27][UVA][1395][D -?Slim Span]

[2016-01-27][UVA][1395][D - Slim Span] 时间:2016-01-21  11:06:30  星期四 题目编号:UVA 1395 题目大意:求所有生成树 最大边和最小边之差的最小值 分析: 想法是枚举所有边,但是分析之后发现可以不用枚举所有的树 已知krukal得到的最小生成树得到的最小生成树,最大边最小 也就是说,这个最大边,就是使得 最大边和最小边 差值最大的边 那么,只需要求每条边的对应的差值最小的最大边即可 方法: 从最小的边开始跑生成树,然后跑krus

UVA 1395 - Slim Span(MST)

UVA 1395 - Slim Span 题目链接 题意:给定一些结点和边,要求出最苗条度最小的生成树,苗条度定义为:生成树中最大权的边减去最小权的边的值 思路:类似建最小生成树的算法,多一步枚举起始边即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 105; const int INF = 0x3f3f3f3f; int

UVA1395 Slim Span(kruskal)

题目:Slim Span UVA 1395 题意:给出一副无向有权图,求生成树中最小的苗条度(最大权值减最小权值),如果不能生成树,就输出-1: 思路:将所有的边按权值有小到大排序,然后枚举每一条边,以这条边开始利用Kruskal算法生成树,生成过程中求出权值的最大值,这个最大值减去当前枚举的边的权值就是苗条度,再动态维护一下最小苗条度就可以了. #include <iostream> #include <algorithm> #include <queue> #inc

UVA1395 Slim Span(kruskal算法)

Slim Span [PDF Link] 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,.