生成最小树是指在一个无向连通图上选择适当的边,生成一个子连通图满足全部边的权重加起来最小的条件
应用:给出n个城市和各个城市间的距离,问若要修建若干个公路连通所有城市,问应该如何修建和这时的公路长度。。。
以下是经 黑车司机 总结和优化的最小树算法
用到的数据结构
1.node_set[i] ,节点i所属的集合,刚开始是i
2.size[i],集合i包含的节点,刚开始是1
3.edge[i], edge结构保存无向边的信息,包括两边的节点和边的权重
大概流程:
1.初始化数据结构
2.对edge数组进行升序排序
3.遍历edge数组,如果edge[i]两边的节点来自不同的集合,则将这连个结合合并起来,
直至全部节点都属于同一个集合。
代码和注释:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 100;
int node_set[maxn]; //the set node i belong to
int size[maxn]; //the munber of element that in set i
vector<int> ans; //save the edge that take accepted
struct Edge{
int node1;
int node2;
int val;
}edge[maxn];