当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Yunna | work order management measures, how to carry out work order management

dvajs的基础介绍及使用

AI automatically generates annotation documents from code

Installation of gazebo & connection with ROS

tansig和logsig的差异,为什么BP喜欢用tansig

鼠标右键 自定义

Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which

设置Wordpress伪静态连接(无宝塔)

LeetCode:1175. 质数排列

【信号与系统】
随机推荐
AI 从代码中自动生成注释文档
AcWing 904. 虫洞 题解(spfa求负环)
糊涂工具类(hutool)post请求设置body参数为json数据
Neon Optimization: performance optimization FAQ QA
Transformation transformation operator
json学习初体验–第三者jar包实现bean、List、map创json格式
mongodb查看表是否导入成功
Taro2.* applet configuration sharing wechat circle of friends
Taro 小程序开启wxml代码压缩
数据手册中的词汇
Vocabulary in Data Book
C language instance_ three
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
How to manage distributed teams?
AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)
docker 方法安装mysql
Let's see through the network i/o model from beginning to end
736. LISP syntax parsing: DFS simulation questions
从零开始匹配vim(0)——vimscript 简介
鼠标右键 自定义