$P2121 拆地毯$
\(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 ;
}
随机推荐
- ubuntu14.04 fcitx安装
先卸载ibus sudo apt-get remove ibus (也可尝试不卸载ibus,直接安装fcitx) 添加源 sudo add-apt-repository ppa:fcitx-team/ ...
- Tornado进阶
三.Tornado进阶 3.1 Application settings debug,设置tornado是否工作在调试模式,默认为False即工作在生产模式.当设置debug=True 后,torna ...
- Cmake的介绍和使用 Cmake实践
Cmake的介绍和使用 Cmake实践http://www.cppblog.com/Roger/archive/2011/11/17/160368.html
- MessageFormat理解,MessageFormat.format(Object obj)方法
MessageFormat.format(Object obj)方法主要用途为拼接message信息 用法: Object[] testArgs = {new String("张三" ...
- MVC系统学习2—MVC路由
在MVC下不是通过对物理文件的映射来实行访问的,而是通过定义后的路由Url来实现访问的.在前一篇讲到我们是在全局文件下进行路由配置. routes.MapRoute( & ...
- How to put username &password in MongoDB(Security&Authentication)?(配置用户认证在MongoDB)
Default do not need username and password authenticate when access mongoDB ,I want to set up the use ...
- poj 1562 简单深搜
//搜八个方向即可 #include<stdio.h> #include<string.h> #define N 200 char ma[N][N]; int n,m,vis[ ...
- mtk display 架构
Hwc Dispatch Layers .Layer 的区分 每个layer 都会有对应的一个BufferQueue,BufferQueue都有一个mConnectapi属性,mConnectAPI ...
- BZOJ3991 寻宝游戏 LCA 虚树 SET
5.26 T1:寻宝游戏 Description 小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择一个村庄, ...
- Hyper-v交换机添加出错解决方法
这个问题中文找不到解决方法,只能重新安装系统了 重新安装系统后,按照顺序操作,不要去动设备管理器中的东西,也不用动驱动,先删除虚拟机的网络连接,在删除虚拟交换机就可以了,没有再出现问题