UVA1395 Slim Span(枚举最小生成树)

题意: 求最小生成树中,最大的边减去最小的边 最小值。

看了题解发现真简单=_=

将每条边进行从小到大排序,然后从最小到大一次枚举最小生成树,当构成生成树的时候,更新最小值

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int INF = 0x3f3f3f3f;
 7 const int Max = 5000;
 8 const int N = 110;
 9 int father[N];
10 struct Edge
11 {
12     int x, y, dist;
13 };
14 Edge edge[Max];
15 int cmp(Edge t1, Edge t2)
16 {
17     return t1.dist < t2.dist;
18 }
19 int find_father(int x)
20 {
21     if (x == father[x])
22         return x;
23     return father[x] = find_father(father[x]);
24 }
25 int main()
26 {
27     int n, m;
28     while (scanf("%d%d", &n, &m) != EOF)
29     {
30         if (m == 0 && n == 0)
31             break;
32         for (int i = 0; i < m; i++)
33         {
34             scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].dist);
35         }
36         sort(edge, edge + m, cmp);
37         int minn = INF;
38         for (int i = 0; i < m; i++)
39         {
40             for (int j = 0; j <= n; j++)
41                 father[j] = j;
42             int cnt = 0;
43             for (int j = i; j < m; j++)  // 依次枚举每一个生成树
44             {
45                 int fx = find_father(edge[j].x);
46                 int fy = find_father(edge[j].y);
47                 if (fx != fy)
48                 {
49                     father[fx] = fy;
50                     cnt++;
51                     if (cnt == n - 1) // 一旦构成生成树,j是最大边权,i是最小权,相减更新
52                     {
53                         minn = min(minn, edge[j].dist - edge[i].dist);
54                         break;
55                     }
56                 }
57             }
58         }
59         if (minn == INF)
60             printf("-1\n");
61         else
62             printf("%d\n", minn);
63     }
64     return 0;
65 }

时间: 2024-10-06 20:59:35

UVA1395 Slim Span(枚举最小生成树)的相关文章

uva1395 - Slim Span(最小生成树)

先判断是不是连通图,不是就输出-1. 否则,把边排序,从最小的边开始枚举最小生成树里的最短边,对每个最短边用Kruskal算法找出最大边. 或者也可以不先判断连通图,而是在枚举之后如果ans还是INF,说明就没有,就输出-1. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath>

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 

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,.

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 

UVA 1359 POJ 3522 Slim Span(最小生成树kruskal)

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 

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

UVa 1395 Slim Span (最小生成树)

题意:给定n个结点的图,求最大边的权值减去最小边的权值最小的生成树. 析:这个和最小生成树差不多,从小到大枚举左端点,对于每一个左端点,再枚举右端点,不断更新最小值.挺简单的一个题. #include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 100 + 5; const int INF = 0x3f3f3f3f; int p[maxn]

uva 1395 - Slim Span poj 3522 Slim Span(最小生成树算法)

最近学习了一下 最小生成树 算法. 所谓最小生成树算法,就是给出一个连通图g[ maxn ][ maxn  ], 找出这个连通图的边权和最小的生成图(树). 可以实现这个目的的算法,我叫它最小生成树算法.kruskal算法就是我学到的一种实现这种功能的算法. 对于kruskal算法的描述以及简单的证明在刘汝佳第二版上已经说得够明白 本题就是求 最小生成树 里面的 最大边权和最小边权 相差最小的最小生成树. #include<cstdio> #include<cstring> #in