标签:include ring space max clu turn using std printf
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5+10;
int fa[maxn];
struct Edge{
int from;
int to;
int w;
}edge[maxn];
bool cmp(Edge a,Edge b){
return a.w < b.w;
}
int find(int x){
return (x == fa[x]) ? x : (fa[x] = find(fa[x]));
}//找到节点祖先并且压缩路径
void Merge(int x,int y){
fa[find(x)] = find(y);
}//判断两个节点是否为同一个祖先
int tot;
void add(int x,int y,int z){
edge[++tot].from = x;
edge[tot].to = y;
edge[tot].w = z;
}
int Solve(int n,int m){
sort(edge+1,edge+1+m,cmp);
int cnt = 0;
int ans = 0;
for(int i = 1;i <= m;++i){
int x = edge[i].from,y = edge[i].to;
if(find(x) != find(y)){
Merge(x,y);
ans += edge[i].w;
if(++cnt == n - 1)return ans;//cnt 用来记录已经添加了的边的条数,最后跳出循环表示已经添加了n - 1条边,即树已经完成
}
}
return -1;//找不到最小生成树,即图不联通
}
int main(){
int n;
scanf("%d",&n);
for(int i = 1;i <= n;++i)fa[i] = i;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
int dis;
scanf("%d",&dis);
add(i,j,dis);
}
}
int ans = Solve(n,n*n);
printf("%d",ans);
return 0;
}
//感到了最小生成树深深的恶意
标签:include ring space max clu turn using std printf
原文地址:https://www.cnblogs.com/2004-08-20/p/12318293.html