1827: [Usaco2010 mar]gather Cow Rally time limit: 1 Sec Memory Limit: MB
Submit: 722 Solved: 314
[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
HINT
Source
Gold
Thinking questions.
found that the farms constituted a tree.
So first make the root of any one point and take this point as the target point.
So the final answer is that every tree by a few cows to go, a side with a few cows to go is actually this side of the next there are several descendants.
So we preprocessed the answer to the tree with the root of 1, and found that each node had several descendants, and then O (1) transferred to his son's answer, and the son could then O (1) transfer to his own son ...
So we use O (n) time to find the distance at each point as the destination and the output is minimized.
The transfer method is:
When the root becomes a son, only the side that connects the son wants to change, according to the son of each node that is preprocessed, can transfer.
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath > #include <algorithm> #define M 200000+5#define LL long longusing namespace Std;int tot=0,h[m],n,c[m],cnt; LL ans;struct Edge{int Y,ne; LL v;} E[m*2];struct data{int Son; LL l;} A[m];void addedge (int x,int y,ll l) {E[++tot].y=y;e[tot].ne=h[x];e[tot].v=l;h[x]=tot;} void Prepare (int x,int fa) {for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (Y==FA) continue; Prepare (y,x); a[x].son+=a[y].son;a[x].l=a[x].l+a[y].l+ (1ll*e[i].v*a[y].son);} A[X].SON+=C[X];} void Dfs (int x,int fa,ll now) {for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (Y==FA) continue; LL k=now+1ll*e[i].v* (cnt-a[y].son*2); Ans=min (ans,k);d FS (y,x,k);}} int main () {scanf ("%d", &n), cnt=0;for (int i=1;i<=n;i++) scanf ("%d", &c[i]), cnt+=c[i];for (int i=1;i<n;i + +) {int x, y; LL l;scanf ("%d%d%lld", &x,&y,&l); Addedge (x,y,l); Addedge (y,x,l);} Prepare (1,0); Ans=a[1].l;dfs (1,0,A[1].L); Cout<<ans<<endl;return 0;}
"Bzoj 1827" [Usaco2010 Mar]gather Dairy Big Rally