POJ-图论-最小生成树模板
POJ-图论-最小生成树模板
Kruskal算法
1.初始时所有结点属于孤立的集合。
2.按照边权递增顺序遍历所有的边,若遍历到的边两个顶点仍分属不同的集合(该边即为连通这两个集合的边中权值最小的那条)则确定该边为最小生成树上的一条边,并将这两个顶点分属的集合合并。
3.遍历完所有边后,原图上所有结点属于同一个集合则被选取的边和原图中所有结点构成最小生成树;否则原图不连通,最小生成树不存在。
数据结构:引入边结构,并重载小于号运算符
struct Edge
{
int a, b;//边的两端结点编号
int cost;//边的权值
bool operator <(const Edge &A)const
{
return cost < A.cost;//边权从小到大排列
}
}edge[];
用并查集来实现集合操作
void init()
{
for (int i = ; i <= n; i++)p[i] = i;
ans = ;
} int find(int x)
{
return (x == p[x]) ? x : p[x] = find(p[x]);
} void Union(int i)//以边为单位合并
{
int a = find(edge[i].a);
int b = find(edge[i].b);//查找边的两个顶点所在集合的信息
if (a != b) //若他们属于不同集合,则选用该边
{
p[b] = a;//合并集合
ans += edge[i].cost;//累加权值
}
}
例 5.3 还是畅通工程
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = ; int p[N];//父结点数组
int n;//结点数量
int ans;//最小权值和 struct Edge
{
int a, b;//边的两端结点编号
int cost;//边的权值
}edge[]; bool cmp(Edge a, Edge b)
{
return a.cost<b.cost;
} void init()
{
for (int i = ; i <= n; i++)p[i] = i;
ans = ;
} int find(int x)
{
return (x == p[x]) ? x : p[x] = find(p[x]);
} void Union(int i)//以边为单位合并
{
int a = find(edge[i].a);
int b = find(edge[i].b);//查找边的两个顶点所在集合的信息
if (a != b) //若他们属于不同集合,则选用该边
{
p[b] = a;//合并集合
ans += edge[i].cost;//累加权值
}
} int main()
{
while (scanf("%d", &n) != EOF && n != )
{
for (int i = ; i <= n * (n - ) / ; i++) scanf("%d%d%d", &edge[i].a, &edge[i].b, &edge[i].cost);
sort(edge + , edge + + n * (n - ) / , cmp);//起始元素为edge[1],一共n * (n - 1) / 2个待排序元素
init();
for (int i = ; i <= n * (n - ) / ; i++) Union(i);
printf("%d\n", ans);
}
return ;
}
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = ; int p[N];//父结点数组
int n;//结点数量
int ans;//最小权值和 struct Edge
{
int a, b;//边的两端结点编号
int cost;//边的权值
bool operator <(const Edge &A)const
{
return cost < A.cost;//边权从小到大排列
}
}edge[]; void init()
{
for (int i = ; i <= n; i++)p[i] = i;
ans = ;
} int find(int x)
{
return (x == p[x]) ? x : p[x] = find(p[x]);
} void Union(int i)//以边为单位合并
{
int a = find(edge[i].a);
int b = find(edge[i].b);//查找边的两个顶点所在集合的信息
if (a != b) //若他们属于不同集合,则选用该边
{
p[b] = a;//合并集合
ans += edge[i].cost;//累加权值
}
} int main()
{
while (scanf("%d", &n) != EOF && n != )
{
for (int i = ; i <= n * (n - ) / ; i++) scanf("%d%d%d", &edge[i].a, &edge[i].b, &edge[i].cost);
sort(edge + , edge + + n * (n - ) / );//起始元素为edge[1],一共n * (n - 1) / 2个待排序元素
init();
for (int i = ; i <= n * (n - ) / ; i++) Union(i);
printf("%d\n", ans);
}
return ;
}
重载Edge小于号
POJ-图论-最小生成树模板的更多相关文章
- poj 1258 最小生成树 模板
POJ 最小生成树模板 Kruskal算法 #include<iostream> #include<algorithm> #include<stdio.h> #in ...
- Poj 2187 凸包模板求解
Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...
- 图论-最小生成树<Kruskal>
昨天: 图论-最小生成树<Dijkstra,Floyd> 以上是昨天的Blog,有需要者请先阅读完以上再阅读今天的Blog. 可能今天的有点乱,好好理理,认真看完相信你会懂得 然而,文中提 ...
- POJ 2031 Building a Space Station 最小生成树模板
题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...
- 最小生成树模板题POJ - 1287-prim+kruskal
POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出 ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- POJ 1789 Truck History (Kruskal最小生成树) 模板题
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- POJ 1287 Networking (最小生成树模板题)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
随机推荐
- mvc5中webapi的路由
1.Global.asax中路由的注册 public class WebApiApplication : System.Web.HttpApplication { protected void App ...
- docker部署angular和asp.net core组成的前后端分离项目
最近使用docker对项目进行了改进,把步骤记录一下,顺便说明一下项目的结构. 项目是前后端分离的项目,后端使用asp.net core 2.2,采用ddd+cqrs架构的分层思想,前端使用的是ang ...
- crawler_app_在Airtest无线模式控制手机
无线模式开启: adb tcpip 48887 参考引用: https://www.cnblogs.com/xieqiankun/p/wireless-mode-of-poco.html
- 易百教程人工智能python补充-NLTK包
自然语言处理(NLP)是指使用诸如英语之类的自然语言与智能系统进行通信的AI方法. 如果您希望智能系统(如机器人)按照您的指示执行操作,希望听取基于对话的临床专家系统的决策时,则需要处理自然语言. N ...
- element表格的滚动条在合计上边
默认滚动条是在下边的,不好看,这里改一下 修改样式.完美解决: .el-table { overflow-x: auto; } .el-table__header-wrapper, .el-table ...
- Android 培训准备资料之project与module的区别(1)
project和module的区别? 现在我们来看看在Android studio中怎样新建一个project (1)file->new->new project. Application ...
- Markdown随笔
Markdown随笔 这两天兴致来了,想尝试一下使用 Markdown. 写这篇博客时我用的是: MarkdownPad 2 关于 MarkdownPad 2 的安装破解网上有很多教程了,这里我就不赘 ...
- 外汇盈利EA
>>>>>>>>>>>>>>>>>>>>>>>>> ...
- Elasticsearch 在 7.X版本中去除type的概念
背景说明 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全的搜索引擎库. El ...
- windows系统DOC命令启动或停止服务
-- 启动服务 -- net start Mysql -- 停止服务 -- net stop Mysql-- 说明:MysqL没有大小写区分.可以直接小写mysql. -- 启动报错(net star ...