记录编号 598230 评测结果 AAAAAAWWWA
题目名称 通信线路 最终得分 70
用户昵称 Gravatar李奇文 是否通过 未通过
代码语言 C++ 运行时间 1.049 s
提交时间 2025-01-22 17:38:15 内存使用 6.03 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
int n,m,ans,f[514514];
struct node{
    int x,y,w;
}a[514514];
bool cmp(node a,node b){return a.w<b.w;}
int find(int x){return (f[x]==x)?x:find(f[x]);}
int main(){
    freopen("mcst.in","r",stdin);
    freopen("mcst.out","w",stdout); 
    std::cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            int tb;
            std::cin>>tb;
            if(tb!=-1) a[++m].x=i,a[m].y=j,a[m].w=tb;
        }
    }
    for(int i=1;i<=n;i++) f[i]=i; 
    sort(a+1,a+m+1,cmp);
    for(int i=1;i<=m;i++){
        int cs1=find(a[i].x),cs2=find(a[i].y);
        if(cs1!=cs2) ans+=a[i].w,f[cs2]=cs1;
    }
    std::cout<<ans<<endl;
    return 0;
}