UVALive 3887 Slim Span(枚举+MST,4级)

全局最小割 一个无向连通网络,去掉一个边集可以使其变成两个连通分量则这个边集就是割集;最小割集当然就权和最小的割集。 可以用最小切割最大流定理: 1.min=MAXINT,确定一个源点 2.枚举汇点 3.计算最大流,并确定当前源汇的最小割集,若比min小更新min 4.转到2直到枚举完毕 5.min即为所求输出min     不难看出复杂度很高:枚举汇点要O(n),最 阅读详情

E - Slim Span
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

Download as PDF

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 $ \in$ 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 .

\epsfbox{p3887a.eps}

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

=6in  \epsfbox{p3887b.eps}

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 
$ \vdots$ 
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$ \le$n$ \le$100 and 0$ \le$m$ \le$n(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 k -th 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

思路:枚举+MST


#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<algorithm>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mp=109;
class Edge
{
public:
  int u,v,next,w;
  bool operator<(const Edge&x)const
  {
    return w<x.w;
  }
};
class Tree
{ public:
  int n,m,slim;
  int rt[mp];
  Edge e[mp*mp*2];
  void clear()
  {
    FOR(i,0,n)rt[i]=i;
  }
  int find(int x)
  {
    if(rt[x]^x)
      rt[x]=find(rt[x]);
    return rt[x];
  }
  int MST_tree(int s)
  {
    int num=0,u,v,t;
    FOR(i,s,m-1)
    { u=e[i].u;v=e[i].v;
      u=find(u);v=find(v);
      if(u==v)continue;
      rt[u]=v;
      ++num;
      if(num==n-1){t=i;break;}
    }
    if(num!=n-1)return -1;
    return e[t].w-e[s].w;
  }
  void getans()
  {
    sort(e,e+m);
    clear();
    slim=MST_tree(0);
    if(slim==-1)
    {
      printf("-1\n");return;
    }
    FOR(i,1,m-1)
    if(e[i].w==e[i-1].w)continue;
    else
    { clear();
      int t=MST_tree(i);
      if(t==-1){printf("%d\n",slim);return;}
      if(t<slim)slim=t;
    }
    printf("%d\n",slim);
  }
}sp;
int main()
{
  int n,m;
  while(~scanf("%d%d",&n,&m))
  { sp.n=n;sp.m=m;
    if(n==0&&m==0)break;
    FOR(i,0,m-1)
    {
      scanf("%d%d%d",&sp.e[i].u,&sp.e[i].v,&sp.e[i].w);

    }sp.getans();
  }
}




科技中介服务机构想要提升服务效率,需要哪些数智工具支持?.docx 科技中介服务机构想要提升服务效率,需要哪些数智工具支持? 立即下载

相关推荐

usb 扩容盘检测修复工具源码,编译产物

usb 扩容盘检测修复工具源码,编译产物

【UVALive】3887 Slim Span 枚举+最小生成树

传送门:【UVALive】3887 Slim Span ti mu da

poursoul 1007

融合UWB和IMU割草机定位EKF研究(Matlab代码实现)

内容概要:本文主要介绍了一个名为“Plecs全桥LLC谐振变换器仿真模型”的技术资源,该资源聚焦于电力电子领域中的LLC谐振变换器建模与仿真,涵盖开环、闭环控制、软启动设计及参数优化等核心内容。文档不仅提供了LLC变换器的详细建模方法与仿真案例,还系统梳理了其在高效电源转换系统中的应用,包括隔离型全桥拓扑结构的设计与性能分析。此外,资源汇总了大量Matlab/Simulink仿真模型及相关科研方向,涉及新能源并网、储能控制、电机驱动等多个前沿领域,全面展示LLC谐振变换器在现代电力系统中的关键技术实现路径。同时,文档附带多种学习资料获取途径,如网盘链接和公众号资源,便于研究人员深入实践与拓展研究。; 适合人群:具备扎实的电力电子、自动控制理论基础,从事新能源发电、开关电源设计、电力系统仿真等相关领域的研究生、工程师及科研人员。; 使用场景及目标:①深入理解LLC谐振变换器的工作原理、建模方法与控制策略;②开展电力电子变换器的仿真验证与参数优化设计;③支撑科研项目、学位论文或工程项目的系统仿真与方案验证; 阅读建议:建议结合所提供的仿真模型与参考文献,使用Plecs或Simulink软件进行动手实践,重点掌握LLC谐振变换器的建模流程、闭环控制设计、软启动机制及参数调试技巧,以全面提升在实际工程中的应用与创新能力。

uva 10917 Walk Through the Forest(最短路+DP路径,4级)

Description Problem C: A Walk Through the Forest Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he lik

nealgavin的专栏 2130

(92页PPT)某大型集团信息化项目企业数字化转型ERP系统蓝图设计规划方案(附下载方式)

同时,外贸获利空间被严重挤压,必须从价格竞争转向品牌竞争。该方案针对某大型外贸集团在数字化时代的转型需求,提出了一套完整的ERP系统建设框架和实施路径,具有很高的专业参考价值。特别值得关注的是方案提出的"价值为先"指导思想,通过将企业价值图与信息系统建设紧密结合,确保IT投资直接支撑价值创造。同时,方案充分考虑了集团多元化、国际化的发展需求,设计了灵活可扩展的系统架构。总的来说,这份实施方案专业性强、内容全面、措施具体,既立足解决当前问题,又着眼未来发展需求,为大型集团企业的数字化转型提供了优秀范本。

AI_data_cloud的博客 1027

深入解析磁盘性能测试:vdbench与fio的实战对比

本文深入对比了磁盘性能测试两大工具vdbench与fio的核心定位与实战应用。vdbench擅长多客户端、可编排的存储系统集成测试,而fio则以灵活的单机深度定制见长,是测试硬件极限和模拟特定应用IO模型的利器。文章通过安装指南、参数解析和典型场景对比,帮助读者根据需求选择合适工具,精准评估磁盘性能。

weixin_29300931的博客 330

CF-29C - Mail Stamps(DFS+离散化)

C - Mail Stamps Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 29C Description

nealgavin的专栏 1707

操作系统原理:【优麒麟】linux内核添加系统调用

在底端添加如下代码。

Joyce_zz的博客 2709

hdu 4756 Install Air Conditioning(MST+全边次小树,4级)

Install Air Conditioning Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 174    Accepted Submission(s): 23 Problem Description   

nealgavin的专栏 1443

群晖NAS安装HomeAssistant保姆级教程:从Docker部署到智能设备接入

本文提供了一份详尽的群晖NAS安装HomeAssistant保姆级教程,涵盖从Docker部署到智能设备接入的全过程。通过规划存储路径、配置容器环境变量和网络模式,确保系统稳定运行。教程还介绍了如何接入小米、苹果HomeKit等智能家居设备,并利用自动化与仪表盘打造本地化、高响应速度的智能中枢,实现数据隐私与远程控制的完美平衡。

play7的博客 696

工业机器人抓取目标检测与抓取姿态估计系统:基于YOLOv12与Cornell Grasping Dataset

工业机器人抓取是自动化生产中的核心环节,而精确的目标检测和抓取姿态估计是实现高效抓取的关键。本文将详细介绍如何构建一个完整的工业机器人抓取系统,该系统结合了最新的YOLOv12目标检测算法和Cornell Grasping Dataset的抓取姿态估计,并提供了用户友好的UI界面。本文详细介绍了一个完整的工业机器人抓取系统,该系统结合了YOLOv12目标检测和基于Cornell Grasping Dataset的抓取姿态估计。我们将使用PyQt5构建一个用户友好的界面,集成目标检测和抓取姿态估计功能。

YOLO 574

hdu 4650 Minimum Average Weight Path(最短路,5级)

第4场多校联合训练解题报告——详见杭电ACM微博~ Minimum Average Weight Path Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 94    Accepted Submissio

nealgavin的专栏 1389

基于STM32设计的智能家居控制系统(华为云)_363

本项目设计并实现了一种基于STM32的智能家居控制系统,融合了语音识别、远程控制、环境监测与安全报警等功能。系统以STM32F103C8T6单片机为核心控制器,集成ESP8266 Wi-Fi模块实现与华为云IOT物联网平台的数据交互,采用MQTT协议进行通信。用户可通过语音识别模块控制风扇、空调、窗帘、卧室灯、客厅灯和电视等家电的开关操作,同时也可通过Android手机APP或Windows电脑上位机进行远程控制。系统还具备温湿度、光照强度、烟雾浓度和火焰检测功能,当检测到异常情况时,自动触发蜂鸣器报警并向

882

UVA1416 Warfare And Logistics(LA4080)(最短路,5级)

Description The army of United Nations launched a new wave of air strikes on terrorist forces. The objective of the mission is to reduce enemy's logistical mobility. Each air strike will destro

nealgavin的专栏 1343

【部署优化篇三】《DeepSeek边缘计算实战:把目标检测模型塞进树莓派,让AI在巴掌大的设备上“开天眼“》

谁说只有超级计算机才能跑AI?今天咱们就要在树莓派上玩转DeepSeek目标检测,让这个巴掌大的小盒子变成会‘看’世界的智能终端!本文手把手教你从零开始,把最潮的目标检测模型塞进树莓派。全程高能预警,建议准备好你的树莓派4B/5和散热风扇,咱们这就开启边缘计算的魔法之旅!

商务合作|问题讨论|交流学习 请联系作者微信,加微信请务必注明来意,博客主页有联系方式 1306

Vue3 - 详解实现网站接入钉钉扫码登录功能,钉钉授权生成登录二维码用户用手机扫描二维码登陆,支持弹框式、浏览器单独新开页显示,钉钉扫描二维码登录详细流程及示例代码,最新vue集成钉钉扫码授权登录!

vue3,nuxt3,钉钉,扫码登录,vue3钉钉扫码登陆,钉钉授权第三方WEB网站扫码登录功能,钉钉扫码登录第三方网站,单点登录,二维码不显示,报错,扫了没反应,出错,vue3钉钉扫码登录,内嵌,获取个人信息,pc网页钉钉登录,钉钉扫码登录怎么新开一页,跳页显示二维码,内嵌登录二维码,iframe新页面弹出,弹出一个小窗口显示登录二维码,登录报错,扫码没反应,解决手机点登录报错没反应,官方文档,手机扫描钉钉二维码登录网站,页面弹出一个小窗口而不是打开一个页面,vue3中使用iframe实现另开小窗口扫码登

🏆 前端领域优质创作者 3695

HDU 1068 Girls and Boys(二分图最大独立点集,3级)

Girls and Boys Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status   Appoint description:  System Crawler  (2012-10-30) Description the secon

nealgavin的专栏 1230

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

uva 1364 - Knights of the Round Table(点双连通分量,5级)

Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising tha

nealgavin的专栏 1225

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

ZOJ 3780 Paint the Grid Again(topsort)

ZOJ Problem Set - 3780 Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black

nealgavin的专栏 1209

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

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

10054 - The Necklace(欧拉回路+DFS,2级)

Problem D: The Necklace  My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace shared a common color at their meeting point. The figure belo

nealgavin的专栏 1184

hdu 4725 The Shortest Path in Nya Graph(堆+dij,最短路,5级)

The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 193    Accepted Submission(s): 38 Problem Description Th

nealgavin的专栏 1167

hdu 4685 Prince and Princess(最大匹配+强连通所点,5级)

Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 248    Accepted Submission(s): 76 Problem Description There are n p

nealgavin的专栏 1149

uva 11374 Airport Express(最短路dijistra+堆优化,4级)

Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. The

nealgavin的专栏 1124

uva 1423 - Guess (topsort,4级)

Given a sequence of integers, a1, a2,..., an , we define its sign matrix S such that, for 1ijn , Sij = `` + " if ai +...+ aj > 0 ; Sij = `` - " if ai +...+ aj  ; and Sij = ``0" otherwise. For exa

nealgavin的专栏 1121

hdu 4679 Terrorist’s destroy (DP or 树直径,4级)

Terrorist’s destroy Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 132    Accepted Submission(s): 20 Problem Description There is a ci

nealgavin的专栏 1068
上一篇: codeforce 365 C. Compartments(gready,4级)
下一篇: HDU 4081 Qin Shi Huang's National Road System(次小树,4级)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值