POJ 3522 Slim Span(MST)

2024年下半年网络规划设计师论文真题 3、深入论述自身参与的工业网络是如何实施的,为实现制造业的智能化,如何实现数据的跨系统互通,促进各种工业数据的充分流动和无缝集成以及数据安全。3、结合自身参与的网络建设,详细叙述针对该网络“去NAT”的规划和设计方案。4、在实施过程中遇到的问题以及如何处理,项目实施后的效果和评价。2、大型园区网络“去NAT”改造和业务融合的整体架构。1、“网络去NAT”对推进ipv6演进和应用的意义。4、改造过程中遇到的问题及解决方案。2、简要叙述自身参与的制造网络建设。1、网络规划在智能制造中的意义。 阅读详情
Slim Span
Time Limit:  5000MS
Memory Limit: 65536K

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.

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 ≤ m ≤ n(n − 1)/2. ak andbk (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 ofek. 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

思路:枚举+并查集

完整代码:
/*79ms,208KB*/

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 101;
const int M = 5001;
const int INF = 0x7fffffff;

int fa[N], rank[N], n, m;

struct edge
{
	int a;
	int b;
	int w;
} edges[M];

bool cmp(const edge a, const edge b)
{
	return a.w < b.w;
}

int find_set(int n)
{
	if (fa[n] == n)
		return n;
	fa[n] = find_set(fa[n]);
	return fa[n];
}

bool union_set(int a, int b)
{
	int ra = find_set(a);
	int rb = find_set(b);
	if (ra != rb)
	{
		if (rank[ra] >= rank[rb])
		{
			fa[rb] = ra;
			rank[ra] += rank[rb];
		}
		else
		{
			fa[ra] = rb;
			rank[rb] += rank[ra];
		}
		return true;
	}
	return false;
}

int main()
{
	int i, j;
	int num;
	int min;
	int fedge, ledge;
	while (scanf("%d%d", &n, &m), n)
	{
		min = INF;
		bool flag = false;
		for (i = 1; i <= m; i++)
			scanf("%d%d%d", &edges[i].a, &edges[i].b, &edges[i].w);
		sort(edges + 1, edges + m + 1, cmp);
		if (n == 2 && m == 1)
		{
			printf("0\n");
			continue;
		}
		for (j = 1; j <= m; j++)
		{
			num = 0;
			for (i = 1; i <= n; i++)
			{
				fa[i] = i;
				rank[i] = 1;
			}
			for (i = j; i <= m; i++)
			{
				if (j + n - 2 > m)
				{
					flag = true;
					break;
				}
				if (union_set(edges[i].a, edges[i].b))
				{
				    //for (i = 1; i <= n; i++)
                    //{
                        //printf("%d %d\n",fa[i],rank[i]);
                    //}
					num++;
					if (num == 1)
						fedge = edges[i].w;
					else if (num == n - 1)
					{
						ledge = edges[i].w;
						if (ledge - fedge < min)
							min = ledge - fedge;
						break;
					}
				}
			}
			//printf("\n");
			if (flag)
				break;
		}
		printf("%d\n", min != INF ? min : -1);
	}
	return 0;
}


基于深度学习的水质预测:多参数监测与预测 在本篇博客中,我们详细介绍了如何使用深度学习技术处理水质预测问题,包括数据集的准备与预处理、多层感知机模型的构建、模型的训练与评估以及模型优化策略。我们使用 Python 语言基于 TensorFlow 和 Keras 框架实现了模型,并给出了各种模型优化方法及对应代码。通过实践,我们可以发现深度学习技术在水质预测方面具有很好的应用潜力。在实际应用中,我们还可以根据具体问题和数据集进一步调整和优化模型,以获得更好的预测性能。 阅读详情

相关推荐

Win11网络卡顿?用Wireshark抓包5分钟定位问题(保姆级实战)

本文详细介绍了如何使用Wireshark在Win11系统中快速定位网络卡顿问题。通过5分钟抓包分析,作者成功发现TCP重传率高达37%的罪魁祸首,并提供了从准备工作到实战分析的保姆级教程,帮助用户解决游戏卡顿、视频会议延迟等常见网络问题。

weixin_28839629的博客 306

图

翻译可参考:https://www.jianshu.com/p/bce71b2bdbc8 Graph A graph looks like the following picture: In computer science, a graph is defined as a set of vertices paired with a set of edges. The vertices are represented by circles, and the edges are the lines betw

老照片 317

人形机器人骨骼材料选型实战:从铝合金到碳纤维的5种方案对比

本文深入探讨人形机器人骨骼材料的选型策略,对比铝合金、镁合金、钛合金、碳纤维复合材料及3D打印材料等5种方案的性能优劣。通过建立强度-重量比、疲劳寿命等核心评价维度,结合具体应用场景,提供科学的材料决策方法论,助力开发者优化机器人结构设计,实现性能与成本的平衡。

weixin_29199873的博客 436

POJ3522:Slim Span

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4023   Accepted: 2116 Description Given an undirected weighted graph G, you should

第七殿堂 524

华为海思2026届数字IC秋招笔试真题解析与实战技巧(2025最新版)

本文深度解析华为海思2026届数字IC秋招笔试真题,涵盖高频考点如后端设计、低功耗设计、SystemVerilog语法等核心内容。提供时序违例修复、时钟树综合等实战技巧,帮助考生高效备考。特别关注UPF低功耗设计规范等最新考点,助力考生在秋招笔试中脱颖而出。

lg888的博客 1088

手把手教你用三菱PLC控制JET伺服原点:从硬件接线到GXWORKS3程序调试

本文详细介绍了如何使用三菱PLC控制JET伺服系统实现原点回归功能,涵盖硬件接线、参数配置到GX Works3程序调试的全流程。通过实战指南,帮助工程师快速掌握伺服控制技术,提升工业自动化项目的实施效率。

weixin_30642561的博客 534

生成树 最小生成树_最小生成树

生成树 最小生成树Problem statement: 问题陈述: Given a weighted undirected graph. Find the sum of weights of edges of a Minimum Spanning Tree. 给定加权无向图。 查找最小生成树边缘的权重总和。 Input: 输入: Given 2 integers N and M. N ...

cumtv80668的专栏 300

DeepSeek-OCR 本地部署使用教程(附完整环境配置与推理示例)

DeepSeek-OCR是由DeepSeek AI推出的开源OCR模型,支持高精度文档识别与结构化输出(如Markdown转换)。本文提供了详细的本地部署指南,包括系统要求(推荐Linux+A100 GPU)、环境配置(Python 3.12.9+PyTorch 2.6.0+CUDA 11.8)、模型下载及两种推理方式:高性能的vLLM(适合批量处理)和灵活的Transformers(适合调试)。支持多种分辨率模式和提示词模板,适用于合同识别、PDF转换等场景。常见问题包括显存优化和性能调优建议。

qq_58607032的博客 6716

POJ 3522 Slim Span 并查集

http://poj.org/problem?id=3522 题意:给定一个含有N个结点的图,求这样一种生成树,该生成树中的最大边和最小边的权值之差最小。N 思路:由题意可知, 边的条数M的范围为:M 代码: #include #include #include using namespace std; #define MIN(a,b) (a)>(b)?(b):(a) const int

Chris_Home 778

从‘查理复用’到TM52F1363:深入剖析5脚188数码管的驱动原理与避坑指南

本文深入解析了5脚188数码管的驱动原理,重点介绍了查理复用技术如何通过5个IO引脚高效驱动188段显示,并详细探讨了TM52F1363单片机的寄存器配置与驱动实现。文章还提供了常见问题的解决方案和性能优化技巧,帮助开发者避免实际应用中的各种陷阱。

weixin_29159441的博客 478

RT-DETR改进策略【Neck】| GFPN 超越BiFPN 通过跳层连接和跨尺度连接改进RT-DETR颈部网络

本文记录的是利用颈部结构优化RT-DETR的目标检测网络模型。利用改进后的颈部网络,通过跳层连接,避免了在进行反向传播时的梯度消失问题,并且引入跨尺度连接,可以实现不同级别和层次的特征充分融合,获取足够的高层语义信息和低层空间信息交换,从而在大规模变化场景下提高检测性能。GIRAFFEDET: A HEAVY-NECK PARADIGM FOR OBJECT DETECTION-GFPN包含了上述的跳层连接(dense-link和log2nlog _{2} nlog2​n-link)以及跨尺度连接(Quee

Limiiiing的博客 2228

poj 3522 Slim Span【kruskal算法】

http://poj.org/problem?id=3522 题意: 题解: 代码: #include #include #include #include using namespace std; const int N = 111 , M = 10010; struct Edge { int u , v , w; }edge[M]; int n , m , E , f[

算法街区 559

基于互联网的智能旧衣回收箱设计与实现

本文介绍了基于嵌入式系统的智能旧衣回收箱的设计与实现,涵盖了硬件设计、软件开发、图像识别、物联网连接等方面的内容。通过利用最新的技术,如图像识别、物联网和云计算,实现了智能、便捷的旧衣物回收系统。未来,随着技术的不断进步,智能回收系统将会在环保领域发挥更大的作用。

2201_76125393的博客 906

POJ 3522 Slim Span MST变形

题意:给你N,M,表示N个节点,M条边。接下来给出M条边。 求生成一棵树之后,树中最长的路和最短的路的差值最小。求出,若没有则输出-1. 思路:进行多次kruskal算法,每次找出差值,进行更新,最后输出差值。 #include #include #include #include #include #include #include #include #include #

just_water 936

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

POJ 3522 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 ...

北里五井的博客 189

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

POJ 3522 Slim Span 暴力枚举 + 并查集

http://poj.org/problem?id=3522 一开始做这个题的时候,以为复杂度最多是O(m)左右,然后一直不会。最后居然用了一个近似O(m^2)的62ms过了。 一开始想到排序,然后扫一个长度n - 1区间,要快速判定这个区间能否构成MST,一直都想不到优秀的算法,然后干脆暴力了。 两种方法,1、dfs,删边容易,标记一下就好,但是这是不行的,删边确实容易,但是dfs的时候多...

weixin_30546933的博客 110

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

POJ 3522 Slim Span (最小生成树)

题目链接:http://poj.org/problem?id=3522 题意:简单的来说就是求一棵生成树使最大的边和最小的边差值最小 思路:用kruskal算法,按照边权从小到大进行枚举,注意判断这些边能否生成MST #include <cstdio> #include <cstring> #include <cstdlib> #...

Tongqi 490

poj 3522(最小生成树应用)

题目链接:http://poj.org/problem?id=3522思路:题目要求最小生成树中最大边与最小边的最小差值,由于数据不是很大,我们可以枚举最小生成树的最小边,然后kruskal求最小生成树,直到不能生成生成树为止,然后取最小的差值即可。 1 #include&lt;iostream&gt; 2 #include&lt;cstdio&gt; 3 #include&l...

weixin_34258078的博客 153

[POJ3522]最小差值生成树——[LCT]

【题目描述】 给定一个标号为从 1 到 n的、有 m 条边的无向图,求边权最大值与最小值的差值最小的生成树。 【输入格式】 第一行两个数 n,m,表示图的点和边的数量。 第二行起 m 行,每行形如 ui, vi, wi,代表 ui到 vi 间有一条长为 wi 的无向边。 【输出格式】 输出一行一个整数,代表你的答案。 数据保证存在至少一棵生成树。 Sample  InputSa...

sleepyNick 610

POJ3522Slim Span(最大边与最小边差值最小的生成树)

感谢这篇文章 本文对其代码,进行一些解释。 这道题的题意很明了。求最大边与最小边差值最小的生成树 首先,把所有的生成树都求出来是不可能的,所以,必须用别的方法。 在学习次小生成树的过程中,知道了一个最小生成树的性质, 一个图的最小生成树不一定是唯一的,但是组成这些最小生成树的各个边的权值一定都是一一对应相同的。不会出现这种一个树上有两个边权值a+b等于另外一颗树上两个边c+d,然后这两个树都是最小生成树的情况。 对于本题来讲,上面那个性质就说明了一个图的最小生成树上的最小边的权值和最大边的权值是固定不变的。

qq_39838607的博客 264

【CodeForces - 545 ABCDE套题训练题解】贪心, 构造,模拟,dp,最短路树(Dijkstra+变形)

A: 题干: Input The first line contains integer n (1 ≤ n ≤ 100) — the number of cars. Each of the next n lines contains n space-separated integers that determine matrix A. It is guaranteed that on t...

xuanweiace的博客 2005

Kruskal模板题 Slim Span uva

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Given an undirected weighted graph G , you should find one of spanning

但求心安的博客 480

poj 3522 Slim Span(最小生成树)kruskal算法

Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7010 Accepted: 3711DescriptionGiven an undirected weighted graph G, you should find one of spanning trees specified as fo

Passion 613

北大2018acm暑期课一贪心&二分&分治

01-派 总时间限制: 1000ms 内存限制: 65536kB 描述 我的生日要到了!根据习俗,我需要将一些派分给大家。我有N个不同口味、不同大小的派。有F个朋友会来参加我的派对,每个人会拿到一块派(必须一个派的一块,不能由几个派的小块拼成;可以是一整个派)。 我的朋友们都特别小气,如果有人拿到更大的一块,就会开始抱怨。因此所有人拿到的派是同样大小的(但不需要是同样形状的),虽然这样有些...

叶子心情你不懂 1032

POJ 3522 暴力+图论

Slim SpanTime Limit: 5000MS Memory Limit: 65536KTotal Submissions: 2967 Accepted: 1579DescriptionGiven an undirected weighted graph G, you s

bingshen的专栏 1639

BUAA-SCSE Training day3 5 6 7

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18400   Accepted: 8543 Description The island nation of Flatopia is perfectly flat. Unfortunately, F

VeryBigMan 2037
上一篇: POJ 1679 The Unique MST(次小生成树&Kruskal)
下一篇: 关于费马小定理的一个奇妙的组合证明
synapse7
博客等级 码龄14年 432粉丝 701原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值