is a template problem of the smallest spanning tree, and by the way it learns the Rujia and Kruskal algorithms.
1 /*2 id:abc312613 lang:c++4 task:agrinet5 */6#include <cstdio>7#include <cstring>8#include <algorithm>9#include <iostream>Ten using namespacestd; One Const intmaxn=11111; A intR[MAXN],F[MAXN],W[MAXN],U[MAXN],V[MAXN]; - - intcmpConst intIConst intj) {returnw[i]<w[j];} {Indirect sort} the - intFindintx) - { - if(f[x]!=x) f[x]=find (F[x]); + returnF[x]; - } + intMain () A { at inti,j,l,x,y,n,m=0, ans=0; -Freopen ("agrinet.in","R", stdin); -Freopen ("Agrinet.out","W", stdout); -scanf"%d",&n); - for(i=1; i<=n;i++) - for(j=1; j<=n;j++) in { -scanf"%d",&l); tow[++m]=l; +u[m]=i; -v[m]=J; the } * for(i=1; i<=m;i++) r[i]=f[i]=i; $Sort (r+1, r+m+1, CMP);Panax Notoginseng for(i=1; i<=m;i++) - { theJ=r[i]; X=find (U[j]); y=find (V[j]); + if(x!=y) A { theans+=W[j]; +f[y]=x; - } $ } $printf"%d\n", ans); - return 0; -}
[Usaco3.1.1]agrinet