当前位置:网站首页>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;
}
边栏推荐
- 接收用户输入,身高BMI体重指数检测小业务入门案例
- 云呐|工单管理软件,工单管理软件APP
- shell脚本快速统计项目代码行数
- golang中的atomic,以及CAS操作
- Meet in the middle
- ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
- Case development of landlord fighting game
- Neon Optimization: About Cross access and reverse cross access
- Neon Optimization: summary of performance optimization experience
- swiper组件中使用video导致全屏错位
猜你喜欢

黑马笔记---异常处理

Js逆向——捅了【马蜂窝】的ob混淆与加速乐
![JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]](/img/40/da56fe6468da64dd37d6b5b0082206.png)
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]

According to the analysis of the Internet industry in 2022, how to choose a suitable position?

修改px4飞控的系统时间

Yunna - work order management system and process, work order management specification

2022 Google CTF SEGFAULT LABYRINTH wp

Lldp compatible CDP function configuration

MySQL script batch queries all tables containing specified field types in the database

LeetCode:1175. 质数排列
随机推荐
Amway wave C2 tools
Atomic in golang, and cas Operations
Implementation principle of waitgroup in golang
云呐-工单管理制度及流程,工单管理规范
2022 Google CTF SEGFAULT LABYRINTH wp
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
第三方跳转网站 出现 405 Method Not Allowed
分享一个通用的so动态库的编译方法
curl 命令
[case sharing] basic function configuration of network loop detection
云呐|工单管理办法,如何开展工单管理
tansig和logsig的差异,为什么BP喜欢用tansig
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
Installation of gazebo & connection with ROS
C语言实例_4
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
7.6 simulation summary
Vocabulary in Data Book
免费白嫖的图床对比
LeetCode:1175. 质数排列