#P4438. [HNOI/AHOI2018] 道路
[HNOI/AHOI2018] 道路
Description
The transportation network of Country W forms a tree. There are cities and villages in total; cities are numbered to , villages are numbered to , and city is the capital. All roads are one-way; in this problem we only consider the road network from villages towards the capital.
For each city, there is exactly one highway and one railway leading into this city. For city , the starting point of a road (highway or railway) that leads to city is either a village, or a city whose index is greater than . No roads lead into any village. Except for the capital, every city or village has exactly one outgoing road; the capital has no outgoing road. Starting from any village and following the unique outgoing road, you will eventually reach the capital.
King W of Country W has obtained some funds to improve transportation. Due to limited funds, he can renovate roads. He will renovate exactly one incoming road for each city, i.e., for each city he chooses either its highway or its railway to renovate. To make travel from villages to the capital as convenient as possible, based on census data he sets three parameters for each village. For village , the parameters are , , and . Suppose that starting from village and going to the capital, you pass through unrenovated highways and unrenovated railways. Then the inconvenience value of this village is:
Under a given renovation plan, the sum of inconvenience values over all villages is the inconvenience value of that plan. Among all ways to renovate roads, the plan with the minimum total inconvenience is called the optimal renovation plan. King W wants this optimal plan. Please compute the total inconvenience of the optimal renovation plan.
Input Format
The first line contains a positive integer .
The next lines each describe a city. The -th of these lines contains two numbers . Here is the starting point of the highway that leads to city , and is the starting point of the railway that leads to city . If , then there is a highway from city to city ; otherwise there is a highway from village to city . Similarly, if , then there is a railway from city to city ; otherwise there is a railway from village to city .
The next lines each describe a village. The -th of these lines contains three numbers , as defined above.
Output Format
Output one line with a single integer: the total inconvenience of the optimal renovation plan.
6
2 3
4 5
-1 -2
-3 -4
-5 -6
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
54
9
2 -2
3 -3
4 -4
5 -5
6 -6
7 -7
8 -8
-1 -9
1 60 1
1 60 1
1 60 1
1 60 1
1 60 1
1 60 1
1 60 1
1 60 1
1 60 1
548
12
2 4
5 3
-7 10
11 9
-1 6
8 7
-6 -10
-9 -4
-12 -5
-2 -3
-8 -11
53 26 491
24 58 190
17 37 356
15 51 997
30 19 398
3 45 27
52 55 838
16 18 931
58 24 212
43 25 198
54 15 172
34 5 524
5744902
Hint
Sample Explanation 1.

In the figure, blue and yellow nodes denote cities and villages, respectively; green and red arrows denote highways and railways, respectively; bold arrows denote renovated roads.
One plan with total inconvenience equal to is: renovate the railways leading to city and city , and renovate the highways leading to all other cities. Using and to denote highway and railway, and and to denote renovated highway and renovated railway, respectively, we have:
The route for village to reach the capital is: , passing unrenovated highways and unrenovated railway, with cost .
The route for village to reach the capital is: , passing unrenovated highways and unrenovated railways, with cost .
The route for village to reach the capital is: , passing unrenovated highway and unrenovated railways, with cost .
The route for village to reach the capital is: , passing unrenovated highway and unrenovated railway, with cost .
The route for village to reach the capital is: , passing unrenovated highway and unrenovated railways, with cost .
The route for village to reach the capital is: , passing unrenovated highways and unrenovated railways, with cost .
The total inconvenience is . It can be proven that this is the optimal answer for this testdata.
Sample Explanation 2.
In this sample, it is obvious that all highways should be renovated.
Constraints
- There are in total sets of testdata, numbered to .
- For sets with index , .
- For sets with index , , .
- For sets with index , .
- For all sets, , , , are integers in , and from any village the capital is reachable via no more than roads.
Translated by ChatGPT 5
京公网安备 11011102002149号