当前位置:网站首页>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;
}
边栏推荐
- Table table setting fillet
- What does front-end processor mean? What is the main function? What is the difference with fortress machine?
- Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
- DS-5/RVDS4.0变量初始化错误
- NEON优化:性能优化常见问题QA
- Analysis of mutex principle in golang
- Yunna | work order management software, work order management software app
- 从零开始匹配vim(0)——vimscript 简介
- Receive user input, height BMI, BMI detection small business entry case
- 1123. 最深叶节点的最近公共祖先
猜你喜欢

【C语言进阶篇】指针的8道笔试题
![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]

黑马笔记---创建不可变集合与Stream流

Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP

2022 Google CTF segfault Labyrinth WP

C language - array
![[signal and system]](/img/aa/a65d6da1d1d9410254ca7b775e24a6.png)
[signal and system]

Wood extraction in Halcon

JTAG debugging experience of arm bare board debugging

According to the analysis of the Internet industry in 2022, how to choose a suitable position?
随机推荐
C语言实例_2
Table table setting fillet
第三方跳转网站 出现 405 Method Not Allowed
Using the entry level of DVA in taro3.*
让我们,从头到尾,通透网络I/O模型
JTAG debugging experience of arm bare board debugging
C语言实例_5
Gnet: notes on the use of a lightweight and high-performance go network framework
AI 从代码中自动生成注释文档
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
Lldp compatible CDP function configuration
shell脚本快速统计项目代码行数
黑马笔记---异常处理
C语言实例_4
Spark TPCDS Data Gen
HMM notes
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
Neon Optimization: an instruction optimization case of matrix transpose
Boot - Prometheus push gateway use
Taro 小程序开启wxml代码压缩