#P4438. [HNOI/AHOI2018] 道路

    ID: 3345 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划,dp2018各省省选安徽湖南枚举,暴力深度优先搜索,DFS树形 dp概率论,统计

[HNOI/AHOI2018] 道路

Description

The transportation network of Country W forms a tree. There are n1n - 1 cities and nn villages in total; cities are numbered 11 to n1n - 1, villages are numbered 11 to nn, and city 11 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 ii, the starting point of a road (highway or railway) that leads to city ii is either a village, or a city whose index is greater than ii. 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 n1n - 1 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 ii, the parameters are aia_i, bib_i, and cic_i. Suppose that starting from village ii and going to the capital, you pass through xx unrenovated highways and yy unrenovated railways. Then the inconvenience value of this village is:

ci(ai+x)(bi+y)c_i \cdot (a_i + x) \cdot (b_i + y)

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 n1n - 1 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 nn.

The next n1n - 1 lines each describe a city. The ii-th of these lines contains two numbers si,tis_i, t_i. Here sis_i is the starting point of the highway that leads to city ii, and tit_i is the starting point of the railway that leads to city ii. If si>0s_i > 0, then there is a highway from city sis_i to city ii; otherwise there is a highway from village si-s_i to city ii. Similarly, if ti>0t_i > 0, then there is a railway from city tit_i to city ii; otherwise there is a railway from village ti-t_i to city ii.

The next nn lines each describe a village. The ii-th of these lines contains three numbers ai,bi,cia_i, b_i, c_i, 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 5454 is: renovate the railways leading to city 22 and city 55, and renovate the highways leading to all other cities. Using \rightarrow and \Rightarrow to denote highway and railway, and ∗\rightarrow and ∗\Rightarrow to denote renovated highway and renovated railway, respectively, we have:

The route for village 11 to reach the capital is: 131-1 ∗\rightarrow 3 \Rightarrow 1, passing 00 unrenovated highways and 11 unrenovated railway, with cost 3×(1+0)×(2+1)=93 \times (1 + 0) \times (2 + 1) = 9.

The route for village 22 to reach the capital is: 231-2 \Rightarrow 3 \Rightarrow 1, passing 00 unrenovated highways and 22 unrenovated railways, with cost 2×(1+0)×(3+2)=102 \times (1 + 0) \times (3 + 2) = 10.

The route for village 33 to reach the capital is: 3421-3 ∗\rightarrow 4 \rightarrow 2 ∗\rightarrow 1, passing 11 unrenovated highway and 00 unrenovated railways, with cost 3×(2+1)×(1+0)=93 \times (2 + 1) \times (1 + 0) = 9.

The route for village 44 to reach the capital is: 4421-4 \Rightarrow 4 \rightarrow 2 ∗\rightarrow 1, passing 11 unrenovated highway and 11 unrenovated railway, with cost 1×(2+1)×(3+1)=121 \times (2 + 1) \times (3 + 1) = 12.

The route for village 55 to reach the capital is: 5521-5 \rightarrow 5 ∗\Rightarrow 2 ∗\rightarrow 1, passing 11 unrenovated highway and 00 unrenovated railways, with cost 2×(3+1)×(1+0)=82 \times (3 + 1) \times (1 + 0) = 8.

The route for village 66 to reach the capital is: 6521-6 ∗\Rightarrow 5 ∗\Rightarrow 2 ∗\rightarrow 1, passing 00 unrenovated highways and 00 unrenovated railways, with cost 1×(3+0)×(2+0)=61 \times (3 + 0) \times (2 + 0) = 6.

The total inconvenience is 9+10+9+12+8+6=549 + 10 + 9 + 12 + 8 + 6 = 54. 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 2020 sets of testdata, numbered 11 to 2020.
  • For sets with index 4\le 4, n20n \le 20.
  • For sets with index 585 \sim 8, ai,bi,ci5a_i, b_i, c_i \le 5, n50n \le 50.
  • For sets with index 9129 \sim 12, n2000n \le 2000.
  • For all sets, n20000n \le 20000, 1ai,bi601 \le a_i, b_i \le 60, 1ci1091 \le c_i \le 10^9, si,tis_i, t_i are integers in [n,1](i,n1][-n, -1] \cup (i, n - 1], and from any village the capital is reachable via no more than 4040 roads.

Translated by ChatGPT 5