#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
int n,m,ans,pp,sum,num;
int f[maxn];
struct node{
int a,b,w;
}l[maxn];
int find(int x){
if(f[x]==x) return f[x];
else return f[x]=find(f[x]);
}
bool cmp(node k1,node k2){
return k1.w<k2.w;
}
void puio(){
sort(l+1,l+1+m,cmp);
for(int i=1;i<=pp-num;i++){
int A=find(l[i].a),B=find(l[i].b);
if(A!=B){
f[A]=f[B];
ans+=l[i].w;
sum++;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i=1;i<=m;i++) f[i]=i;
for(int i=1;i<=m;i++){
l[i].a=0;
l[i].b=i;
l[i].w=n;
}
for(int i=1; i<=m; i++) {
for(int j=1; j<=m; j++) {
int x;
cin>>x;
if(i>=j || x==0) continue;
l[++pp].a=i;
l[pp].b=j;
l[pp].w=x;
if(l[pp].w>n) num++;
}
}
if(sum==m-1) cout<<ans+n;
else cout<<ans+(m-sum)*n ;
return 0;
}
克鲁斯卡尔算法 不知道哪里错了,大佬解惑一下