POJ 3522 Slim Span(MST)

运维面试常见问题 对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。优缺点:优点:利用索引可以加速对数据的检索,索引是可以唯一的,创建索引允许指定单个列或者多个列缺点:减慢了数据录入速度,同时也增加了数据库的尺寸大小。 阅读详情
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;
}


Java对接快递鸟,查询快递物流信息 根据用户输入的订单号,我们的后台识别订单号并根据快递鸟查询快递API接口,实现自动查询的功能。下面是java版本的根据单号查询物流跟踪信息的详细示例代码。只需要下载就可以运行,(自己需要简单的修改)注:登录快递鸟用户管理后台后获得。登录快递鸟用户管理后台。 阅读详情

相关推荐

CSP历年真题201503-1-图像旋转(C/C++)

一、问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度。 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可。 输入格式 二、输入格式 输入的第一行包含两个整数n, m,分别表示图像矩阵的行数和列数。 接下来n行每行包含m个整数,表示输入的图像。 三、输出格式 输出m行,每行包含n个整数,表示原始矩阵逆时针旋转90度后的矩阵。 四、...

GTboy100的博客 1679

图

翻译可参考: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

老照片 318

i春秋第二届春秋欢乐赛 classical writeup

i春秋第二届春秋欢乐赛 classical writeup 第一步:题目给出的提示 根据题目我们可以了解这道题应该是一个古典密码的题目,但是古典密码太多了我们如何知道是什么? 看题目给出的附件是一个很长很长的乱序字符串,我们可以想到用词频分析一下: 发现里面有一段不一样的信息:LyjtL3fvnSRlo2xvKIjrK2ximSHkJ3ZhJ2Hto3x9 像是base64解码能够做出来,但是发现...

hanjinrui15的博客 1435

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

Matplotlib图片尺寸调整全攻略:从figsize到savefig的实战技巧

在数据可视化与科学计算中,图形尺寸控制是影响图表可读性与专业度的基础环节。其核心原理在于协调逻辑尺寸(英寸)与物理输出(像素)的映射关系,这直接决定了信息呈现的清晰度与布局合理性。从技术价值看,精准的尺寸控制能提升图表在不同媒介(如学术论文、报告、网页)中的适配性,避免显示不全、模糊失真等常见问题。在应用场景上,无论是Jupyter Notebook中的交互探索、自动化报告的批量生成,还是满足严格出版要求的插图制作,都需要掌握对应的尺寸调整策略。本文聚焦于matplotlib中的figsize参数、rcPa

weixin_30899023的博客 351

基于 Python + Flask + Echarts 的疫情监控系统

基于 Python + Flask + Echarts 的疫情监控系统

753

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

生成树 最小生成树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

Codex 100个真实案例 - 用AI生成Blender 3D建模脚本(建模师要失业了?)

本案例将展示如何用 Codex 生成 Blender Python API(bpy)脚本,自动创建完整的 3D 模型。我们将从基础几何体开始,逐步实现布尔运算、材质设置、光照配置,最终完成一张精美桌子的 3D 建模——包括桌面、桌腿、倒角、木纹材质。还会实现参数化批量生成不同风格的桌子、渲染输出 PNG 图片、以及 360 度旋转动画。全程用自然语言驱动 Codex,让建模从"点鼠标"变成"说人话"!

yueyeguzhuo的专栏 656

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

DDS协议--[第一章][概述]

Data Distribute Service,数据分发服务:数据分发服务(DDS™)是一个由对象管理组(OMG)发布的以数据为中心的中间件协议和API标准。DDS集成系统中的各个组件,提供低延迟数据连接、高可靠性以及高可扩展体系结构,以适应商业级物联网(IoT)应用程序的需求。

qq_41816308的博客 1159

2024年游戏策划最新面试题及答案

1. **游戏策划**:负责游戏设计和开发的创意和技术过程。2. **主要职责**:设计游戏玩法、故事、角色、世界等。4. **游戏机制**:定义玩家如何与游戏世界互动的规则。13. **引人入胜的角色**:深入的背景故事、个性特征。3. **游戏平衡**:确保游戏公平、有趣且具有挑战性。7. **RPG特点**:角色发展、故事驱动、玩家选择。12. **关卡设计技巧**:挑战性、流动性、视觉引导。11. **游戏关卡设计**:创建游戏世界的不同阶段。5. **游戏循环**:玩家在游戏中重复的行为模式。

guo162308的博客 2330

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

SPICE仿真软件:Ngspice_(2).安装与配置

在Netlist文件中直接定义模型。V1 1 0 5R1 2 0 1k* Analysis库文件中可以包含多种电路元件模型和参数。

kkchenjj的博客 1615

POJ 3522 Slim Span MST变形

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

just_water 936

inori mmd 7z

楪祈的模型资源....................................................................................................................................................................................................................

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

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

北里五井的博客 190

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

POJ 3522 Slim Span (最小生成树)

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

Tongqi 492

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

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

sleepyNick 613

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[

算法街区 560

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

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

qq_39838607的博客 265

【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的博客 2011

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个朋友会来参加我的派对,每个人会拿到一块派(必须一个派的一块,不能由几个派的小块拼成;可以是一整个派)。 我的朋友们都特别小气,如果有人拿到更大的一块,就会开始抱怨。因此所有人拿到的派是同样大小的(但不需要是同样形状的),虽然这样有些...

叶子心情你不懂 1033

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

但求心安的博客 481

图论——最小生成树(Prim算法,Kruskal算法及常用模板)

最小生成树算法——Prim 算法(普⾥姆)Prim代码模板例题 POJ - 1258 Agri-NetAC代码最小生成树算法——Kruskal 算法(克鲁斯卡尔)Kruskal代码模板例题 POJ - 1287 NetworkingAC代码 最小生成树算法——Prim 算法(普⾥姆) 从某⼀个顶点开始构建⽣成树;每次将代价最⼩的新顶点纳⼊⽣成树,直到所有顶点都纳⼊为⽌。 算法描述: 在一个加权连通图中,顶点集合C,边集合为E 任意选出一个点作为初始顶点,标记为✔,计算所有与之相连接的点的距离,选择距离最.

沐雨风栉 1690

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、付费专栏及课程。

余额充值
⌂ 换网址