#P9476. [_-0 B] 地铁

    ID: 10557 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DPO2优化动态规划优化斜率优化树形 DP

[_-0 B] 地铁

Background

City A, the hometown of little f\mathfrak{f}, has recently opened a subway.

Problem Description

City A has nn residential areas (2n105)(2\le n \le 10^5). The population of the ii-th residential area is si(1si107)s_i (1 \le s_i \le 10^7). There are also n1n-1 bidirectional roads forming a tree. The ii-th road connects residential areas uiu_i and viv_i. It takes wi(1wi107)w_i (1 \le w_i\le 10^7) time for a person to walk through this road.

Now the government of City A decides to build one subway line. The subway line is a simple path on the tree. If the path passes through the ii-th road, then taking the subway through (under) this road only takes wi(1wi107)w_i^{\prime} (1 \le w_i^{\prime} \le 10^7) time. Also, entering and exiting the subway stations costs a total of t(0t107)t (0 \le t \le 10^7) time.

It is known that when a person travels from one residential area to another, if the travel path shares at least one common edge with the subway path, then they will definitely choose to take the subway for as much of the trip as possible. If there is no common edge, they will walk the whole way. The problem guarantees that for the ii-th road, wiwitw_i^{\prime} \le w_i - t. We consider that the larger the product of the populations of two residential areas is, the more likely people want to move between them.

Now, little f\mathfrak{f} wants to know, among all n(n1)2\frac{n(n-1)}{2} ways to build the subway line, the minimum value of $\sum_{a=1}^{n-1}\sum_{b=a+1}^{n}(s_a \cdot s_b \cdot d_{a,b})$, where da,bd_{a,b} denotes the time needed to travel from residential area aa to bb (or from bb to aa, which is the same).

But he cannot do it, so he comes to ask the all-powerful you for help.

Input Format

The first line contains three integers id,n,tid, n, t separated by spaces, representing the subtask id, the number of residential areas, and the time cost for entering and exiting stations.

The next nn lines each contain one integer sis_i, representing the population of each residential area.

The next n1n - 1 lines each contain four integers ui,vi,wi,wiu_i, v_i, w_i, w_i^{\prime} separated by spaces, representing the two endpoints of each road, the walking time, and the subway time.

Output Format

One line with one integer, representing the minimum value required.

The answer may exceed the range of a 6464-bit integer. You may use the __int128 type, whose range is [2127,21271][-2^{127},2^{127}-1].

The following is an output template for the __int128 type:

#include <bits/stdc++.h>
using namespace std;
__int128 ans;
int main() {
    /* Your code here */
    string str;
    if (!ans) {
        str = "0";
    } else {
        str = "";
        while (ans) {
            str = ((char)(48 + ans % 10)) + str;
            ans /= 10;
        }
    }
    cout << str << endl;
    return 0;
}
0 5 0
9
9
3
2
3
1 2 7 6
1 3 8 5
1 4 6 5
2 5 9 9
2262
0 10 86
50
6
84
50
83
67
93
55
93
70
1 2 94 7
1 3 97 4
1 10 98 12
2 4 89 1
2 7 98 1
4 5 99 13
4 6 96 5
5 8 95 5
5 9 97 7
33600416

Hint

Sample 11 explanation:

Before building the subway, it is as shown in the figure below:

One optimal subway plan is to build the subway from 22 to 33. It is as shown in the figure below (solid lines indicate roads where the subway is built):

From 11 to 22, the path uses the subway. The required time is 6+0=66+0=6, and the contribution to the answer is 9×9×6=4869\times9\times6=486.

From 11 to 33, the path uses the subway. The required time is 5+0=55+0=5, and the contribution to the answer is 9×3×5=1359\times3\times5=135.

From 11 to 44, the path does not use the subway. The required time is 66, and the contribution to the answer is 9×2×6=1089\times2\times6=108.

From 11 to 55, the path uses the subway. The required time is 6+9+0=156+9+0=15, and the contribution to the answer is 9×3×15=4059\times3\times15=405.

From 22 to 33, the path uses the subway. The required time is 6+5+0=116+5+0=11, and the contribution to the answer is 9×3×11=2979\times3\times11=297.

From 22 to 44, the path uses the subway. The required time is 6+6+0=126+6+0=12, and the contribution to the answer is 9×2×12=2169\times2\times12=216.

From 22 to 55, the path does not use the subway. The required time is 99, and the contribution to the answer is 9×3×9=2439\times3\times9=243.

From 33 to 44, the path uses the subway. The required time is 5+6+0=115+6+0=11, and the contribution to the answer is 3×2×11=663\times2\times11=66.

From 33 to 55, the path uses the subway. The required time is 5+6+9+0=205+6+9+0=20, and the contribution to the answer is 3×3×20=1803\times3\times20=180.

From 44 to 55, the path uses the subway. The required time is 6+6+9+0=216+6+9+0=21, and the contribution to the answer is 2×3×21=1262\times3\times21=126.

In summary, the answer is 486+135+108+405+297+216+243+66+180+126=2262486+135+108+405+297+216+243+66+180+126=2262.

It can be proven that there is no better subway construction plan.

This problem uses bundled tests and subtask dependencies.

ID Score nn \le Property Dependency
00 N/A Sample None
11 55 1010 None
22 500500 11
33 1010 50005000 1,21,2
44 3030 10510^5 A None
55 B
66 2020 C
77 1515 D
88 1010 None 0,1,2,3,4,5,6,70,1,2,3,4,5,6,7

Special property A: t=0t=0.

Special property B: ui=i,vi=i+1u_i=i, v_i=i+1.

Special property C: The degree of every node is at most 100100.

Special property D: ui=1,vi=i+1u_i=1, v_i=i+1.

Translated by ChatGPT 5