Tree Transformation Cost
Time limit3sMemory limit1024 MB
Find the minimum cost to turn one weighted ordered rooted tree into another using grow, expand, contract, and relabel operations.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, Recursion
- Solved
- No attempts yet
Problem
You are given two weighted rooted trees. For each vertex, the children are ordered from left to right. There are four elementary operations.
-
Growing: for a vertex with children , add a new vertex and an edge between and , then insert at position of the children list, so the list becomes . The cost is times the weight of the new edge .
-
Expansion: for a vertex with children , choose an interval with . Add a new vertex as the parent of , and add an edge between and . The children list of becomes , and the children list of is . Each edge keeps the weight of from the original tree. The cost is times the weight of the new edge .
-
Contraction: for a vertex , choose a child whose children list is , and contract the edge . Vertex is removed, and the children list of becomes . Each edge keeps the weight of . The cost is times the weight of the edge in the original tree.
-
Relabeling: for a vertex and its child , change the weight of edge from to . The cost is .
An edge added by growing or expansion cannot be relabeled. An edge that has been relabeled cannot be contracted.
You want to turn the first tree into the second tree. Output the minimum total cost. Two trees are the same if there is a bijection between their vertices that preserves the root and the order of children, and the weights of corresponding edges match.
Input
The first line contains three integers , , and (). Then the two trees follow. For each tree, the first line contains , the number of vertices. Each of the next lines starts with , the number of children, followed by integers (), which give the children list and the weights of the edges to the children.
The first tree has at most 50 vertices, and the second tree has at most 2000 vertices.
Output
Print the answer.