http://www.51nod.com/Challenge/Problem.html#problemId=1212

代码

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
const int maxn=5e4+;
int par[maxn];
int rank1[maxn]; void f(int n) //初始化
{
for(int i=;i<n;i++)
{
par[i]=i;
rank1[i]=;
}
}
int find(int x)
{
if(par[x]==x)
{
return x;
}
else
{
return par[x]=find(par[x]);
}
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
return ;
if(rank1[x]<rank1[y])
{
par[x]=y;
}
else
{
par[y]=x;
}
if(rank1[x]==rank1[y])
rank1[x]++;
}
int n,m;
struct edge{
int x,y,cost;
}e[];
bool cmp(const edge e1,const edge e2)
{
return e1.cost<e2.cost;
}
int main()
{
cin>>n>>m;
f(n);
for(int i=;i<=m;i++)
{
cin>>e[i].x>>e[i].y>>e[i].cost;
}
ll res=;
sort(e+,e+m+,cmp);
for(int i=;i<m;i++)
{
edge e1=e[i];
if(find(e1.x)!=find(e1.y))
{
unite(e1.x,e1.y);
res=res+e1.cost;
}
}
cout<<res<<"\n";
return ;
}

51 nod 1212 无向图最小生成树的更多相关文章

  1. 51 nod 1212 无向图最小生成树(Kruckal算法/Prime算法图解)

    1212 无向图最小生成树 N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. 收起 输入 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N < ...

  2. 51Nod 1212 无向图最小生成树 (路径压缩)

    N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树.   Input 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N <= 1000, 1 &l ...

  3. 51Nod 1212无向图最小生成树

    prim #include<stdio.h> #include<string.h> #define inf 0x3f3f3f3f ][]; ],lowc[]; ],int n) ...

  4. 51nod 1212 无向图最小生成树(Kruskal模版题)

    N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树.   Input 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N <= 1000, 1 &l ...

  5. (图论)51NOD 1212 无向图最小生成树

    N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. 输入 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N <= 1000, 1 <= M ...

  6. 51Nod-1212 无向图最小生成树

    51Nod: 1212 无向图最小生成树. link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1212 1212  ...

  7. 51nod1212无向图最小生成树

    1212 无向图最小生成树 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树.   Inpu ...

  8. 51 nod 1439 互质对(Moblus容斥)

    1439 互质对 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 有n个数字,a[1],a[2],…,a[n].有一个集合,刚开 ...

  9. 51 nod 1495 中国好区间

    1495 中国好区间 基准时间限制:0.7 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   阿尔法在玩一个游戏,阿尔法给出了一个长度为n的序列,他认为,一段好的区间,它的长度是& ...

随机推荐

  1. C++ substr 截取子串

    1. substr() 方法使用 string substr (size_t pos = 0, size_t len = npos) const;pos: 截取初始位置(从头开始截取pos=0)len ...

  2. StarUML之二、StarUML初识

    为什么用StarUML UML建模工具比较常见的PowerDesigner ROSE StarUML starUML-开源免费(1-2百M),PowerDesigner-精细和一体化(6-7百M),R ...

  3. redis的基础知识

    select切换数据库 remoteSelf:0>select 0 "OK" dbsize查看当前数据库的key数量 remoteSelf:0>dbsize " ...

  4. windows7精简iso

    网站地址: https://www.90pan.com/b1268487 迅雷链接: magnet:?xt=urn:btih:EE4B0FE780B6EC97D6FB9A9D22A8EE1627DA7 ...

  5. 在本地搭建git服务器

    GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业公司来说,既不想公开源代码,又舍不得给GitHub交保护费,那就只能自己搭建一台Git服务器作为私有仓库使用. 搭建Gi ...

  6. 物联网开源框架Thingsboard使用总结

    Thingsboard中文社区:http://thingsboard.iotschool.com/ 参考网址:https://thingsboard.io/docs/getting-started-g ...

  7. LAMP环境搭建+配置虚拟域名

    Centos下PHP,Apache,Mysql 的安装 安装Apache yum -y install httpd systemctl start httpd 添加防火墙 firewall-cmd - ...

  8. JAVA JDBC 连接数据库

    方式一 Driver driver = new com.mysql.jdbc.Driver(); String url = "jdbc:mysql://localhost:3306/test ...

  9. string_random

    1.随机数 import random 0-1间的随机浮点数,random.random() 指定区间随机浮点数,random.uniform(a,b) 指定区间随机整数(闭区间),random.ra ...

  10. MySQL 8 在一台机器上运行多个MySQL实例

    可以为每个实例使用一个MySQL Server二进制程序,也可以为不同实例使用同一个MySQL Server二进制程序. 不管哪一种选择,部分参数可能需要不同配置,以避免多个实例之间的冲突. 可能需要 ...