#include<stdio.h>
#include<algorithm>
using namespace std;
#define maxn 400001 //数据范围仅供参考
struct edge { int u,v,w; }e[maxn];
int f[5001];
int A,B,u,v,w,x,ans=0,cnt=0,tot=0;
inline bool cmp(edge x,edge y);
inline int find(int x);
inline void kruskal();
inline int read();
int main()
{
A=read(),B=read();
for(int i=1;i<=B;i++)
{
f[i]=i;
e[++tot].v=i;
e[tot].w=A;
}
for(int i=1;i<=B;i++)
for(int j=1;j<=B;j++)
{
x=read();
if(x==0) continue;
e[++tot].w=x;
e[tot].u=i;
e[tot].v=j;
}
kruskal();
printf("%d",ans);
return 0;
}
inline bool cmp(edge x,edge y)
{
return x.w<y.w;
}
inline int find(int x)
{
if(f[x]==x) return x;
return f[x]=find(f[x]);
}
inline void kruskal()
{
sort(e+1,e+B+1,cmp);
for(int i=1;i<=B;i++)
{
int u=find(e[i].u),v=find(e[i].v);
if(u==v) continue;
ans+=e[i].w;
f[v]=u;
}
}
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
第二个题目样例过不了,不知道为什么?