求助
#include<bits/stdc++.h>
using namespace std;
struct node {
long long from,to,quan;
} egde[50000001];
long long n,m,x,y,z,fa[50000001],ans,tot,s;
bool cmp(node a,node b) {
return a.quan<b.quan;
}
int get(int x1) {
if(fa[x1]!=x1) fa[x1]=get(fa[x1]);
return fa[x1];
}
int main() {
cin>>n>>m;
for(int i=1; i<=m; i++) {
for(int j=1; j<=m; j++) {
cin>>x;
if(x) {
s++;
egde[s].from=i;
egde[s].to=j;
egde[s].quan=min(x,n);
}
}
}
sort(egde+1,egde+1+s,cmp);
for(int i=1; i<=m; i++) fa[i]=i;
for(int i=1; i<=s; i++) {
int xx=get(egde[i].from);
int yy=get(egde[i].to);
if(tot==m-1) {
break;
}
if(xx==yy) continue;
tot++;
fa[xx]=yy;
ans+=egde[i].quan;
}
cout<<ans+n<<endl;
return 0;
}