P2330 繁忙的城市(krusal最小生成树)

直接干

复制代码
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<climits>
 4 using namespace std;
 5 struct edge
 6 {
 7     int from,to,weight;
 8 }a[100010];//存边
 9 int fa[310];//存并查集
10 bool cmp(edge a,edge b)
11 {
12     return a.weight<b.weight;
13 }
14 int finds(int x)//本来以为还需要路径压缩的
15 {
16     while(fa[x]!=x)
17     {
18         x=fa[x];
19     }
20     return x;
21 }
22 int main(void)
23 {
24     int n,m;
25     cin>>n>>m;
26     for(int i=0;i<m;i++)
27     {
28         cin>>a[i].from>>a[i].to>>a[i].weight;
29     }
30     sort(a,a+m,cmp);
31     for(int i=1;i<=n;i++)
32         fa[i]=i;//初始化并查集
33     int cnt=0,maxn=INT_MIN;
34     for(int i=0;i<m;i++)
35     {
36         int t1=a[i].from;
37         int t2=a[i].to;
38         int w=a[i].weight;
39         int f1=finds(t1);
40         int f2=finds(t2);
41         if(f1!=f2)
42         {
43             cnt++;//边数加一
44             maxn=max(maxn,w);
45             fa[f1]=f2;
46         }
47     }
48     cout<<cnt<<" "<<maxn;
49     return 0;
50 }
复制代码

 

posted on   greenofyu  阅读(138)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示