当前位置:网站首页>AcWing 1142. 繁忙的都市 题解(最小生成树)
AcWing 1142. 繁忙的都市 题解(最小生成树)
2022-07-06 18:00:00 【乔大先生】
AcWing 1142. 繁忙的都市
克鲁斯卡尔算法求最小生成树里最大的边,记录最后加入最小生成树内的边即可
#include<bits/stdc++.h>
using namespace std;
const int N = 8010;
struct Edge{
int a, b, w;
bool operator < (const Edge &t) const{
return w < t.w;
}
}e[N];
int p[N];
int n, m;
int find(int x){
if(x != p[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;
e[i] = {
a, b, c};
}
sort(e, e + m);
int res = 0;
for(int i = 0; i < m; i ++ ){
int a = find(e[i].a), b = find(e[i].b), w = e[i].w;
if(a != b){
p[a] = b;
res = w;
}
}
cout<<n - 1<<' '<<res<<endl;
return 0;
}
边栏推荐
- 如何管理分布式团队?
- swiper组件中使用video导致全屏错位
- 2022 Google CTF SEGFAULT LABYRINTH wp
- 机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
- What are the differences between Oracle Linux and CentOS?
- DS-5/RVDS4.0变量初始化错误
- [signal and system]
- Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
- Amway wave C2 tools
- 前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
猜你喜欢
随机推荐
The difference between spin and sleep
2022 Google CTF SEGFAULT LABYRINTH wp
Supersocket 1.6 creates a simple socket server with message length in the header
分享一个通用的so动态库的编译方法
go-zero微服务实战系列(九、极致优化秒杀性能)
The MySQL database in Alibaba cloud was attacked, and finally the data was found
golang中的atomic,以及CAS操作
How to manage distributed teams?
LeetCode:1175. Prime permutation
Using the entry level of DVA in taro3.*
What are the differences between Oracle Linux and CentOS?
Neon Optimization: an optimization case of log10 function
Oracle: Practice of CDB restricting PDB resources
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
1123. The nearest common ancestor of the deepest leaf node
c语言—数组
C language instance_ two
Dark horse notes - exception handling
树莓派/arm设备上安装火狐Firefox浏览器
剑指 Offer II 035. 最小时间差-快速排序加数据转换