hdu 1233

#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 ;
}

MST-kruskal ElogE+V的更多相关文章

  1. MST — Kruskal's algorithm

    算法简介 Kruskal算法可用来求解MST(最小生成树)问题,还可以作为迷宫生成算法等. 算法分析 其实算法不难理解,算法先要将 $ G(V, E) $ 的集合 $ E $ 按权重 $ \Omega ...

  2. MST(Kruskal’s Minimum Spanning Tree Algorithm)

    You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...

  3. POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27141   Accepted: 9712 D ...

  4. 最小生成树 (Minimum Spanning Tree,MST) --- Kruskal算法

    本文链接:http://www.cnblogs.com/Ash-ly/p/5409265.html 引导问题: 假设要在N个城市之间建立通信联络网,则连通N个城市只需要N - 1条线路.这时,自然会考 ...

  5. POJ 1679 The Unique MST --Kruskal应用

    这题可以用次小生成树解,这里用Kruskal算法来做.每条边除维护u,v,w外,还维护: used:表示这条边是否加过 eq:表示有没有与这条边相等的边 del:删除标记,以便删边之用 如果对于一个最 ...

  6. NOI.AC #31 MST —— Kruskal+点集DP

    题目:http://noi.ac/problem/31 好题啊! 题意很明白,对于有关最小生成树(MST)的题,一般是要模拟 Kruskal 过程了: 模拟 Kruskal,也就是把给出的 n-1 条 ...

  7. The Unique MST(次小生成树)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22335   Accepted: 7922 Description Give ...

  8. 数据结构学习笔记05图(最小生成树 Prim Kruskal)

    最小生成树Minimum Spanning Tree 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 树: 无回路   |V|个顶 ...

  9. BZOJ 1083: [SCOI2005]繁忙的都市(MST)

    裸的最小生成树..直接跑就行了 ---------------------------------------------------------------------- #include<c ...

随机推荐

  1. spring boot系列(四)spring boot 配置spring data jpa (保存修改删除方法)

    spring boot 使用jpa在pom.xml在上文中已经介绍过.在这里直接介绍各个类文件如何编写: 代码结构: domain(存放实体类文件): repository(存放数据库操作文件,相当于 ...

  2. linux下mysql定时备份,数据保存周期一周

    以下脚本来自网络,版权归原作者所有(推荐放在夜间自动备份,用cron制定计划任务) crontab -e 0 3 * * * /var/erp/data/mysql_backup.sh #!/bin/ ...

  3. EncryptionAndDecryptionC# 加密 解密

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. RazorSQL for Mac如何编辑数据?

    RazorSQL 是一个非开源的功能非常强大数据库查询工具.SQL的编辑.数据库管理工具.支持通过 JDBC 和 ODBC 连接超过 29 种的数据库.允许您从一个数据库工具查询,更新,导航和管理所有 ...

  5. ERROR】Unable to open underlying table which is differently defined or of non-MyISAM type or ...

    Error: Unable to open underlying table which is differently defined or of non-MyISAM type or doesn’t ...

  6. mac环境提示:make sure that /usr/local/bin is in your path

    今天我在Mac环境下安装了Homebrew和node.js后,输入node -v.brew.npm都提示: command not found,然后我查看了下Homebrew的安装日志,发现日志里有个 ...

  7. PTA(Basic Level)1046.划拳

    划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒.两人同赢或两人同输 ...

  8. Python内置函数Object

    英文文档 class object Return a new featureless object. object is a base for all classes. It has the meth ...

  9. JAVA基础--JAVA API常见对象(字符串&缓冲区)

    一. String 类型 1. String类引入 第二天学习过Java中的常量:   常量的分类:   数值型常量:整数,小数(浮点数) 字符型常量:使用单引号引用的数据 字符串常量:使用双引号引用 ...

  10. Mysql workbench 字段类型(转载)

    转载自:https://blog.csdn.net/j_h_xie/article/details/52924521 项目初始,在使用workbench建表时,字段中有PK,NN,UQ,BIN,UN, ...