当前位置:网站首页>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;
}
边栏推荐
- 从零开始匹配vim(0)——vimscript 简介
- Typical problems of subnet division and super network construction
- AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)
- AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
- Let's see through the network i/o model from beginning to end
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- AcWing 345. 牛站 题解(floyd的性质、倍增)
- 对C语言数组的再认识
- C language instance_ two
- 736. LISP syntax parsing: DFS simulation questions
猜你喜欢
随机推荐
【信号与系统】
MySQL script batch queries all tables containing specified field types in the database
使用nodejs完成判断哪些项目打包+发版
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
公钥\私人 ssh避password登陆
AcWing 904. 虫洞 题解(spfa求负环)
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
[JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
c语言—数组
LeetCode:1175. Prime permutation
云呐|工单管理办法,如何开展工单管理
免费白嫖的图床对比
AcWing 1140. 最短网络 (最小生成树)
2022 Google CTF SEGFAULT LABYRINTH wp
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
Neon Optimization: an optimization case of log10 function
Taro 小程序开启wxml代码压缩
Google released a security update to fix 0 days that have been used in chrome
Dark horse notes - exception handling