跟D题一样,还简单一些
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=1000+10;
int par[maxn];
struct node{
int u,v,len;
friend bool operator <(node a,node b){
return a.len>b.len;
}
};
int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
}
int main()
{
int t,n;
//freopen("I.txt","r",stdin);
while(scanf("%d",&t)!=EOF)
{
node s;
priority_queue<node> pq;
for(int i=1;i<=t;i++)
{
s.u=i;
par[i]=i;
for(int j=1;j<=t;j++)
{
s.v=j;
scanf("%d",&s.len);
pq.push(s);
}
}
node th;int ans=0;
while(!pq.empty())
{
th=pq.top();pq.pop();
int u=find(th.u),v=find(th.v);
if(u!=v)
{
par[u]=v;
ans+=th.len;
}
}
printf("%d\n",ans);
}
return 0;
}