代码如下
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=2000+5;
struct node{
int x,y,w;
}mp[maxn*maxn];
int n,f[maxn],tot;
long long cost;
int find(int x){
return f[x]==x ? x : f[x]=find(f[x]);
}
bool cmp(node a,node b){
return a.w<b.w;
}
int main(){
cost=0,tot=0,scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
scanf("%d",&mp[++tot].w),mp[tot].x=i,mp[tot].y=j+1;
sort(mp+1,mp+tot+1,cmp);//一定不能用priority_queue会TLE的
for(int i=1;i<=n+1;i++)
f[i]=i;
for(int i=1;i<=tot;i++)
if(find(mp[i].x)!=find(mp[i].y))
f[find(mp[i].x)]=find(mp[i].y),cost+=mp[i].w;
cout<<cost<<endl;
return 0;
}
by >o< neighthorn
408

被折叠的 条评论
为什么被折叠?



