1827: [Usaco2010 mar]gather Cow big rally time limit:1 Sec Memory limit:64 MB
submit:793 solved:354
[Submit] [Status] [Discuss] Descriptionbessie is planning an annual cow rally, and cows from all over the country will attend the rally. Of course, she will choose the most convenient place to host the rally. Each cow lives on one of the N (1<=n<=100,000) farms, which are connected by N-1 roads and can reach another farm from any farm. Road I connects Farms A_i and b_i (1 <= a_i <=N; 1 <= b_i <= N), with a length of l_i (1 <= l_i <= 1,000). A rally can be held at any of the N farms. In addition, each barn occupant c_i (0 <= c_i <= 1,000) cows only. When choosing a venue for a rally, Bessie wants to maximize convenience (i.e. minimizing inconvenience). For example, the choice of the X Farm as a meeting place, its inconvenience is the other cattle in the barn to go to the rally of the distance of the sum, (for example, farm I arrived at Farm X is 20, then the total journey is c_i*20). Help Bessie find the most convenient place to hold a big rally. Consider a country of five farms, each connected by roads of varying lengths. On all farms, no cows live in numbers 3rd and 4th. Input first line: An integer N * second to N+1 line: line i+1 has an integer c_i * n+2 line to 2*n line, i+n+1 behavior 3 integers: a_i,b_i and l_i. output* First line: A value that represents the smallest inconvenient value. Sample Input5
1
1
0
0
2
1 3 1
2 3 2
3 4 3
4 5 3
Sample Output15
the number of sub-tree points for each point is asked firstthen the root node is set to the size of the collection pointthen search all the way down, update the weightsthe feeling can solve a kind of problem again.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 6 #defineMAXN 1000017 8 using namespacestd;9 Ten Long Longtot=0, W[maxn],f[maxn],ans; One AInlineint inch() - { - intx=0;CharCh=GetChar (); the while(ch<'0'|| Ch>'9') ch=GetChar (); - while(ch<='9'&&ch>='0') x=x*Ten+ch-'0', ch=GetChar (); - returnx; - } + - structed{ + intTo,c,last; A}edge[maxn*2]; at - intLast[maxn],cnt=0, A[MAXN]; - - BOOLVIS[MAXN]; - - voidAddintUintVintc) in { -edge[++cnt].to=v,edge[cnt].c=c,edge[cnt].last=last[u],last[u]=CNT; toedge[++cnt].to=u,edge[cnt].c=c,edge[cnt].last=last[v],last[v]=CNT; + } - the voidDP (intPOS) * { $w[pos]=a[pos];vis[pos]=1;Panax Notoginseng for(intI=last[pos];i;i=edge[i].last) - { the intu=edge[i].to; + if(Vis[u])Continue; A DP (u); thew[pos]+=W[u]; +f[pos]+=w[u]*edge[i].c+F[u]; - } $ } $ - voidDfsintPoiLong Longval) - { thevis[poi]=1; ans=min (ans,val); - for(intI=last[poi];i;i=edge[i].last)Wuyi if(!vis[edge[i].to]) theDFS (edge[i].to,val+ (tot-2*W[EDGE[I].TO]) *edge[i].c); - } Wu - intMain () About { $ intn,u,v,c; -n=inch(); - for(intI=1; i<=n;i++) a[i]=inch(), tot+=A[i]; - for(intI=1; i<n;i++) A { +u=inch(); v=inch(); c=inch(); the Add (u,v,c); - } $DP (1); ans=f[1]; thememset (Vis,0,sizeof(Vis)); theDfs1, f[1]); theprintf"%lld", ans); the return 0; -}
View Code
"Tree-shaped dp/search" Bzoj 1827: [Usaco2010 mar]gather Cow Rally