当前位置:网站首页>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;
}
边栏推荐
- [case sharing] basic function configuration of network loop detection
- 让我们,从头到尾,通透网络I/O模型
- 修改px4飞控的系统时间
- Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
- Taro2.* 小程序配置分享微信朋友圈
- Vocabulary in Data Book
- Yunna | work order management measures, how to carry out work order management
- Docker method to install MySQL
- 黑马笔记---创建不可变集合与Stream流
- 云呐|工单管理软件,工单管理软件APP
猜你喜欢
![[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)](/img/40/dc45df3cd3ee7642277eff899bc6aa.png)
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)

移植DAC芯片MCP4725驱动到NUC980

JTAG debugging experience of arm bare board debugging

Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman

go-zero微服务实战系列(九、极致优化秒杀性能)

第三方跳转网站 出现 405 Method Not Allowed

字节P7专业级讲解:接口测试常用工具及测试方法,福利文

1123. 最深叶节点的最近公共祖先

LeetCode:1175. 质数排列

Send template message via wechat official account
随机推荐
Neon Optimization: an optimization case of log10 function
Let's see through the network i/o model from beginning to end
Neon Optimization: performance optimization FAQ QA
交叉验证如何防止过拟合
Google released a security update to fix 0 days that have been used in chrome
Send template message via wechat official account
uva 1401 dp+Trie
The cost of returning tables in MySQL
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Share a general compilation method of so dynamic library
接收用户输入,身高BMI体重指数检测小业务入门案例
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
系统休眠文件可以删除吗 系统休眠文件怎么删除
Instructions for using the domain analysis tool bloodhound
curl 命令
从底层结构开始学习FPGA----FIFO IP的定制与测试
分享一个通用的so动态库的编译方法
Transformation transformation operator
1123. 最深叶节点的最近公共祖先
Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe