51Nod 1212 无向图最小生成树 (路径压缩)
第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量。(2 <= N <= 1000, 1 <= M <= 50000)
第2 - M + 1行:每行3个数S E W,分别表示M条边的2个顶点及权值。(1 <= S, E <= N,1 <= W <= 10000)
输出最小生成树的所有边的权值之和。
9 14
1 2 4
2 3 8
3 4 7
4 5 9
5 6 10
6 7 2
7 8 1
8 9 7
2 8 11
3 9 2
7 9 6
3 6 4
4 6 14
1 8 8
37 题解:还是套模板 但是之前边数开小了哇了半天 (测试了几次还是开挂的快)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
bool cmp(int x,int y)
{
return x>y;
}
const int N=;
const int mod=1e9+;
int f[N];
struct edge
{
int u,v,w,flag;
}a[N];
void init()
{
for(int i=;i<=N;i++)
f[i]=i;
}
int find1(int x)
{
if(x!=f[x])
f[x]=find1(f[x]);
return f[x];
}
bool cmp1(edge a,edge b)
{
return a.w<b.w;
}
int main()
{
std::ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
init();
for(int i=;i<m;i++)
cin>>a[i].u>>a[i].v>>a[i].w;
sort(a,a+m,cmp1);
int s=;
for(int i=;i<m;i++){
int u=a[i].u,v=a[i].v,w=a[i].w;
if(find1(u)==find1(v)) continue;
f[find1(u)]=find1(v);
s+=w;
}
cout<<s<<endl;
return ;
}
51Nod 1212 无向图最小生成树 (路径压缩)的更多相关文章
- 51Nod 1212无向图最小生成树
prim #include<stdio.h> #include<string.h> #define inf 0x3f3f3f3f ][]; ],lowc[]; ],int n) ...
- 51nod 1212 无向图最小生成树(Kruskal模版题)
N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. Input 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N <= 1000, 1 &l ...
- (图论)51NOD 1212 无向图最小生成树
N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. 输入 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N <= 1000, 1 <= M ...
- 51 nod 1212 无向图最小生成树(Kruckal算法/Prime算法图解)
1212 无向图最小生成树 N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. 收起 输入 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N < ...
- 51 nod 1212 无向图最小生成树
http://www.51nod.com/Challenge/Problem.html#problemId=1212 代码 #include<bits/stdc++.h> using na ...
- 51Nod-1212 无向图最小生成树
51Nod: 1212 无向图最小生成树. link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1212 1212 ...
- 51nod1212无向图最小生成树
1212 无向图最小生成树 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. Inpu ...
- poj1703Find them, Catch them(并查集以及路径压缩)
/* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...
- [HDOJ2818]Building Block(带权并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...
随机推荐
- 用laravel dingo/api创建产品api
沿着上一篇来讲,我们来创建一个简单的item产品api,也是用到laravel dingo/api来实现,对dingo/api不熟的朋友可以翻看前面的文章.好,我们随着ytkah一起来创建产品api ...
- The Swift Programming Language 中文版
http://numbbbbb.github.io/the-swift-programming-language-in-chinese/
- 小程序编辑器vscode
安装中文版 1)打开vscode工具: 2)使用快捷键组合[Ctrl+Shift+p],在搜索框中输入“configure display language”,点击确定后: 3)如图所示 =>安 ...
- 静态库lib和动态库dll相关总结
1.静态链接库LIB和动态链接库DLL的区别 若采用静态链接库,lib 中的指令都全部被直接包含在最终生成的 EXE 文件中了.而动态动态链接库则不必被包含在最终 EXE 文件中,EXE 文件执行时可 ...
- dataframe操作
1.去除重复项drop_duplication #去除重复项drop_duplication import pandas as pd df = pd.DataFrame({"col1&quo ...
- eos 空投列表网址 及 工具网站列表
https://eosdrops.io https://www.shensi.com/#/eos eos 区块链浏览器: https://eostracker.io/ https://eospark. ...
- php-fpm sock 进程池优化
更改tcp为sock ;listen = 127.0.0.1:9000 listen = /dev/shm/php-cgi.sock pm = static \\静态进程池 pm.max_childr ...
- [LeetCode] questions conclusion_ Binary Search
Binary Search T(n) = T(n/2) + O(1) => T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...
- [LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL
Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Writ ...
- 遇到问题---hosts不起作用问题的解决方法
c:\WINDOWS\system32\drivers\etc\hosts 文件的作用是添加 域名解析 定向 比如添加 127.0.0.1 www.baidu.com 那我们访问www.baidu. ...