记录编号 |
598216 |
评测结果 |
AAAAAAAAAA |
题目名称 |
通信线路 |
最终得分 |
100 |
用户昵称 |
李金泽 |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.706 s |
提交时间 |
2025-01-22 17:20:41 |
内存使用 |
11.59 MiB |
显示代码纯文本
#include<cstdio>
#include<algorithm>
#define N 1505
#define int long long
using namespace std;
int n,m,f[N],x,ans;
struct edge{int u,v,w;bool operator<(edge y){return w<y.w;}}a[N*N];
int fd(int x){return x^f[x]?f[x]=fd(f[x]):x;}
void mg(int x,int y){f[fd(x)]=fd(y);}
signed main()
{
freopen("mcst.in","r",stdin);freopen("mcst.out","w",stdout);
scanf("%lld",&n);
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%lld",&x);
if(~x)a[++m]={i,j,x};
}
sort(a+1,a+m+1);
for(int i=1;i<=m;i++)
{
int u=a[i].u,v=a[i].v,w=a[i].w;
if(fd(u)==fd(v))continue;
mg(u,v);
ans+=w;
}
printf("%lld",ans);
return 0;
}