当前位置:网站首页>AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)
AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)
2022-07-06 18:00:00 【乔大先生】
AcWing 1141. 局域网
克鲁斯卡尔求最小生成森林(就是每个联通块求最小生成树),克鲁斯卡尔的算法过程刚好是从小到大依次将边加入集合中,所以这里刚好利用克鲁斯卡尔算法求
#include<bits/stdc++.h>
using namespace std;
const int N = 210;
int p[N]; //并查集祖先数组
int n, m;
int res; //记录答案
struct Edge{
int a, b, w;
bool operator<(const Edge &t) const {
return w < t.w;
}
}edge[N];
int find(int x){
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
cin>>n>>m;
for(int i = 1; i <= n; i ++ ) p[i] = i; //注意初始化并查集
for(int i = 0; i < m; i ++ ){
int a, b, c;
cin>>a>>b>>c;
edge[i] = {
a, b, c};
}
sort(edge, edge + m);
for(int i = 0; i < m; i ++ ){
//a和b相连才会被遍历到,所以这里不用担心将两个不同的连通块连在一起
int a = find(p[edge[i].a]), b = find(p[edge[i].b]);
if(a != b) p[a] = b; //如果不在一个集合内,就加入到一个集合内
else res += edge[i].w; //如果这两个点在一个联通块内,说明他们已经通过花费更小的边连在一起,目前这个边就可以删除了
}
cout<<res<<endl;
return 0;
}
边栏推荐
- 【芯片方案设计】脉搏血氧仪
- C language instance_ three
- 字节P7专业级讲解:接口测试常用工具及测试方法,福利文
- Taro applet enables wxml code compression
- swiper组件中使用video导致全屏错位
- Make Jar, Not War
- Body mass index program, entry to write dead applet project
- Case development of landlord fighting game
- 机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
- Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
猜你喜欢
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
HMM notes
Comparison of picture beds of free white whoring
【C语言进阶篇】指针的8道笔试题
405 method not allowed appears when the third party jumps to the website
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
今日问题-2022/7/4 lambda体中修改String引用类型变量
第三方跳转网站 出现 405 Method Not Allowed
黑马笔记---创建不可变集合与Stream流
Yunna - work order management system and process, work order management specification
随机推荐
Force buckle 1037 Effective boomerang
NEON优化:关于交叉存取与反向交叉存取
Taro2.* 小程序配置分享微信朋友圈
Instructions for using the domain analysis tool bloodhound
Supersocket 1.6 creates a simple socket server with message length in the header
Match VIM from zero (0) -- Introduction to vimscript
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
Receive user input, height BMI, BMI detection small business entry case
Atomic in golang and CAS operations
Share a general compilation method of so dynamic library
C language instance_ four
修改px4飞控的系统时间
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
第三方跳转网站 出现 405 Method Not Allowed
golang中的WaitGroup实现原理
Yunna | work order management software, work order management software app
一起看看matlab工具箱内部是如何实现BP神经网络的
云呐|工单管理办法,如何开展工单管理
Metauniverse urban legend 02: metaphor of the number one player
【信号与系统】