#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
//---------------------------------Sexy operation--------------------------// #define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define debug(n) printf("%d_________________________________\n",n);
#define speed ios_base::sync_with_stdio(0)
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
//-------------------------------Actual option------------------------------//
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define Swap(a,b) a^=b^=a^=b
#define Max(a,b) (a>b?a:b)
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define mp(a,b) make_pair(a,b)
#define pb(n) push_back(n)
#define dis(a,b,c,d) ((double)sqrt((a-c)*(a-c)+(b-d)*(b-d)))
//--------------------------------constant----------------------------------// #define INF 0x3f3f3f3f
#define esp 1e-9
#define PI acos(-1)
using namespace std;
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef long long ll;
//___________________________Dividing Line__________________________________/ int n,m;
int father[1100000];
struct node
{
int x;
int y;
int k;
}Q[1100000];
int find(int x)
{
if(father[x]==x)
return x;
return father[x]=find(father[x]);
}
bool cmp(node a,node b)
{
return a.k<b.k;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
int cont=0,sum=0,st=0;
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&Q[i].x,&Q[i].y,&Q[i].k);
cont+=Q[i].k;
}
sort(Q,Q+m,cmp);
for(int i=1;i<=n;i++) father[i]=i;
for(int i=0;i<m;i++)
{
int tx=find(Q[i].x);
int ty=find(Q[i].y);
if(tx!=ty)
{
sum+=Q[i].k;
st++;
father[tx]=ty;
if(st==n-1)
break;
}
}
printf("%d\n",sum);
}
return 0;
}

图论--最小生成树--Kruscal 模板的更多相关文章

  1. 图论算法-Tarjan模板 【缩点;割顶;双连通分量】

    图论算法-Tarjan模板 [缩点:割顶:双连通分量] 为小伙伴们总结的Tarjan三大算法 Tarjan缩点(求强连通分量) int n; int low[100010],dfn[100010]; ...

  2. HDU 5253 最小生成树 kruscal

    Description 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行 ...

  3. 图论-最小生成树<Kruskal>

    昨天: 图论-最小生成树<Dijkstra,Floyd> 以上是昨天的Blog,有需要者请先阅读完以上再阅读今天的Blog. 可能今天的有点乱,好好理理,认真看完相信你会懂得 然而,文中提 ...

  4. hdu1162(最小生成树 prim or kruscal模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 意义:给出一些点,用线问使所有点直接或间接连通,需要多长: 思路:裸最小生成树: 法1: pri ...

  5. P3366 最小生成树【模板+Kruscal讲解】

    此题数组大小非常重要 算法过程: 现将全部边按照权值(由小到大)排序. 按顺序(同上)考虑每条边,只要这条边和之前已选择的边不构成圈,就保留这条边,否则放弃这条边. 具体算法 成功选择(n-1)条边后 ...

  6. 图论--最小生成树--Prim算法(带边输出)模板

    #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 100 ...

  7. 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...

  8. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

  9. 最小生成树——Kruscal(克鲁斯卡尔算法)

    一.核心思想 ​ 将输入的数据由小到大进行排序,再使用并查集算法(传送门)将每个点连接起来,同时求和. ​ 个人认为这个算法比较偏向暴力,有些题可能会超时. 二.例题 洛谷-P3366 题目地址:ht ...

随机推荐

  1. 7.1 java 类、(成员)变量、(成员)方法

    /* * 面向对象思想: * 面向对象是基于面向过程的编程思想. * * 面向过程:强调的是每一个功能的步骤 * 面向对象:强调的是对象,然后由对象去调用功能 * * 面向对象的思想特点: * A:是 ...

  2. Struts2-学习笔记系列(2)-常量配置和实现action

    1.常量配置 在struts配置文件中,下面突出部分,就是常量配置. <constant name="struts.enable.DynamicMethodInvocation&quo ...

  3. Docker命名空间

    命名空间 命名空间( namespace )是 Linux 内核的一个强大特性,为容器虚拟化的实现带来极大便利,利用这 特性,每个容器都可以拥有自己单独的命名空间,运行在其中的应用都像是在独立的操作系 ...

  4. Python——详解__slots__,property和私有方法

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Python专题的第11篇文章,我们来聊聊面向对象的一些进阶使用. __slots__ 如果你看过github当中一些大牛的代码,你会 ...

  5. 搭建Ubuntu虚拟机

    搭建Ubuntu虚拟机 前言 1. 啰嗦一下 1.1 ubuntu虚拟机的作用 1.2 为什么选择Ubuntu 1.3 工具准备 2. 正式开始 2.1 安装VMware 2.2 创建Ubuntu虚拟 ...

  6. Liunx常用操作(一)-删除命令

    在linux命令行模式下,如何一次性快速删除一行刚刚输入的命令? 经常在命令行输入命令的时候,一段文字都需要删除,一个字段一个字段,比较耗费时间 以下提供一些命令,配合在一起操作,可以一定程度提高工作 ...

  7. Java集合:ArrayList (JDK1.8 源码解读)

    ArrayList ArrayList几乎是每个java开发者最常用也是最熟悉的集合,看到ArrayList这个名字就知道,它必然是以数组方式实现的集合 关注点 说一下ArrayList的几个特点,也 ...

  8. AIX详细的VG,LV扩容步骤

    需求 1.归档日志刷得太快,经常把空间挤爆. 2.Oracle数据库表空间需要扩容 解决方案 1.先做重要数据备份 2.进行文件系统扩容 步骤 1. df -g 查找出/u01 对应的VG卷 VOLU ...

  9. RedHat 的 crontab

    Chapter 39. Automated Tasks In Linux, tasks can be configured to run automatically within a specifie ...

  10. SpringBoot @ConfigurationProperties详解

    文章目录 简介 添加依赖关系 一个简单的例子 属性嵌套 @ConfigurationProperties和@Bean 属性验证 属性转换 自定义Converter SpringBoot @Config ...