G - 最小生成树

POJ - 3522

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

n m  
a1 b1 w1
   
am bm wm

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

emmm 题目意思就是枚举最小边,求最小生成树的边的最小距离

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{int x,y;int val;
}s[10500];
int pre[105];
bool cmp(node a,node b)
{return a.val<b.val;
}
int find(int x){if(x==pre[x]) return x;else return pre[x] = find(pre[x]);
}
bool merge(int x,int y)
{int fx = find(x);int fy = find(y);if(fx!=fy){pre[fx]=fy;return true;}return false;
}
int main()
{int n,m;while(scanf("%d%d",&n,&m)!=EOF){if(n==0&&m==0)break;for(int i=1;i<=m;i++)scanf("%d %d %d",&s[i].x,&s[i].y,&s[i].val);sort(s+1,s+1+m,cmp);//从小到大排个序列//枚举当这条边是最小边的时候int dur = 0x3f3f3f3f;for(int i = 1;i<=m;i++){int minn = s[i].val,maxn = 0;int now = 0;for(int k =1;k<=n;k++)pre[k] = k;for(int j = i;j<=m;j++){if(merge(s[j].x,s[j].y)){maxn = max(maxn,s[j].val);++now;}}if(now>=n-1){dur = min(dur,maxn-minn);    }   }if(dur==0x3f3f3f3f)printf("-1\n");else printf("%d\n",dur);   }return 0;
} 

最小生成树 POJ - 3522(枚举边最小生成树)相关推荐

  1. poj 3522(最小生成树应用)

    题目链接:http://poj.org/problem?id=3522思路:题目要求最小生成树中最大边与最小边的最小差值,由于数据不是很大,我们可以枚举最小生成树的最小边,然后kruskal求最小生成 ...

  2. POJ 3522 Slim Span (Kruskal枚举最小边)

    题意: 求出最小生成树中最大边与最小边差距的最小值. 分析: 排序,枚举最小边, 用最小边构造最小生成树, 没法构造了就退出 1 #include <stdio.h> 2 #include ...

  3. poj 1789 Truck History(最小生成树 prim)

    题目:http://poj.org/problem?id=1789 大意:每个卡车都有自己的编号,由七位字母组成 d(to,td) is the distance of the types指t0 和 ...

  4. poj 3026 Borg Maze (最小生成树+bfs)

    有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...

  5. POJ 1258 Agri-Net (最小生成树)

    题目: Description Farmer John has been elected mayor of his town! One of his campaign promises was to ...

  6. POJ 2485 Highways(最小生成树 Prim)

    Highways   大意:给你一个用邻接矩阵形式存储的有n个顶点的无向图,让你求它的最小生成树并求出在这个生成树里面最大的边的权值. 思路:用Prim求,判断条件改一下就行. PS:dis数组初始化 ...

  7. POJ 2485 Highways (prim最小生成树)

    对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...

  8. 【SHOI2010】最小生成树(最小割,最小生成树)

    初看题目,发现题目的操作比较复杂.仔细想了一想,发现题目中的操作"把图中除了这条边以外的边,每一条的权值都减少 1 1 1"就等价于"把这条边的权值加 1 1 1&quo ...

  9. ds图—最小生成树_Java: Kruskal算法生成最小生成树(邻接矩阵)

    Java: Kruskal算法生成最小生成树(邻接矩阵): package 输出: Kruskal=36: (E,F) (C,D) (D,E) (B,F) (E,G) (A,B) 分析: Java: ...

  10. 最小生成树实验报告c语言,最小生成树(C语言, prim算法)

    图(来源:<>p250) #include #include #include /* * 邻接矩阵, prim普里姆算法(属贪婪算法),无向图,最小生成树 * 代码实现<>p2 ...

最新文章

  1. SAP Fiori Elements save按钮的实现细节
  2. python ioc di_Sping(一)——IOC/DI
  3. [js] 使用delete删除数组,其长度会改变吗
  4. 大学计算机基础书本里的毕业论文源稿,计算机基础毕业论文范文
  5. 计算机打印中 进纸盘2,软件、计算机和打印机接口问题-HP.PDF
  6. BZOJ 2466 [中山市选2009]树(高斯消元)
  7. 中国好声音不如有中国好创意
  8. 计算机网络(第七版)谢希仁
  9. VS社区版离线试用到期解决办法
  10. ros中odometry数据生成方式与分发去向
  11. HTML 的属性 lang=“en“ 语言设置为中文
  12. java获取excel行数_Java如何利用POI读取Excel行数
  13. 宋浩《概率论与数理统计》自用笔记
  14. redux的原理、工作流程及其应用
  15. android shell卸载应用程序,android系统软件卸载_adb配置使用
  16. 第五周综述(商业智能之数据处理)
  17. kafka SASL认证介绍及自定义SASL PLAIN认证功能
  18. P1796 汤姆斯的天堂梦(动态规划)
  19. 国标、行标、地标、团标、企标
  20. 网站UI设计应具有的8大品质和特点--摘自《众妙之门--网站UI设计之道》

热门文章

  1. Spring Cloud构建微服务架构(四)分布式配置中心(续)
  2. java多线程 信号量(Semaphore),死锁
  3. Python面向对象成员修饰符
  4. Apache Shiro学习笔记(七)IniWebEnvironment
  5. Spring的p标签
  6. myeclipse 8.5安装freemarker插件方法
  7. zookeeper的zxid
  8. 死锁的产生原因和解决办法
  9. java创建一个单链表,接受输入的数据,并输出
  10. gridview求和