Official

D - 接続規格と変換器 / Connection Standards and Converters Editorial by admin

imported

Summary

We assign one of two standards to each device connected by tree-structured cables to minimize the sum of the configuration costs for each device and the costs of converters connecting different standards. We use tree DP to find the minimum cost for each standard assigned to the root of the subtree.

Analysis

Trying all configurations of standards yields \(2^N\) possibilities. Simply choosing the cheaper standard for each device independently may lead to high converter costs. For example, if the configuration costs of two devices are \((0,5),(5,0)\) respectively and the converter cost is \(10\), setting both to the same standard for a total cost of \(5\) is cheaper than individually choosing the cheaper standard.

Consider a rooted tree with device \(1\) as the root. If we represent the cost of the subtree at vertex \(v\) with only a single value, we cannot determine whether a converter is needed between \(v\) and its parent. Therefore, we also include the standard of \(v\) in the state.

Let \(dp[v][s]\) be the minimum cost within the subtree of \(v\) when device \(v\)’s standard is fixed to \(s\). This includes device \(v\)’s own configuration cost and the converter costs for all cables within the subtree, but does not include the cost of the cable connecting \(v\) to its parent.

Let \(w\) be a child of \(v\), and let \(c\) be the converter cost between the two vertices. When the standard of \(v\) is \(0\), the minimum cost of this child branch is

\[ \min(dp[w][0],\ dp[w][1]+c) \]

This is because if the child also has standard \(0\), no converter is needed, and if it has standard \(1\), a cost of \(c\) is added. Similarly, when the standard of \(v\) is \(1\), it is

\[ \min(dp[w][1],\ dp[w][0]+c) \]

In a tree, there are no other cables connecting the subtrees of different children. Once the standard of \(v\) is fixed, the standard of each child and the configurations further down can be chosen independently of one another. Therefore, we can initialize \(dp[v][0]\) with \(A_v\) and \(dp[v][1]\) with \(B_v\), and add the above minimum value for each child.

Correctness

Since a leaf’s subtree contains only a single device, the initial values \(A_v,B_v\) are indeed the minimum costs for the respective standards.

Assume that the DP values for each child are correct, and fix the parent’s standard. In any configuration, the child’s standard is either \(0\) or \(1\), and the cost required for that branch is at least the sum of the corresponding DP value and the converter cost between parent and child. Therefore, the sum of the minimum values of each branch plus the parent’s own configuration cost is less than or equal to the cost of any configuration.

On the other hand, for each child, we can choose the standard that yields the smaller value and adopt the configuration that achieves that DP value. Because the subtrees of the children are disjoint and the parent’s standard is fixed, these can be achieved simultaneously. Thus, the calculated lower bound is attainable, and the parent’s DP is also correct.

By induction from the leaves to the root, all DP values are computed correctly. Since the root has no cable to a parent, the answer is \(\min(dp[1][0],dp[1][1])\).

Algorithm

  1. Store the devices and cables as an adjacency list, and initialize the two DP values for each device with \(A_i,B_i\).
  2. Traverse the tree iteratively from the root, recording each vertex’s parent, the converter cost to its parent, and the visiting order where parents appear before children.
  3. Traverse the visiting order in reverse, and for each vertex other than the root, add the minimum cost of each branch from its DP values to its parent’s two DP values.
  4. Output the smaller of the two DP values of the root.

By processing in reverse order, all values of a vertex’s children are aggregated before that vertex passes its values to its parent.

Complexity

Let \(N\) be the number of devices.

The time complexity is \(O(N)\). The tree traversal processes each cable a constant number of times, and the DP aggregation processes each vertex once.

The space complexity is \(O(N)\). We store the adjacency list, parents, visiting order, cable costs to parents, and the two DP values for each vertex.

Implementation Notes

If all devices are assigned the same standard, no converters are needed. Since this configuration is also feasible in a subtree with a fixed standard, each minimum DP value is at most \(N\times10^9\). Values obtained by adding converter costs during the computation will also fit within the range of a signed \(64\)-bit integer. We use long long for the costs.

The traversal does not use recursion, preventing the call stack from growing too deep even if the tree is a simple line of length \(N\). When \(N=1\), no aggregation is performed, and the smaller of the two configuration costs is the answer.

Source Code

#include <bits/stdc++.h>
using namespace std;
struct Edge { int to; long long cost; };
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<array<long long, 2>> dp(n);
    for (auto &d : dp) cin >> d[0] >> d[1];
    vector<vector<Edge>> graph(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        long long c;
        cin >> u >> v >> c;
        --u; --v;
        graph[u].push_back({v, c});
        graph[v].push_back({u, c});
    }
    vector<int> parent(n, -1), order{0};
    vector<long long> parent_cost(n);
    parent[0] = 0;
    for (int at = 0; at < n; ++at) {
        int v = order[at];
        for (auto e : graph[v]) {
            if (e.to == parent[v]) continue;
            parent[e.to] = v;
            parent_cost[e.to] = e.cost;
            order.push_back(e.to);
        }
    }
    for (int at = n - 1; at > 0; --at) {
        int v = order[at], u = parent[v];
        long long c = parent_cost[v];
        dp[u][0] += min(dp[v][0], dp[v][1] + c);
        dp[u][1] += min(dp[v][1], dp[v][0] + c);
    }
    cout << min(dp[0][0], dp[0][1]) << '\n';
}

This editorial was generated by imported.