比赛 |
板子大赛 |
评测结果 |
AAAAAAAAAA |
题目名称 |
通信线路 |
最终得分 |
100 |
用户昵称 |
zqy |
运行时间 |
0.757 s |
代码语言 |
C++ |
内存使用 |
8.53 MiB |
提交时间 |
2025-01-22 09:42:42 |
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=1505,M=N*N;
struct edge{int u,v,w;}e[M];
int n,m,ans,f[N];
bool cmp(edge a,edge b){
return a.w<b.w;
}
int found(int x){
return f[x]==x?x:f[x]=found(f[x]);
}
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++){
int w;
scanf("%d",&w);
if(w!=-1)e[++m]=edge{i,j,w};
else e[++m]=edge{i,j,0x3f3f3f3f};
}
}
sort(e+1,e+1+m,cmp);
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=m;i++){
int u=found(e[i].u);
int v=found(e[i].v);
if(u==v)continue;
f[u]=v,ans+=e[i].w;
}
printf("%d\n",ans);
return 0;
}