Noi2011 Road Construction

Source: Internet
Author: User
2435: [noi2011] road construction time limit: 10 sec memory limit: 128 MB
Submit: 1974 solved: 550
[Submit] [Status] Description

There are n countries on planet W. For the economic development of their respective countries, they decided
Build two-way roads to connect countries. However, kings in every country are very stingy.
Build EXACTLY n-1 bidirectional Road. The construction of each road requires a certain amount of cost, which is equal to the absolute value of the length of the road multiplied by the number of countries at both ends of the road. For example, in the middle and dotted lines, there are two or four countries at both ends of the road. If the length of the road is 1, the cost is 1 × | 2-4 | = 2. The number in the circle indicates the country number.


Because the number of countries is very large, there are many kinds of road construction solutions.
The cost is hard to be calculated manually. The Kings decide to find someone to design a software program and calculate the given construction scheme.
Calculate the required fees. Please help the Kings design such a software.

Input

The first line of the input contains an integer N, indicating the number of countries on the W planet. The number of countries ranges from 1 to n.
Id. The next n-1 lines describe the road construction, where row I contains three integers: AI, Bi, and CI.
It indicates that the second two-way road is built between AI and Bi with a length of Ci.

Output

An integer indicates the total cost required to build all roads.

Sample Input
6
1 2 1
1 3 1
1 4 2
6 3 1
5 2 1
Sample output20
Hint


N = 1,000,000 1 ≤ AI, Bi ≤ n

0 ≤ CI ≤ 10 ^ 6

Source

Day2

Question:

I have never seen such a score.

It seems that 2011 is the year where I saw the most score delivery. day1t1 75 day2t1100. No wonder that year's bronze line was 300.

In addition, a raccoon's typewriter can get around 20 points with KMP ......

Randomly select a node (random), convert the rootless tree to a rootless tree, traverse from the bottom up, directly update the answer and parent node information just so

This scale is said to have exploded ...... Sure enough, Cena broke 11 points, but there should be no pressure AC in Linux ......

Code:

 1 const maxn=1000000+10; 2 type node=record 3      from,go,next,w:int64; 4      end; 5 var ans:int64; 6     e:array[0..2*maxn] of node; 7     head,s:array[0..maxn] of int64; 8     v:array[0..maxn] of boolean; 9     i,n,x,y,z,root,tot:longint;10 procedure ins(x,y,z:longint);11  begin12  inc(tot);13  e[tot].from:=x;e[tot].w:=z;e[tot].go:=y;e[tot].next:=head[x];head[x]:=tot;14  end;15 procedure insert(x,y,z:longint);16  begin17  ins(x,y,z);ins(y,x,z);18  end;19 procedure init;20  begin21  readln(n);22  for i:=1 to n-1 do begin readln(x,y,z);insert(x,y,z);end;23  end;24 procedure dfs(x:longint);25  var i,y:longint;26  begin27  s[x]:=0;28  v[x]:=true;29  i:=head[x];30  while i<>0 do31   begin32   y:=e[i].go;33   if not(v[y]) then34    begin35     dfs(y);36     inc(s[x],s[y]);37     inc(ans,e[i].w*(abs(s[y]-(n-s[y]))));38    end;39   i:=e[i].next;40   end;41  inc(s[x]);42  end;43 procedure main;44  begin45  randomize;46  ans:=0;47  root:=random(n)+1;48  fillchar(v,sizeof(v),false);49  dfs(root);50  writeln(ans);51  end;52 begin53  assign(input,‘road.in‘);assign(output,‘road.out‘);54  reset(input);rewrite(output);55  init;56  main;57  close(input);close(output);58 end.    
View code

 

Contact Us

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.