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算法松弛的同时求得 ...
随机推荐
- php-fpm的status可以查看汇总信息和详细信息
nginx.conf 配置文件 server { listen ; server_name localhost; index index.php index.html; root /home/tiny ...
- spring+spring mvc+JdbcTemplate 入门小例子
大家使用这个入门时候 最好能够去 搜一下 spring mvc 的 原理,我放一张图到这里,自己琢磨下,后面去学习就容易了 给个链接 (网上一把,千万不能懒) https://www.cnblo ...
- js截取图片上传(仅原理)----闲的无聊了代码就不共享了!写的难看,不好意思给你们看了(囧)
就算世界再坑爹,总有一些属性能带你走出绝望(伟大的absolute) 今天吐槽一下!......在我的世界里没有正统UI,所以效果图永远都是那么坑爹! 这里我要感谢有个position:absolut ...
- asp.net(c#)中相对路径(虚拟路径)和物理磁盘路径的转换
物理路径:磁盘路径,也就是在磁盘上的位置. 虚拟路径:web页面上的路径,是相对于应用程序而言的. /// 将物理路径转换成相对路径 /// </summary> ...
- 爬虫笔记之w3cschool注册页面滑块验证码破解(巨简单滑块位置识别,非鼠标模拟轨迹)
一.背景介绍 最开始接触验证码破解的时候就是破解的w3cschool的使用手机号找回密码页面的验证码,详见:验证码识别之w3cschool字符图片验证码(easy级别),这次破解一下他们注册页面的滑块 ...
- G. (Zero XOR Subset)-less(线性基)
题目链接:http://codeforces.com/contest/1101/problem/G 题目大意:给你n个数,然后让你把这n个数分成尽可能多的集合,要求,每个集合的值看做这个集合所有元素的 ...
- mysql学习------MySQL慢查询日志
一.什么是慢查询日志 1.当查询超过一定时间没有返回结果的时候,才会记录进慢查询日志 2.慢查询日志可以帮助dba找出执行效率缓慢的sql语句,为数据库的优化工作提供帮助 3.慢查询日志默认是不开启的 ...
- 大数据系列之并行计算引擎Spark部署及应用
相关博文: 大数据系列之并行计算引擎Spark介绍 之前介绍过关于Spark的程序运行模式有三种: 1.Local模式: 2.standalone(独立模式) 3.Yarn/mesos模式 本文将介绍 ...
- mysql5.7.20:安装教程
从mysql官网下载安装包:/mysql-5.7.20-linuxglibc2.12-x86_64.tar.gz #切换目录 cd /usr/local #解压下载的安装包 tar -zxvf /so ...
- android 通用 Intent
通用 Intent 本文内容显示详细信息 闹钟 日历 相机 联系人/人员应用 电子邮件 文件存储 本地操作 地图 音乐或视频 新笔记 电话 搜索 设置 发送短信 网络浏览器 使用 Android 调试 ...