F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

The Experience of Love

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 853    Accepted Submission(s): 314


Problem Description
A girl named Gorwin and a boy named Vivin is a couple. They arrived at a country named LOVE. The country consisting of $N$ cities and only $N-1$ edges (just like a tree), every edge has a value means the distance of two cities. They select two cities to liveŁ¬Gorwin living in a city and Vivin living in another. First date, Gorwin go to visit Vivin, she would write down the longest edge on this path(maxValue).Second date, Vivin go to Gorwin, he would write down the shortest edge on this path(minValue),then calculate the result of maxValue subtracts minValue as the experience of love, and then reselect two cities to live and calculate new experience of love, repeat again and again.

Please help them to calculate the sum of all experience of love after they have selected all cases.
 

Input
There will be about $5$ cases in the input file.
For each test case the first line is a integer $N$, Then follows $n-1$ lines, each line contains three integers $a$, $b$, and $c$, indicating there is a edge connects city $a$ and city $b$ with distance $c$.

[Technical Specification]
$1 < N <= 150000,1 <= a, b <= n,1 <= c <= {10}^{9}$
 

Output
For each caseŁ¬the output should occupies exactly one line. The output format is Case #x: answer, here x is the data number, answer is the sum of experience of love.
 

Sample Input
3 1 2 1 2 3 2 5 1 2 2 2 3 5 2 4 7 3 5 4
 

Sample Output
Case #1: 1 Case #2: 17
 

Hint
huge input,fast IO method is recommended.

In the first sample:
The maxValue is 1 and minValue is 1 when they select city 1 and city 2, the experience of love is 0.
The maxValue is 2 and minValue is 2 when they select city 2 and city 3, the experience of love is 0.
The maxValue is 2 and minValue is 1 when they select city 1 and city 3, the experience of love is 1.
so the sum of all experience is 1;
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-05-11 23:04:47, Gzip enabled