标签:http printf das round pen print 生成 can std
★★ 输入文件:mcst.in
输出文件:mcst.out
简单对比
时间限制:1.5 s 内存限制:128 MB
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define MAXN 2325750 #define M 2510 using namespace std; int n,tot,fa[M],num,ans; struct nond{ int x,y,z; }edge[MAXN]; int cmp(nond a,nond b){ return a.z<b.z; } int find(int x){ if(fa[x]==x) return fa[x]; else return fa[x]=find(fa[x]); } int main(){ freopen("mcst.in","r",stdin); freopen("mcst.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ int x; scanf("%d",&x); if(x!=-1){ edge[++tot].x=i; edge[tot].y=j; edge[tot].z=x; } } sort(edge+1,edge+1+tot,cmp); for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=tot;i++){ int dx=find(edge[i].x); int dy=find(edge[i].y); if(dx==dy) continue; fa[dx]=dy; num++; ans+=edge[i].z; if(num==n-1) break; } printf("%d",ans); }
标签:http printf das round pen print 生成 can std
原文地址:http://www.cnblogs.com/cangT-Tlan/p/7416297.html