【BZOJ3714】Kuglarz

题面

http://darkbzoj.tk/problem/3714

题解

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ri register int
#define N 2050
using namespace std;
struct edge{
  int u,v,l;
  bool operator < (const edge& rhs) const {
    return l<rhs.l;
  }
} e[N*N];
int f[N];
int n;
int findroot(int x){
  if (f[x]==x) return x;
  return f[x]=findroot(f[x]);
}
int main(){
  int x,cc=0;
  scanf("%d",&n);
  for (ri i=1;i<=n;i++) 
    for (ri j=i;j<=n;j++) {
      scanf("%d",&x);
      e[++cc]=(edge){i,j+1,x};
    }
  sort(e+1,e+cc+1);
  int tot=n+1;
  for (ri i=1;i<=n+1;i++) f[i]=i;
  long long ans=0;
  for (ri i=1;i<=cc;i++) {
    int u=findroot(e[i].u),v=findroot(e[i].v);
    if (u!=v) {
      ans+=e[i].l;
      f[u]=v;
      tot--;
      if (tot==1) {
        printf("%lld\n",ans);
        return 0;
      }
    }
  }
  return 0;
}

 

posted @ 2019-07-31 11:59  HellPix  阅读(133)  评论(0编辑  收藏  举报