公式

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

gpt-6-astra-medium

Overview

Each device is configured with standard \(0\) or \(1\), and we aim to minimize the sum of the configuration costs and the converter costs for cables connecting devices with different standards. This can be solved using Tree DP, which utilizes the tree structure to find the minimum cost for each standard at each device.

Analysis

Since there are two choices for each device, trying all configurations would result in \(2^N\) possibilities. This will not run in time for \(N \leq 200000\).

Moreover, simply choosing the standard with the lower configuration cost for each device fails to take converter costs into account.

For example, suppose the configuration costs of two devices are \((A_1,B_1)=(0,5)\) and \((A_2,B_2)=(5,0)\), and the converter cost between them is \(100\) yen. If we only consider the configuration costs and choose different standards, the total cost will be \(100\) yen; however, if we align them to the same standard, the total cost is only \(5\) yen.

Here, let us view the tree as a rooted tree with an arbitrary vertex as the root.

Once the standard of a vertex is fixed, the subtrees rooted at its children can be optimized independently. This is because there are no cables directly connecting the subtrees of different children, and we only need to consider the cost of the cable connecting each child to its parent.

Therefore, the problem can be solved by maintaining two minimum costs for each vertex—one for setting it to standard \(0\) and one for standard \(1\)—and computing them from the leaves to the root.

Algorithm

Let device \(1\) be the root. In the code, vertex indices are \(0\)-indexed, so the root is vertex \(0\).

1. DP Definition

For a vertex \(u\), we define the following values:

  • \(dp0[u]\): The minimum cost within the subtree of \(u\) when vertex \(u\) is set to standard \(0\)
  • \(dp1[u]\): The minimum cost within the subtree of \(u\) when vertex \(u\) is set to standard \(1\)

This includes the configuration costs of devices and converter costs of cables within the subtree, but does not include the cost of the cable connecting \(u\) to its parent.

Initially, we set each state to only the configuration cost of the vertex itself:

  • \(dp0[u]=A_u\)
  • \(dp1[u]=B_u\)

2. Adding Children’s Results to the Parent

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

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

  • Set to standard \(0\): Since they have the same standard, the cost is \(dp0[v]\).
  • Set to standard \(1\): Since a converter is required, the cost is \(dp1[v]+c\).

Therefore, we add \(\min(dp0[v],dp1[v]+c)\) for child \(v\).

Similarly, when vertex \(u\) is set to standard \(1\), we add \(\min(dp1[v],dp0[v]+c)\).

From the above, the transition formulas are as follows:

  • \(dp0[u]=A_u+\sum_{v:\,\text{child of }u}\min(dp0[v],dp1[v]+c_{uv})\)
  • \(dp1[u]=B_u+\sum_{v:\,\text{child of }u}\min(dp1[v],dp0[v]+c_{uv})\)

Importantly, once the parent’s standard is fixed, the subtrees of the children become independent, allowing us to simply sum their respective minimum costs.

3. Computing in Order from Children to Parent

Without using recursion, we determine the processing order via the following steps:

  1. Add the root to order.
  2. Retrieve vertices from order in order from the front, and append their children to the end.
  3. Simultaneously, record each vertex’s parent in parent and the converter cost to its parent in parent_cost.

In this order, a parent always appears before its children. Therefore, by processing in reverse order, we can ensure that each child’s DP values are fully computed before adding them to the parent.

In the code, vertices \(u\) other than the root are processed in reverse order, adding the following to the parent \(p\):

  • \(\min(dp0[u],dp1[u]+c)\) to \(dp0[p]\)
  • \(\min(dp1[u],dp0[u]+c)\) to \(dp1[p]\)

For leaves, the initial values are already the correct minimum costs. By aggregating these results up to the parents in order, the DP values for all vertices are correctly computed.

4. Finding the Answer

Since the root can take either standard, the answer is \(\min(dp0[0],dp1[0])\).

Complexity

  • Time Complexity: \(O(N)\)
    Each edge of the tree is visited a constant number of times, and the DP values of each vertex are added to its parent exactly once.
  • Space Complexity: \(O(N)\)
    Used to store the adjacency list, DP arrays, parent information, and processing order.

Implementation Notes

  • Avoid recursion
    If the tree is a simple path (line graph), the depth can be up to \(N\). Since deep recursion often causes issues in Python, the code is implemented using the reverse of order.

  • In a tree, simply ignore the edge leading to the parent
    Because there are no cycles, we can visit each vertex exactly once during traversal simply by verifying that an adjacent vertex is not the parent.

  • Do not double-count converter costs
    The cost of each edge is only considered when adding the child’s DP values to the parent. Furthermore, it is only added to the choices where the standards at both endpoints differ.

  • Be mindful of integer sizes
    The answer may exceed the range of a \(32\)-bit integer. While this is not an issue with Python’s integers, \(64\)-bit integers should be used in other languages.

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

    Source Code

import sys


def main():
    readline = sys.stdin.buffer.readline
    n = int(readline())

    dp0 = [0] * n
    dp1 = [0] * n
    for i in range(n):
        a, b = map(int, readline().split())
        dp0[i] = a
        dp1[i] = b

    graph = [[] for _ in range(n)]
    for _ in range(n - 1):
        u, v, c = map(int, readline().split())
        u -= 1
        v -= 1
        graph[u].append((v, c))
        graph[v].append((u, c))

    parent = [-1] * n
    parent_cost = [0] * n
    order = [0]

    for u in order:
        p = parent[u]
        for v, c in graph[u]:
            if v != p:
                parent[v] = u
                parent_cost[v] = c
                order.append(v)

    for i in range(n - 1, 0, -1):
        u = order[i]
        p = parent[u]
        c = parent_cost[u]
        x = dp0[u]
        y = dp1[u]

        dp0[p] += x if x <= y + c else y + c
        dp1[p] += y if y <= x + c else x + c

    print(min(dp0[0], dp1[0]))


if __name__ == "__main__":
    main()

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