Official

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

gpt-6-astra-medium

Overview

We assign standard \(0\) or \(1\) to each device to minimize the sum of the configuration costs and the converter costs for cables connecting devices with different standards. By utilizing the tree structure, this problem can be solved using tree DP, which computes the minimum cost for each standard at each vertex.

Analysis

Simply choosing the cheaper standard for each device is insufficient

Even if the configuration cost of an individual device is cheaper, if it differs from the standard of an adjacent device, a converter cost will be incurred.

For example, consider devices \(1\) and \(2\) connected by a cable, with the following costs:

  • Device \(1\): \(A_1=0,\ B_1=5\)
  • Device \(2\): \(A_2=5,\ B_2=0\)
  • Converter: \(C=10\)

If we choose the cheaper standard for each device, the standards differ, resulting in a total cost of \(0+0+10=10\) yen. On the other hand, if we assign the same standard to both, the total cost is only \(5\) yen.

Therefore, we must consider configuration costs and converter costs together.

On a tree, fixing the parent’s standard allows subtrees of children to be considered independently

Trying all possible standard assignments would take \(2^N\) ways, which will not run within the time limit.

Thus, we view the tree as rooted at device \(1\). Once the standard of a vertex \(v\) is fixed, the subtree of each child can be optimized independently, because in a tree, there are no edges directly connecting different children’s subtrees.

From the parent’s perspective, we only need two values: “the minimum cost when the child vertex is set to standard \(0\)” and “the minimum cost when it is set to standard \(1\).” There is no need to keep track of every assignment within the subtree.

Algorithm

1. Define DP states

For the subtree rooted at vertex \(v\), we define the following values:

  • \(dp0[v]\): Minimum cost when vertex \(v\) is set to standard \(0\)
  • \(dp1[v]\): Minimum cost when vertex \(v\) is set to standard \(1\)

These values include the configuration costs of all devices in the subtree and the converter costs of all cables within the subtree. However, they do not include the cost of the cable connecting vertex \(v\) to its parent.

First, initialize them with the configuration cost of the vertex itself:

\(dp0[v]=A_v\)

\(dp1[v]=B_v\)

For leaf vertices, which have no children, these values are immediately the final answers for their subtrees.

2. Add children’s results to the parent

Let \(u\) be a child of vertex \(v\), and let \(c\) be the converter cost of the cable between them.

If vertex \(v\) is set to standard \(0\), there are two choices for child \(u\):

  • Set to standard \(0\): No converter is needed, so the cost is \(dp0[u]\)
  • Set to standard \(1\): A converter is needed, so the cost is \(dp1[u]+c\)

Therefore, we update as follows:

\(dp0[v]\mathrel{+}=\min(dp0[u],\ dp1[u]+c)\)

Similarly, if vertex \(v\) is set to standard \(1\):

\(dp1[v]\mathrel{+}=\min(dp1[u],\ dp0[u]+c)\)

We perform this for all children. When the standard of vertex \(v\) is fixed, each child’s subtree is independent, so we can simply sum up their minimum costs.

By computing these transitions from the leaves up to the root, we correctly find the DP values for each vertex.

3. Compute in order from children to parents

In the provided implementation, this is processed without recursion as follows:

  1. Traverse the tree from the root, recording each vertex’s parent in parent.
  2. Record the cost of the cable to the parent in parentCost.
  3. Store the traversal order in order, where parents appear before their children.
  4. Process order in reverse, adding each vertex’s DP values to its parent.

Reversing order ensures that all children are processed before their parent. Thus, by the time a vertex contributes to its parent, its own DP values are already fully computed.

4. Choose the answer from the root’s two states

Since the root has no parent, its subtree is the entire tree. The root can take either standard, so the final answer is:

\(\min(dp0[\text{根}],\ dp1[\text{根}])\)

In the code, since 0-indexed vertex numbering is used, we output min(dp0[0], dp1[0]).

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(N)\)

Each vertex and edge is processed a constant number of times. The adjacency list, DP arrays, parent information, and traversal order each use \(O(N)\) space.

Implementation Details

  • Manage costs using long long
    While individual costs are at most \(10^9\), the total sum may exceed a 32-bit integer. Even if all device and cable costs are summed, it is at most \((2N-1)\times 10^9\), which can be handled using 64-bit integers.

  • Process in reverse order without recursion
    If the tree is a line, the depth can be up to \(N\). To avoid stack overflow caused by deep recursion, the code builds order and computes the DP in reverse order.

  • Add converter costs when propagating results to the parent
    Do not include the cable cost to the parent in the child’s DP; instead, add it only to the choice where the parent and child have different standards. This prevents double counting.

  • \(N=1\) can be handled with the same logic
    No updates from children to parents are performed, and \(\min(A_1,B_1)\) is simply output as is.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;

    vector<long long> dp0(N), dp1(N);
    for (int i = 0; i < N; ++i) {
        cin >> dp0[i] >> dp1[i];
    }

    vector<vector<pair<int, long long>>> graph(N);
    for (int i = 0; i < N - 1; ++i) {
        int u, v;
        long long c;
        cin >> u >> v >> c;
        --u;
        --v;
        graph[u].emplace_back(v, c);
        graph[v].emplace_back(u, c);
    }

    vector<int> parent(N, -1), order;
    vector<long long> parentCost(N, 0);
    order.reserve(N);
    order.push_back(0);

    for (int i = 0; i < N; ++i) {
        int v = order[i];
        for (auto [u, c] : graph[v]) {
            if (u == parent[v]) continue;
            parent[u] = v;
            parentCost[u] = c;
            order.push_back(u);
        }
    }

    for (int i = N - 1; i > 0; --i) {
        int v = order[i];
        int p = parent[v];
        long long c = parentCost[v];
        dp0[p] += min(dp0[v], dp1[v] + c);
        dp1[p] += min(dp1[v], dp0[v] + c);
    }

    cout << min(dp0[0], dp1[0]) << '\n';
    return 0;
}

This editorial was generated by gpt-6-astra-medium.

posted:
last update: