1060: [ZJOI2007] Temporal synchronization
Description
The small q learns the welding circuit board in the electronic craft practice class. A circuit board consists of several components, we might call it a node, and use it as a digital .... to label. Each node of the board is connected by a number of disjoint conductors, and for any two nodes of the board, there is only one path (the path refers to a sequence of conductors connecting two elements). There is a special element called the "activator" on the circuit board. When the exciter is working, an excitation current is generated, passing through the wire to each node to which it is connected. And the intermediate node receives the excitation current, obtains the information, and passes the excitation current to the node which is connected with it and has not received the excitation current. Eventually, the intense current will reach some "terminating nodes"-nodes that are no longer forwarded after receiving the excitation current. The propagation of the excitation current on the wire takes time, and for each side E, the excitation current passes through the time it takes to TE, and the forwarding of the excitation current received by the node can be considered instantaneous. The circuit board now requires each "terminating node" to get an excitation circuit at the same time-keeping the tense in sync. Because the current structure does not conform to the requirement of temporal synchronization, it is necessary to change the structure of the connecting line. At present, small Q has a prop, the use of the props, you can make the excitation current through a connecting wire to increase the time of a unit. How many times can the small q use props to make all the "terminating node" tenses synchronized?
Input
The first line contains a positive integer n, which represents the number of nodes in the board. The second line contains an integer s, which is the number of the board's activator. Next N-1 line, three integers a, B, t per line. Indicates that the wire connects Node A with node B, and that the excitation current passes through this conductor and requires T unit time
Output
Contains only one integer v, the minimum number of items used for small q
Sample Input3
1
1 2 1
1 3 3Sample Output2HINT
"Data Size"
For 100% of data, n≤500000
For all the data, te≤1000000
Source
Exercises
Set F[x] Indicates the maximum time of the leaf node to X
Then the f[x]=f[son]+edge.v answer is Sigma f[x]-f[son]-edge.v
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<map>using namespacestd;Const intN = 5e5+ -, M =30005, mod =1000000007, INF =0x3f3f3f3f; typedefLong Longll;//The difference is 1, the same is 0intn,s,head[n],t=1, f[n];ll ans;structedge{intTo,next,v;} e[n*2];voidAddintUintVintW) {e[t].to = V,e[t].next = HEAD[U],E[T].V = w,head[u]=t++;}voidDfsintXintFA) { for(intI=head[x];i;i=E[i].next) { if(fa==e[i].to)Continue; DFS (E[I].TO,X); F[X]= Max (f[x],f[e[i].to]+e[i].v); } for(intI=head[x];i;i=E[i].next) { if(fa==e[i].to)Continue; Ans+=f[x]-f[e[i].to]-e[i].v; }}voidsolve () {DFS (s,s); cout<<ans<<Endl;}intMain () {scanf ("%d%d",&n,&s); for(intI=1; i<n;i++) { intA,b,c; scanf ("%d%d%d",&a,&b,&c); Add (a,b,c); add (b,a,c); } solve (); return 0;}
Bzoj 1060: [ZJOI2007] temporal sync tree dp