当前位置:网站首页>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;
}
边栏推荐
- What does front-end processor mean? What is the main function? What is the difference with fortress machine?
- 机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
- Match VIM from zero (0) -- Introduction to vimscript
- HMM notes
- Spark TPCDS Data Gen
- Instructions for using the domain analysis tool bloodhound
- Neon Optimization: performance optimization FAQ QA
- Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
- Neon Optimization: an instruction optimization case of matrix transpose
- Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
猜你喜欢
云呐-工单管理制度及流程,工单管理规范
第三方跳转网站 出现 405 Method Not Allowed
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
让我们,从头到尾,通透网络I/O模型
Gazebo的安装&与ROS的连接
LeetCode:1175. 质数排列
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
身体质量指数程序,入门写死的小程序项目
MySQL script batch queries all tables containing specified field types in the database
Wood extraction in Halcon
随机推荐
THREE. AxesHelper is not a constructor
c语言—数组
swiper组件中使用video导致全屏错位
安利一波C2工具
[chip scheme design] pulse oximeter
NEON优化:性能优化经验总结
LeetCode:1175. 质数排列
云呐|工单管理软件,工单管理软件APP
Match VIM from zero (0) -- Introduction to vimscript
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
C语言实例_2
How to manage distributed teams?
NEON优化:矩阵转置的指令优化案例
域分析工具BloodHound的使用说明
Make Jar, Not War
C language instance_ four
Let's see through the network i/o model from beginning to end
Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
taro3.*中使用 dva 入门级别的哦
Analysis of mutex principle in golang