MST-kruskal ElogE+V
2024-09-02 10:33:25
#include<stdio.h>
#include<algorithm>
using namespace std;
struct dis
{
int a, b, c;
} s[];
int cmp(dis x, dis y)
{
return x.c < y.c;
}
int father[];
int fsize[];
int findfather(int y)
{
int r = y;
while (r != father[r])
{
r = father[r];
}
return r;
}
int combine(int a, int b)
{
int fx = findfather(a);
int fy = findfather(b);
if (fx != fy)
{
if (fsize[fx] >= fsize[fy])
{
father[fy] = fx;
fsize[fx] += fsize[fy];
fsize[fy] = ;
}
else
{
father[fx] = fy;
fsize[fy] += fsize[fx];
fsize[fx] = ;
}
return ;
}
else
{
return ;
}
}
int main()
{
int t, i, n, sum, m;
while (~scanf("%d", &t), t)
{
n = t * (t - ) / ;
for (i = ; i <= t; i++)
{
fsize[i] = ;
father[i] = i;
}
for (i = ; i < n; i++)
{
scanf("%d%d%d", &s[i].a, &s[i].b, &s[i].c);
}
sort(s, s + n, cmp);
m = , sum = ;
for (i = ; i < n && m < t; i++)
{
if (combine(s[i].a, s[i].b))
{
m++;
sum += s[i].c;
}
}
printf("%d\n", sum);
}
return ;
}
最新文章
- 发布Live Writer代码着色插件CNBlogs.CodeHighlighter
- github上比较全的知识
- ASP.NET之Ajax系列(三)
- 怎么用ABBYY打开PDF文档
- do while 与while的区别!
- Android 字体颜色变化(点击)
- poj2196---Specialized Four-Digit Numbers
- 汉化Eclipse
- 海康&;大华&;DSS视频拉流-RTSP转RTMP多媒体播放技术
- CentOS配代理服务器
- Windows操作系统下搭建Git服务器和客户端。
- Laravel 学习笔记
- P2880 [USACO07JAN]平衡的阵容Balanced Lineup(RMQ的倍增模板)
- python-面向对象(绑定方法与非绑定方法)
- 2.匿名类,匿名类对象,private/protected/public关键字、abstract抽象类,抽象方法、final关键字的使用,多线程Thread类start方法原理
- Docker学习笔记之浅谈虚拟化和容器技术
- nc命令简介
- matplotlib01
- python numpy logic_and
- 生成带有表格的word附件和动态赋值