Bzoj_1601_[usaco2008_oct]_ Irrigation _ (minimum spanning tree _kruskal)

Source: Internet
Author: User

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&AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.