\(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. Missing message for key "xxx" in bundle "(default bundle)" for locale zh_CN

    参考文章http://programmerslounge.blogspot.com/2013/03/error-missing-message-for-key.html 错误的struts-confi ...

  2. cmd杀死进程

    打开cmd 1.查看所有进程占用的端口 输入:netstat –ano(查看所有进程,查找相应占用端口的程序的pid) 直接查看占用指定端口的程序的pid 输入:netstat -ano|findst ...

  3. java 反射运用

    一,获取私有的属性,方法,构造器(俗名:暴力反射) 现有一个类,属性,方法,构造器均为私有的,如何创建实例对象,更该属性值,调用方法? public class Student { private S ...

  4. 别墅房间 CodeVS原创

    时间限制: 1 s 空间限制: 64000 KB 题目等级 : 青铜 Bronze 题目描述 Description 小浣熊松松到他的朋友家别墅去玩,发现他朋友的家非常大,而且布局很奇怪.具体来说,朋 ...

  5. Linux下汇编语言学习笔记70 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  6. iOS消息推送原理和实现总结

    一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图:1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provi ...

  7. Performance Tunning - OCP

    This artical is forcused on Oracle 11g Release 2.  It is an summary from the OCP documentation. The ...

  8. CentOS 7: Install vsftpd

    Install vsftpd All commands should be run with ‘root’ user. Run the following command in terminal to ...

  9. ssh连接失败

    参考:http://www.cnblogs.com/starof/p/4709805.html https://www.chenyudong.com/archives/ssh-using-privat ...

  10. 关于NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

    NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary 在 OC 中我们天天都要用,而我们要怎 ...