\(problem\)

\(kruskal\)的模板题。

#ifdef Dubug

#endif
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
inline LL In() { LL res(0),f(1); register char c ;
while(isspace(c=getchar())) ; c == '-'? f = -1 , c = getchar() : 0 ;
while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ;
return res * f ;
}
int n , m , k ;
const int N = 100000 + 5 ;
int fa[N] ;
struct node{
int u ;
int v ;
int w ;
};
node edge[N] ;
LL ans(0) ;
bool cmp(node x,node y) {
return x.w > y.w ;
}
inline int find(int x) {
return fa[x] == x ? fa[x] : fa[x] = find(fa[x]) ;
}
inline void merge(int x,int y) {
fa[find(x)] = find(y) ;
}
inline void kruskal() {
sort(edge+1,edge+m+1,cmp) ;
int cnt (0) ;
for(register int i=1;i<=m;i++) {
if(find(edge[i].u) == find(edge[i].v)) continue ;
merge(edge[i].u,edge[i].v) ;
ans += edge[i].w ;
if(++cnt == k) return ;
}
}
signed main() {
n = In() , m = In() , k = In() ;
for(register int i=1;i<=n;i++) fa[i] = i ;
for(register int i=1;i<=m;i++) {
int u , v , w ;
u = In() , v = In() , w = In() ;
edge[i] = node{u,v,w} ;
}
kruskal() ;
cout << ans << endl ;
return 0 ;
}

随机推荐

  1. 热词解析(9) — hangry

    今天给大家介绍一个非常有趣.又超级实用的词!!中文叫"饿极而怒",英文叫... 不知道你有没有这样的经历,当你饿着肚子等着你妈做饭,结果你妈却在麻将桌上不下来,你就越来越饿,越饿越 ...

  2. 设置Python解析器

    如果同时安装了多个Python,如 Python2.7 和 Python3.7 .如果某些特殊原因(比如有些框架只能在Python2.7中使用),需要修改程序在 Python2.7 下运行,即可设置P ...

  3. Jmeter使用笔记之断言

    前言 Jmeter的断言方式有很多种,由于在工作中经常做的是API接口测试,所以这篇文章主要介绍如何对接口的字段进行解析,如何对解析出来的字段的值断言 了解API接口 Restful API 规范 协 ...

  4. Python随笔day03

    温故知新: 注释: 单行注释   # 多行注释   ‘’’ ‘’’  或者  “””  “”” 注意:三个单引号或双引号可以用于表示多行字符串. 判断输入的字符串是否是数字 salary = inpu ...

  5. Vue.Draggable实现拖拽效果(采坑小记)

    之前有写过Vue.Draggable实现拖拽效果(快速使用)(http://www.cnblogs.com/songdongdong/p/6928945.html)最近项目中要用到这个拖拽的效果,当产 ...

  6. Spring AOP学习(六)

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. 在eclipse中如何在大量项目中查找指定文件

    在eclipse中如果希望在大量的项目中寻找指定的文件可不是一件轻松的事,还好eclipse提供了强大的搜索功能. 我们可以通过通配符或正则表达式来设定查寻条件,下面是操作示例: ctrl+h 打开搜 ...

  8. Oracle学习总结(4)——MySql、SqlServer、Oracle数据库行转列大全

    MySql行转列 以id分组,把name字段的值打印在一行,逗号分隔(默认) select CustomerDrugCode,group_concat(AuditItemName) from noau ...

  9. cmd界面中断一个程序快捷键 ctrl+c

    cmd界面中断一个程序快捷键   ctrl+c

  10. C. Painting Fence 分治

    memory limit per test 512 megabytes input standard input output standard output Bizon the Champion i ...