# (Tree movable Regulation) Rockwell synchronization state valley P1131 [ZJOI2007] (increase + / Province selected from -)

Title Description

Small Q Q learning soldered on an electronic circuit board technology training courses. A circuit board by a number of elements, we might call a node, and with their digital 1,2,3 ... 1 , 2 , 3 .... For reference. Each node is connected to the circuit board by a plurality of disjoint conductor, and for any two nodes of the circuit board, and there exist only one path (path means for connecting two conductors sequence).

In the presence of a particular element on a circuit board called a "trigger." When operating excitation, generate an excitation current, passed through a wire connected to each of the nodes it. And the intermediate node after receiving the excitation current, obtain information, and the energization to spread it and connected to the node has not received the exciting current. Finally, intense current will reach some "terminal node" - node after receiving the excitation current is no longer forwarded.

Propagating the excitation current on the wire is takes time, for each edge E E, by the time it takes the excitation current for t_e T E , the node forwards the received excitation current may be considered to be instantaneous. The board now requires each "termination node" while the obtained excitation circuit - i.e. the synchronization holding state. Since the structure does not comply with current temporal synchronization requirements, it needs by changing the configuration of the connecting line. Currently small Q Q has a prop, once the prop, so that the excitation current can be increased by one unit time through a connection strip conductor. Q Will the small synchronous state how many times the minimum use of props available so that all the "end node" when?

Input Format

The first line contains a positive integer N N, is the number of nodes in the circuit board.

The second row contains an integer S S, numbered for the excitation circuit board.

Next, N. 1- N - . 1 lines of three integers A, B, T A , B , T. It signifies that this wire connecting node A A and the node B B, and the excitation current required by these wires T T units of time.

Output Format

It contains only one integer V V, a small Q frequency and Q props least used.

Sample input and output

Input # 1
3
1
1 2 1
1 3 3
Output # 1
2

Description / Tips

For 40 \% . 4 0 % of the data, N ≤ 1000 N . 1 0 0 0

For 100 \% . 1 0 0 % data, N 500000 ≤ N . 5 0 0 0 0 0

For all data, t_e 1000000 ≤ T E . 1 0 0 0 0 0 0

 

analysis:

First, the problem can be transformed into: Given the right side of a tree and its each side, from the root to reach the required weight of each leaf node and equal, at least need to change how many times?

Set F [i] [j] denotes the node for the subtree rooted i, i to the right node from each leaf and the values ​​are equal, the right to change and reach a minimum number of values ​​j of the leaf node;

For a node i the son of x1, x2, x3, x4 .....

f[i]=max(f[xi]+e[i].w);

change+=∑{f[i]-(f[xi]+e[i].w)};

Output ans can!

 

See input data, n nodes since only the n-1 sides, it is difficult to see a tree. We can counter the thinking is to make all leaf nodes simultaneously send a signal, then these signals simultaneously reach the root node. So we can be maintained bottom, all the child nodes such that the signal simultaneously reach each node of the node.

 

So we consider how to maintain. All child maintenance we start the search from the root node to the leaf node search, back when the first node to node to maintain maximum right side of the node (the right side is a leaf node to arrive at the same time that it takes time). Then maintain the answer, the answer is the maximum right side minus the right side of all the child nodes. The maximum right side and the right side of the maintenance of the parent node, the parent node for the right side of the child node to the parent node + time of the node. Then backtracking operation is repeated, until the root node.Hard to say clearly ah QWQ see a little more clearly annotated

 

Then we should pay attention to some details:

 

  1. Be sure to add a two-way side, it is undirected graph.
  2. Since it is an undirected graph, not to the maintenance side of the parent node is calculated.
  3. Maintaining order must not be arbitrary.
  4. The answer to use long long memory.

 

Code:

#include <bits/stdc++.h>
#define MAXN 1000005
using namespace std;
struct Edge{int next,to,dis;} edge[MAXN];
int n,s,a,b,t,maxn[MAXN],cnt,head[MAXN];
long long ans;

void addedge(int from, int to, int dis)
{
edge[++cnt].next=head[from];
edge[cnt].to=to;
edge[cnt].dis=dis;
head[from]=cnt;
}

void dfs(int x, int fa)
{
for(int i=head[x]; i; i=edge[i].next)
if(edge[i].to!=fa) dfs(edge[i].to, x);

for(int i=head[x]; i; i=edge[i].next)
if(edge[i].to!=fa) maxn[x]=max(maxn[x], edge[i].dis);

for(int i=head[x]; i; i=edge[i].next)
if(edge[i].to!=fa) ans+=(maxn[x]-edge[i].dis);

for(int i=head[fa]; i; i=edge[i].next)
if(edge[i].to==x) edge[i].dis+=maxn[x];

}

int main()
{
scanf("%d%d",&n,&s);
for(int i=1; i<=n-1; i++)
{
scanf("%d%d%d",&a,&b,&t);
addedge(a, b, t);
addedge(b, a, t);
}
dfs(s, 0);
printf("%lld\n",ans);
return 0;
}

 

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11441431.html