#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N **
#define Max **
int father[N],j,sum,n;
typedef struct
{
int x;
int y;
int len;
}edge;
edge num[Max];
bool cmp(edge a,edge b)
{
return a.len<b.len;
}
int find(int x)
{
return father[x]==x?x:find(father[x]);
}
void Kruskal()
{
int i;
for(i=0;i<n;i++)
father[i]=i;
sort(num,num+j,cmp);
int a,b;
sum=0;
for(i=0;i<j;i++)
{
a=find(num[i].x);
b=find(num[i].y);
if(a!=b)
{
father[b]=a;
sum+=num[i].len;
}
}
}
int main()
{
Kruskal();
}
system("pause");
return 0;
}
最小生成树 kruskal的代码模版
最新推荐文章于 2023-08-11 09:46:24 发布