POJ 1258
来源:互联网 发布:google马赛克还原软件 编辑:程序博客网 时间:2024/05/17 23:46
//11132194c00h00g1258Accepted764K16MSG++1238B2012-12-23 16:07:01#include<stdio.h>#include<stdlib.h>#include<queue>#include<algorithm>using namespace std;int N;struct Edge{ int from,to,w; Edge(int x,int y,int z):from(x),to(y),w(z){} bool operator<(const Edge& rhs) const{ return w>rhs.w; }};priority_queue<Edge> q;int parent[105];void makeset(int i){parent[i]=i;}int findset(int i){return i!=parent[i]?parent[i]=findset(parent[i]):i;}void unionset(int x,int y){ if(x<y) parent[y]=x; else parent[x]=y;}__int64 kruskal(){ __int64 res=0; for(int i=0;i<N;i++) makeset(i); while(!q.empty()){ Edge edge=q.top();q.pop(); int x=findset(edge.from); int y=findset(edge.to); if(x!=y){ res+=edge.w; unionset(x,y); } } return res;}int main(){ while(scanf("%d",&N)!=EOF){ while(!q.empty()) q.pop(); for(int i=0;i<N;i++) for(int j=0;j<N;j++){ int weight; scanf("%d",&weight); if(i<=j) q.push(Edge(i,j,weight)); } printf("%I64d\n",kruskal()); } return 0;}