标签:dash freopen stdout bre ios div strong print 之间
mcst.in
输出文件:mcst.out
简单对比时间限制:1.5 s 内存限制:128 MB
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; const int N=3000100; int fa[N>>2],w,js,answer,tot,n; struct node{ int x,y,dis; }E[N]; inline int read() { int x=0;int f=1;char c=getchar(); while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘,c=getchar(); return x*f; } inline bool cmp(node a,node b) { return a.dis<b.dis; } int getfa(int x) { return fa[x]==x?x:fa[x]=getfa(fa[x]); } int main() { freopen("mcst.in","r",stdin); freopen("mcst.out","w",stdout); n=read(); for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { w=read(); if(w!=-1&&j>i) E[++js].x=i, E[js].y=j, E[js].dis=w; } sort(E+1,E+js+1,cmp); for(int i=1;i<=js;i++) { int fx=getfa(E[i].x); int fy=getfa(E[i].y); if(fx!=fy) { tot++; fa[fx]=fy; answer+=E[i].dis; if(tot==n-1) break; } } printf("%d",answer); return 0; }
标签:dash freopen stdout bre ios div strong print 之间
原文地址:http://www.cnblogs.com/lyqlyq/p/7158590.html