Source: Internet
Author: User

There are n countries on planet W. For the economic development of their respective countries, they decided to
The construction of two-way roads makes connectivity between countries. But the kings of every country are so stingy that they only want to
To build exactly n–1 two-way road. The construction of each road costs a fee equal to the absolute value of the road length multiplied by the number of countries on both sides of the road. For example, in the dotted line, there are 2 or 4 countries at each end of the road, and if the road length is 1, the cost is 1x|2–4|=2. The number in the circle in the figure represents the country's number.

As the number of countries is very large, there are many ways to build roads, while the construction of each of these programmes
Cost is difficult to calculate manually, the king decided to find someone to design a software, for a given construction plan,
To figure out the costs that are required. Please help the Kings design a software like this.

Input

The first line of input contains an integer n, representing the number of countries on planet W, the country from 1 to n
Number. Next n–1 describes road construction, where line I contains three integers ai, bi, and CI, table
The two-way road is constructed between AI and bi two countries with a length of CI.

Output

Output An integer that represents the total cost to build all roads.

Sample Input 6
1 2 1
1 3 1
1 4 2
6 3 1
5 2 1
Sample Output -
HINT

n = 1,000,000 1≤ai, bi≤n

0≤ci≤10^6

Acty really big ... Don't want to say it anymore .... —————— the following ——————noi of God ... is purely DFS ... I can't believe everything in front of me.
`#include <stdio.h>#include<iostream>#include<stdlib.h>using namespacestd;Const intn=1000005;#definell Long Longintn,i,x,y,z,s[n];ll ans;inttot,head[n],next[n<<1],to[n<<1],v[n<<1];voidAddintXintYintz) {Tot++; To[tot]=y; V[tot]=Z; Next[tot]=Head[x]; HEAD[X]=tot;}voidDfsintXinty) {    inti;  for(i=head[x];i!=-1; i=Next[i])if(to[i]!=y) {dfs (to[i],x); S[X]+=S[to[i]]; Ans+ = (ll) (ABS (2*S[TO[I]]-N)) *V[i]; }}intMain () {scanf ("%d",&N);  for(i=1; i<=n;i++) {Head[i]=-1; S[i]=1; }     for(i=1; i<n;i++) {scanf ("%d%d%d",&x,&y,&z);        Add (x, y, z);    Add (y,x,z); } DFS (1,0); cout<<ans; return 0;}`

Related Keywords:

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.