当前位置:网站首页>AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
2022-07-07 01:36:00 【Mr. Qiao Da】
AcWing 1141. LAN
Cruz Carr seeks the minimum generated forest ( Is to find the minimum spanning tree for each connected block ), Kruskal's algorithm process is just to add edges to the set in turn from small to large , So here we just use Kruskal algorithm to find
#include<bits/stdc++.h>
using namespace std;
const int N = 210;
int p[N]; // Merge ancestor array
int n, m;
int res; // Record answer
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; // Pay attention to initialization and set lookup
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 and b Connected will be traversed to , So there is no need to worry about connecting two different connected blocks
int a = find(p[edge[i].a]), b = find(p[edge[i].b]);
if(a != b) p[a] = b; // If not in a set , Just join a set
else res += edge[i].w; // If these two points are in a connection block , It means that they have been connected by the side with less cost , At present, this side can be deleted
}
cout<<res<<endl;
return 0;
}
边栏推荐
- Share a general compilation method of so dynamic library
- 安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
- 剑指 Offer II 035. 最小时间差-快速排序加数据转换
- 增加 pdf 标题浮窗
- The difference between Tansig and logsig. Why does BP like to use Tansig
- AcWing 904. 虫洞 题解(spfa求负环)
- LeetCode:1175. Prime permutation
- DS-5/RVDS4.0变量初始化错误
- Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
- C # method of calculating lunar calendar date 2022
猜你喜欢
AcWing 345. 牛站 题解(floyd的性质、倍增)
Let's see through the network i/o model from beginning to end
Right mouse button customization
微信公众号发送模板消息
【C语言进阶篇】指针的8道笔试题
Yunna - work order management system and process, work order management specification
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
LeetCode:1175. Prime permutation
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
随机推荐
HMM notes
Wood extraction in Halcon
Appium自动化测试基础 — uiautomatorviewer定位工具
Let's see how to realize BP neural network in Matlab toolbox
THREE.AxesHelper is not a constructor
Drag to change order
AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
LeetCode:1175. 质数排列
Google released a security update to fix 0 days that have been used in chrome
1123. 最深叶节点的最近公共祖先
Yunna | work order management software, work order management software app
Add the applet "lazycodeloading": "requiredcomponents" in taro,
C language instance_ five
dvajs的基础介绍及使用
Appium基础 — Appium Inspector定位工具(一)
Body mass index program, entry to write dead applet project
剑指 Offer II 035. 最小时间差-快速排序加数据转换
405 method not allowed appears when the third party jumps to the website
C language instance_ three
AcWing 904. 虫洞 题解(spfa求负环)