比赛 板子大赛 评测结果 AAAAAAAAAA
题目名称 通信线路 最终得分 100
用户昵称 AeeE5x 运行时间 0.511 s
代码语言 C++ 内存使用 6.85 MiB
提交时间 2025-01-22 11:10:17
显示代码纯文本
#include<iostream> 
#include<algorithm>
using namespace std;
int n;long long ans;

struct edge{
    int x,y;
    long long val;
    bool operator<(const edge&qq)const{return val<qq.val;}
}e[4000010];int cnt=0;

int fa[2010];
inline int find(int x){
    if(fa[x]==x) return x;
    return fa[x]=find(fa[x]);
}
inline void merge(int x,int y){
    fa[x]=y;
}

int main(){
    freopen("mcst.in","r",stdin);
    freopen("mcst.out","w",stdout);
    
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            long long x;scanf("%lld",&x);
            if(x!=-1&&i<=j) e[++cnt]=(edge){i,j,x};
        }
    }
    for(int i=1;i<=n;i++) fa[i]=i;
    sort(e+1,e+1+cnt);
    
    for(int i=1;i<=cnt;i++){
        int x=e[i].x;
        int y=e[i].y;
        x=find(x);
        y=find(y);
        if(x!=y) merge(x,y),ans+=e[i].val;
    }
    printf("%lld",ans);
    
    
    
    return 0;
}