以为动态规划,原来是最小生成树kruskal
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int a,b,cnt,res,f[100100];
struct node{
int v,u,w;
friend bool operator <(const node&x,const node&y){
return x.w<y.w;
}
}edge[1000010];
int find(int x){
if(x!=f[x]) return f[x]=find(f[x]);
else return x;
}
void merge(int x,int y){
int x1=find(x),y1=find(y);
f[x1]=y1;
}
int main(){
ios::sync_with_stdio(0);
cin>>a>>b;
int x;
for(int i=1;i<=b;i++){
for(int j=1;j<=b;j++){
cin>>x;
if(i<j&&x){
edge[++cnt].w=x;
edge[cnt].u=i;
edge[cnt].v=j;
}
}
}
for(int i=1;i<=100001;i++)f[i]=i;
sort(edge+1,edge+1+b*b);
/*for(int i=1;i<=b*b;i++){
cout<<edge[i].v<<' '<<edge[i].u<<' '<<edge[i].w<<endl;
}*/
int n=0;
for(int i=1;i<=b*b;i++){
if(find(edge[i].v)!=find(edge[i].u)&&edge[i].w<=a){
merge(edge[i].v,edge[i].u);
res+=edge[i].w;
n++;
}
}
cout<<res+(b-n)*a<<endl;
return 0;
}