Kruskal 模板
最小生成树指的是在图上面找到权值最小的一棵树,并且保证图上所有的点都在这棵树上。
解决办法:Kruskal 算法(贪心思想)
将边按权值从小到大排序,然后按这个顺序不断连边,直到所有点联通。
/** * Fuck you. * I love you too. */ #include<bits/stdc++.h> #define lson i<<2 #define rson i<<2|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define enld endl #define mian main #define itn int #define prinft printf const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; const int EXP = 1e-8; const int N = 1e5 + 5; const int MOD = 1e9 + 7; const int MAXN = 1e5 + 5; using namespace std; int n; int ans; int par[N]; struct ed { int s, e, cost; } edge[MAXN]; bool cmp (ed a, ed b) { return a.cost < b.cost; } int Find (int a) { return a == par[a] ? a : par[a] = Find (par[a]); } void join (int a, int b) { par[Find (a)] = Find (b); } int main() { while (~scanf ("%d", &n)) { ans = 0; for (int i = 0; i < n; ++i) { scanf ("%d%d%d", &edge[i].s, &edge[i].e, &edge[i].cost); par[edge[i].s] = edge[i].s; par[edge[i].e] = edge[i].e; } sort (edge, edge + n, cmp); for (int i = 0; i < n; ++i) { if (Find (edge[i].s) != Find (edge[i].e)) { cout << edge[i].s << ' ' << edge[i].e << endl; join (edge[i].s, edge[i].e); ans += edge[i].cost; } } cout << ans << endl; } return 0; }
Hdu - 1162 Eddy's picture (完全图)
/** * Fuck you. * I love you too. */ #include<bits/stdc++.h> #define lson i<<2 #define rson i<<2|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define enld endl #define mian main #define itn int #define prinft printf const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; int n; int ans; int par[N]; struct ed { int s, e, cost; } edge[MAXN]; bool cmp (ed a, ed b) { return a.cost < b.cost; } int Find (int a) { return a == par[a] ? a : par[a] = Find (par[a]); } void join (int a, int b) { par[Find (a)] = Find (b); } int main() { while (~scanf ("%d", &n)) { ans = ; ; i < n; ++i) { scanf ("%d%d%d", &edge[i].s, &edge[i].e, &edge[i].cost); par[edge[i].s] = edge[i].s; par[edge[i].e] = edge[i].e; } sort (edge, edge + n, cmp); ; i < n; ++i) { if (Find (edge[i].s) != Find (edge[i].e)) { cout << edge[i].s << ' ' << edge[i].e << endl; join (edge[i].s, edge[i].e); ans += edge[i].cost; } } cout << ans << endl; } ; }
Hdu - 1233 还是畅通工程(将上面的模板添加一行即可AC)
/** * Fuck you. * I love you too. */ #include<bits/stdc++.h> #define lson i<<2 #define rson i<<2|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define enld endl #define mian main #define itn int #define prinft printf const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; int n; int ans; int par[N]; struct ed { int s, e, cost; } edge[MAXN]; bool cmp (ed a, ed b) { return a.cost < b.cost; } int Find (int a) { return a == par[a] ? a : par[a] = Find (par[a]); } void join (int a, int b) { par[Find (a)] = Find (b); } int main() { ) { ans = ; n = n * (n - ) / ; ; i < n; ++i) { scanf ("%d%d%d", &edge[i].s, &edge[i].e, &edge[i].cost); par[edge[i].s] = edge[i].s; par[edge[i].e] = edge[i].e; } sort (edge, edge + n, cmp); ; i < n; ++i) { if (Find (edge[i].s) != Find (edge[i].e)) { //cout << edge[i].s << ' ' << edge[i].e << endl; join (edge[i].s, edge[i].e); ans += edge[i].cost; } } cout << ans << endl; } ; }
Hdu - 1598 find the most comfortable road
并查集 + 枚举
先排序(从大到小,从小到大都行),再枚举所有的边,记录其中的最大最小值,更新答案。
/** * Fuck you. * I love you too. */ #include<bits/stdc++.h> #define lson i<<2 #define rson i<<2|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define enld endl #define mian main #define itn int #define prinft printf const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; int n, m, q; int Max, Min, ans; int x, y; int par[N]; struct ed { int s, e, cost; } edge[MAXN]; bool cmp (ed a, ed b) { return a.cost < b.cost; } int Find (int a) { return a == par[a] ? a : par[a] = Find (par[a]); } void join (int a, int b) { par[Find (a)] = Find (b); } void init() { ; i < m+; ++i) par[i] = i; } int main() { while (~scanf ("%d%d", &n, &m)) { mem (par, ); ; i < m; i++) { scanf ("%d%d%d", &edge[i].s, &edge[i].e, &edge[i].cost); } sort (edge, edge + m, cmp); scanf ("%d", &q); while (q--) { scanf ("%d%d", &x, &y); ans = INF; ; j < m; j++) { init(); Min = INF, Max = ; for (int i = j; i < m; i++) { if (Find (edge[i].s) != Find (edge[i].e)) { join (edge[i].s, edge[i].e); Min = min (Min, edge[i].cost), Max = max (Max, edge[i].cost); } if (Find (x) == Find (y)) { ans = min (ans, Max - Min); break; } } } if (ans == INF) cout << "-1" << endl; else { cout << ans << endl; } } } ; }
Kruskal 模板的更多相关文章
- Kruskal模板
Kruskal模板 struct Edge { int from,to,v; }edge[maxn*10]; int fa[maxn]; int n,m; int find(int x) { retu ...
- POJ 1287 Networking【kruskal模板题】
传送门:http://poj.org/problem?id=1287 题意:给出n个点 m条边 ,求最小生成树的权 思路:最小生树的模板题,直接跑一遍kruskal即可 代码: #include< ...
- 最小生成树kruskal模板
算法思路:每次选取权值最小的边,判断这两个点是否在同一个集合内,如果在则跳过,如果不在则加上这条边的权值 可以使用并查集储存结点,可以快速判断结点是否在同一集合内. #include<iostr ...
- 最小生成树prim和kruskal模板
prim: int cost[MAX_V][MAX_V]; //cost[u][v]表示边e=(u,v)的权值(不存在的情况下设为INF) int mincost[MAX_V]; //从集合X出发的每 ...
- HDU 2988 Dark roads(kruskal模板题)
Dark roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【还是畅通工程 HDU - 1233】【Kruskal模板题】
Kruskal算法讲解 该部分内容全部摘录自刘汝佳的<算法竞赛入门经典> Kruskal算法的第一步是给所有边按照从小到大的顺序排列. 这一步可以直接使用库函数 qsort或者sort. ...
- Prime算法 与 Kruskal算法求最小生成树模板
算法原理参考链接 ==> UESTC算法讲堂——最小生成树 关于两种算法的复杂度分析 ==> http://blog.csdn.net/haskei/article/details/531 ...
- 最小生成树(次小生成树)(最小生成树不唯一) 模板:Kruskal算法和 Prim算法
Kruskal模板:按照边权排序,开始从最小边生成树 #include<algorithm> #include<stdio.h> #include<string.h> ...
- 【次小生成树】【Kruskal】【prim】【转】
原博客出处:https://blog.csdn.net/yasola/article/details/74276255 通常次小生成树是使用Prim算法进行实现的,因为可以在Prim算法松弛的同时求得 ...
随机推荐
- 【CodeForces】925 C.Big Secret 异或
[题目]C.Big Secret [题意]给定数组b,求重排列b数组使其前缀异或和数组a单调递增.\(n \leq 10^5,1 \leq b_i \leq 2^{60}\). [算法]异或 为了拆位 ...
- 【转】2019年3月 最新win10激活密匙 win10各版本永久激活序列号 win10正式版激活码分享
现在市面上大致有两种主流激活方法,一种是通过激活码来激活,另外一种是通过激活工具来激活.但是激活工具有个弊端就是激活时间只有180天,很多网友都想要永久激活,现在已经过了win10系统免费推广期了,所 ...
- SQL Server 索引(一)数据结构和存储结构
本文关注以下方面(本文所有的讨论基于SQL Server数据库): 索引的分类: 索引的结构: 索引的存储 一.索引定义分类 让我们先来回答几个问题: 什么是索引? 索引是对数据库表中一列或多列的值进 ...
- innodb和myisam数据库文件存储详解以及mysql表空间
数据库常用的两种引擎有Innodb和Myisam,关于二者的区别参考:https://www.cnblogs.com/qlqwjy/p/7965460.html 1.关于数据库的存储在两种引擎的存储是 ...
- Space Replacement
Write a method to replace all spaces in a string with %20. The string is given in a characters array ...
- php扩展Redis功能
php扩展Redis功能 1 首先,查看所用php编译版本V6/V9 在phpinfo()中查看 2 下载扩展 地址:https://github.com/nicolasff/phpredis/dow ...
- vue 兼容IE报错解决方案
IE 页面空白 报错信息 此时页面一片空白 报错原因 Babel 默认只转换新的 JavaScript 语法(如箭头函数),而不转换新的 API ,比如 Iterator.Generator.Set. ...
- java 重新抛出异常
一.有时希望把刚捕获的异常重新抛出,尤其时在使用Exception捕获所以异常的时候,既然已经得到了对当前异常对象的引用,可以重新把它抛出: catch(Exception e){ System.ou ...
- (四)Dubbo Admin管理控制台
Dubbo Admin管理控制台 在dubbo2.6.0往后的版本在dubbo发布包中就没有dubbo-admin了,而是在incubator-dubbo-ops(https://github.com ...
- Tomcat启动默认访问项目
一般有两种可以实现:推荐使用这一种.更灵活 一般项目的编译项目都在Tomcat的webapps下,项目的访问路径一般为:http://localhost:8080/项目虚拟路径.但是Tomcat的默认 ...