WA 3 个
#include<bits/stdc++.h>
#define ll long long
using namespace std;
namespace Mashiro {
char buf[1<<18],*p1=buf,*p2=buf;
inline int getc() {
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline void read(T& x) {
x=0;int f=1;
char ch=getc();
while(!isdigit(ch)){if(ch=='-')f=~f+1;ch=getc();}
while (isdigit (ch)) {x=(x<<3)+(x<<1)+(ch^48);ch=getc();}
x*=f;
}
template <typename T,typename... Args> inline void read(T& x, Args&... args) {
read(x);
read(args...);
}
char buffer[1<<18];int p11=-1;const int p22=(1<<18)-1;
inline void flush() {fwrite(buffer,1,p11+1,stdout),p11=-1;}
inline void putc(const char &x) {if (p11==p22) flush();buffer[++p11]=x;}
template<typename T>inline void write(T x) {
static int buf[40],top=0;
if(x<0)putc('-'),x=~x+1;
while(x)buf[++top]=x%10,x/=10;
if(top==0)buf[++top]=0;
while (top) putc(buf[top--]^48);
putc(' ');
flush();
}
template <typename T,typename... Args> inline void write(T x, Args... args) {
write(x);
write(args...);
}
}
using namespace Mashiro;
const int maxn=1000;
const int maxm=1e6+10;
int A,B,ans=0,cnt=0;
namespace UFDS{
int fa[maxn];
inline void Memset(int n){
for(int i(0);i<=n;++i){
fa[i]=i;
}
}
inline void merge(int u,int v){
fa[u]=v;
}
inline int find(int u){
if(fa[u]==u)return u;
else return fa[u]=find(fa[u]);
}
}
struct node{
int u,v,w;
}kano[maxm];
int main() {
read(A,B);
for(int i(1);i<=B;++i){
for(int j(1);j<=B;++j){
++cnt;
read(kano[cnt].w);
if(i>j&&kano[cnt].w<=A&&kano[cnt].w!=0){
kano[cnt].u=i;
kano[cnt].v=j;
}
else {
kano[cnt--].w=0;
}
}
}
for(int i(1);i<=B;++i){
++cnt;
kano[cnt].u=0;
kano[cnt].v=i;
kano[cnt].w=A;
}
sort(kano+1,kano+1+cnt,[](const node &a,const node &b){
return a.w<b.w;
});
UFDS::Memset(cnt);
for(int i(1);i<=cnt;++i){
int u=kano[i].u;
int v=kano[i].v;
int fu=UFDS::find(u);
int fv=UFDS::find(v);
if(fu==fv)continue;
UFDS::merge(fu,fv);
ans+=kano[i].w;
}
write(ans);
#ifndef ONLINE_JUDGE
cout<<endl;
system("pause");
#endif
return 0;
}