伪代码:
把所有边排序,记第i小的边为e[i] (1<=i<m)
初始化最小生成树为空
初始化连通分量,让每一个点自成连通分量
for(int i=0;i<m;i++)
{
if(e[i].u与e[i].v不在同一个连通分量)
{
把边e[i]加入到最小生成树中
合并e[i].u和e[i].v所在的连通分量
}
}
可用并查集优化
C++代码:
struct KRUSKAL
{
const int MAXN = 109;
const int MAXE = 5009;
struct EDGE
{
int u, v, length, choose;
} edge[ MAXE ];
int path[ MAXN ];
int N, edgecnt, sum;
void Addedge(int u, int v, int len)
{
++edgecnt;
edge[ edgecnt ].u = u;
edge[ edgecnt ].v = v;
edge[ edgecnt ].length = len;
edge[ edgecnt ].choose = false;
return ;
}
void Set()
{
for (int i = 1; i <= N; i++)
path[i] = i;
return ;
}
int Find_Path(int x)
{
if (x != path[x]) path[x] = Find_Path( path[x] );
return path[x];
}
int Work()
{
int cnt = 0, x, y;
Qsort(1, edgecnt); // i < j -> edge[i].length < edge[j].length
Set();
for (int i = 1; i <= E && cnt < N - 1; i++)
{
x = Find_Path( edge[i].u );
y = Find_Path( edge[i].v );
if (x == y) continue;
path[x] = path[y];
edge[i].choose = true, ++cnt;
sum += edge[i].length;
}
return sum;
}
} Kruskal;
C++并查集优化代码:
#include <iostream>
using namespace std;
const int MAXV = 1024, MAXE = 100001;
int n, m, f[MAXV], ans, cnt;
struct edge{
int f, t, w;
}es[MAXE];
bool cmp(const edge &a, const edge &b){
return a.w < b.w;
}
void Fill(int &a){
static int cnt = 0;
a = ++cnt;
}
int get(int x){
return x == f[x] ? x : f[x] = get(f[x]);
}
void Kruskal(const edge &e){
if(get(e.f) != get(e.t)){
f[get(e.f)] = get(e.t);
ans += e.w;
cnt++;
}
}
void Read(edge &e){
cin>>e.f>>e.t>>e.w;
}
int main()
{
cin>>n>>m;
for_each(es+1, es+m+1, Read);
make_heap(es+1, es+m+1, cmp);
sort_heap(es+1, es+m+1, cmp);
for_each(f+1, f+n+1, Fill);
for_each(es+1, es+m+1, Kruskal);
cout<<(cnt < n-1 ? -1: ans)<<endl;
return 0;
}