Describe
http://www.lydsy.com/JudgeOnline/problem.php?id=1601
There are n\ fields that require irrigation, each of which can be diverted by itself, spent as a w[i]\, or connected to other irrigated fields and spent in \ (p[i][j]\) for minimum cost.
Analysis
I thought it was DP at first sight and found it wrong ...
If the field cannot be diverted by itself, it can only be connected to other fields, the bare smallest spanning tree.
Here add a can own water, equivalent to add a and all points have a connecting edge, and the weights are \ (w[i]\) node, and then ask \ (n+1\) The minimum spanning tree.
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn= -+5;5 structedge{6 intu,v,w;7Edgeintu=0,intv=0,intw=0): U (U), V (v), W (w) {}8 BOOL operator< (ConstEdge &a)Const{returnw<A.W;}9}g[maxn*MAXN];Ten intN,ect,ans; One intF[MAXN]; AInlineintReadint&X) {x=0;intk=1;CharC for(C=getchar ();c<'0'|| C>'9'; C=getchar ())if(c=='-') k=-1; for(; c>='0'&&c<='9'; C=getchar ()) x=x*Ten+c-'0';returnx*=K;} -InlineintFindintx) {returnx==f[x]?x:f[x]=find (F[x]);} - intMain () { the read (n); - for(intI=1, w;i<=n;i++) G[++ect]=edge (0, I,read (W)), f[i]=i; - for(intI=1; i<=n;i++) for(intj=1, w;j<=n;j++){ - Read (w); + if(j<i) g[++ect]=Edge (i,j,w); - } +Sort (g+1, g+ect+1); A for(intI=1, j=n;i<=ect&&j;i++){ at intFu=find (g[i].u), fv=find (G[I].V); - if(FU!=FV) F[FU]=FV, J--, ans+=G[I].W; - } -printf"%d\n", ans); - return 0; -}
View Code
Bzoj_1601_[usaco2008_oct]_ Irrigation _ (minimum spanning tree _kruskal)