problem

#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 ;
const int N = 2300 + 5 ;
const int M = 400000 + 5 ;
struct node {
int u ;
int v ;
int w ;
};
node edge[M] ;
int fa[N] ;
int ans = 0 ;
bool cmp(node x,node y) {
return x.w < y.w ;
}
inline int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]) ;
}
inline void kruskal() {
sort(edge+1,edge+m+1,cmp) ;
for(register int i=1;i<=m;i++){
int x = find(edge[i].u) , y = find(edge[i].v) ;
if(x == y) continue ;
fa[x] = y , ans += edge[i].w ;
}
}
signed main() {
n = In() ; m = In() ;
for(register int i=1;i<=n;i++) fa[i] = i ;
for(register int i=1;i<=m;i++) {
int u = In() , v = In() , w = In() ;
edge[i] = node{u,v,w} ;
}
kruskal() ;
cout << ans << endl ;
return 0 ;
}

随机推荐

  1. python3爬虫-通过requests爬取西刺代理

    import requests from fake_useragent import UserAgent from lxml import etree from urllib.parse import ...

  2. 单例模式(C#实现)

    这是这段时间学习设计模式的时候的源代码. 单例(单件)模式的五种实现. 通过一个计数器的例子调用验证一下. 把下面的代码直接拷进vs下,运行就可以了.(控制台应用程序) 以后把剩余的设计模式有空儿就粘 ...

  3. [COGS309] [USACO 3.2] 香甜的黄油

    ★★   输入文件:butter.in   输出文件:butter.out   简单对比 时间限制:1 s   内存限制:128 MB 描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖 ...

  4. 【GC概述以及查看堆内存使用】Java内存管理和GC学习

    内存划分 1.JAVA内存主要划分为方法栈.方法区.堆. 2.方法栈上内存会自动释放: 3.方法区上主要加载了类的元信息.静态变量.常量.改区域又称为持久代(Perm Gen),默认是最小16M,最大 ...

  5. JAVA NIO 之 Selector 组件

    NIO 重要功能就是实现多路复用.Selector是SelectableChannel对象的多路复用器.一些基础知识: 选择器(Selector):选择器类管理着一个被注册的通道集合的信息和它们的就绪 ...

  6. Servlet监听器(Listener)实例

    以下内容是翻译自http://www.journaldev.com/1945/servletcontextlistener-servlet-listener-example: 说明:web.xml的加 ...

  7. SfM执行流程

    整个过程根据脚本执行过程来分析. 首先我们看到RunBundler.sh,这个shell脚本. 1.定义参数 BASE_PATH="/cygdrive/e/ProjectBefore/Lea ...

  8. maven bug之Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project acSpaceCommon: Fatal error compiling: tools.jar not found: C:\Program Files\J

    maven打包项目的时候一直报这个异常  一般的解决办法我都试过 在pom.xml加代码 也不行  只有10分了  求大神解答 这是因为测试代码时遇到错误,它会停止编译.只需要在pom.xml的< ...

  9. VMware 9 安装 Mac OS X 10.8 Mountain Lion 图文全程

    http://unmi.cc/vmware9-install-mac-os-x-mountain-lion 非常详细,赞一下 本教程是在 VMware 9 下安装当前最新版的 Mac OS X Mou ...

  10. 46.Android 自己定义Dialog

    46.Android 自己定义Dialog Android 自己定义Dialog 前言 提示Dialog 提示Dialog 效果图 菜单Dialog 菜单Dialog 效果图 DialogActivi ...