2435: [Noi2011] Road construction time limit: ten Sec Memory Limit: MB
Submit: 2188 Solved: 639
[Submit] [Status]
Description
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
20
HINT
n = 1,000,000 1≤ai, bi≤n
0≤ci≤10^6
Source
Day2
The problem: In fact, is a search, because the topic gave a tree, so directly build, and then find out how many descendants each point (calculate their own) ... itself should not be difficult, but for P party question came to--BFS, killed or timed out (the pit father's int64 operation is simply slow to cry); Dfs ha directly burst stack does not explain (for C + + Party Linux under the system stack infinite expression seriously despise!!) TT portal), so I still heroic tle, God Ben Help (but the program algorithm should be no problem, is int64 harm, hum ... The Moe sister angry!!! )
1type
2Point=^node;
3Node=Record
4G,w:longint;
5Next:point;
6End;
7
8var
9I,j,k,l,m,n:longint;ll:int64;
TenA:Array[0..1000000] ofPoint
OneC,b,d,e:Array[0..1000500] ofLongint;
AP:point;
-procedureAdd (x,y,z:longint); inline;
-var
theP:point;
-begin
-New (P);
-P^.g:=y;
+P^.w:=z;
-P^.NEXT:=A[X];
+A[x]:=p;
AEnd;
atprocedureBfs1;inline;
-var
-P:point;f,r:longint;
-begin
-b[1]:=1;d [1]:=1;
-f:=1; r:=2;
in whileF<r Do
- begin
toP:=A[D[F]];
+ whileP<>Nil Do
- begin
theifb[p^.g]=0 Then
* begin
$b[d[f]]:=2;
Panax Notoginsengb[p^.g]:=1;
-C[P^.G]:=D[F];
theD[R]:=P^.G;
+Inc (R);
A End;
theP:=p^.next;
+End;
-Inc (F);
$End;
$End;
-begin
-READLN (n);
the fori:=1 toN Doa[i]:=Nil;
- fori:=1 toN-1 Do
Wuyibegin
theREADLN (j,k,l);
-Add (j,k,l);
WuAdd (k,j,l);
-End;
AboutFillchar (b,sizeof (b),0); ll:=0;
$Fillchar (C,sizeof (c),0); Fillchar (d,sizeof (d),0);
-bfs1;ll:=0;
- forI:=nDownto1 Do
-begin
Ae[d[i]]:=1;
+P:=a[d[i]];
the whileP<>Nil Do
-begin
$ifP^.g<>c[d[i]] Then
thebegin
theLl:=ll+int64 (ABS (e[p^.g]-(N-E[P^.G))) *int64 (P^.W);
theE[D[I]]:=E[D[I]]+E[P^.G];
theEnd;
-P:=p^.next;
inEnd;
theEnd;
theWriteln (LL);
AboutREADLN; End.
2435: [Noi2011] Road construction (Help!!!) )