提交时间:2022-07-13 12:22:31
运行 ID: 51646
#include<cstdio> #include<iostream> #include<algorithm> #define ll long long using namespace std; const int N=1005; int n,k,cnt,f[N<<1]; ll ans; struct node { int x,y,z; } edge[N*N]; bool cmp(node a,node b) { return a.z>b.z; } int find(int x) { if(f[x]!=x) f[x]=find(f[x]); return f[x]; } int main() { int x,y,z,fx,fy,sum; scanf("%d%d",&n,&k); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) { scanf("%d",&x); if(i>=j) continue; edge[++cnt]=(node) { i,j,x }; edge[++cnt]=(node) { i,j+n,k-x }; } if(k==1) { printf("%d",(n-2)*(n-1)/2); return 0; } sort(edge+1,edge+1+cnt,cmp); for(int i=1; i<=2*n; i++) f[i]=i; for(int i=1; i<=cnt; i++) { x=edge[i].x; y=edge[i].y; z=edge[i].z; fx=find(x); fy=find(y); if(fx==fy) ans+=z; else { if(sum==n) continue; sum++; ans+=z; f[fy]=fx; } } printf("%d\n",ans); return 0; }