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 ...
随机推荐
- SQL Server merge用法
有两个表名:source 表和 target 表,并且要根据 source 表中匹配的值更新 target 表. 有三种情况: source 表有一些 target 表不存在的行.在这种情况下,需要将 ...
- mvc5中webapi的路由
1.Global.asax中路由的注册 public class WebApiApplication : System.Web.HttpApplication { protected void App ...
- mvc控制器接收ajax传送的数据
视图层中ajax传数据 $.ajax({ type: "post",//提交方式 data: { complay_arry: complay_arry, site_arry: si ...
- 第一阶段:Java基础 1.JAVA开发介绍---1.常用的DOS命令
一,DOS使用常识 DOS的概况:DOS(Disk Operating System)是一个使用得十分广泛的磁盘操作系统.DOS的概况 常见的DOS有两种:IBM公司的PC-DOS和微软公司的MS-D ...
- Java 9新特性
1.jdk 目录结构 JDK9 具体目录结构如下所示: bin: 该目录包含所有的命令. conf: 包含用户可以编辑的配置文件,例如以前位于 jre\lib 目录中的.properties 和 .p ...
- win10桌面左下角搜索框无法搜索解决办法
方法1.首先看下window search服务是不是被禁止或者停止运行了,如果停止了,就重新启动看看. 方法2.如果上面的方法还没有解决的话:任务管理器-详细信息--结束explorer.exe进程- ...
- iOS相关
1. fastlane a collection of tools that help you automate building and releasing iOS and Android apps ...
- vscode vue 去掉语法提示
在vscode中,点击file->preferences->settings, 然后输入vetur, 滚到最下面,那个勾去掉,然后关闭,重启vscode就可以了
- Kubernetes port-forward
命令格式: kubectl port-forward <pod_name> <forward_port> --namespace <namespace> --add ...
- MySQL数据库(四)—— 记录相关操作之插入、更新、删除、查询(单表、多表)
一.插入数据(insert) 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); # 后面的值必须与字段 ...