Parks of Jakarta
Time limit2sMemory limit512 MB
Given an initial stacking of N bricks over three parks plus up to 16 target configurations, find the cheapest move sequence visiting all targets and ending with all bricks in one park.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Brute force, Bit manipulation
- Solved
- No attempts yet
Problem
There are three parks in Jakarta: Park 1, Park 2, and Park 3. The new governor of Jakarta wants to decorate these parks by stacking bricks in them. There are bricks in Jakarta, numbered from to , where a smaller number means a smaller brick. A bigger brick must never sit on top of a smaller brick, so brick may be placed directly on top of brick only when .
A brick configuration is a distribution of the bricks over the three parks. Each park holds at most one stack of bricks, and every stack must respect the rule above. For example, when , one valid configuration keeps brick (on top) and brick (at the bottom) in Park 1, leaves Park 2 empty, and keeps brick in Park 3.
One operation transforms a configuration as follows.
- Choose two different parks and . Take the topmost brick of the stack in Park and place it on top of the stack in Park . The operation is invalid when Park is empty, and it is also invalid when the resulting stack in Park breaks the stacking rule.
- Moving a brick requires a rented truck, so each move from Park to Park costs . The cost from Park to Park may differ from the cost from Park to Park .
The process starts from a given initial configuration. The governor wants to see each of chosen configurations at least once, in any order. At the end, all bricks must stand stacked in a single park. Find the minimum possible total cost.
Formally, let the desired configurations be . Find a sequence of configurations () with minimum cost such that:
- is the initial configuration.
- For every desired configuration there is an integer () with . The visiting order does not matter.
- In all bricks are stacked in one park.
- For every , is obtained from by a single operation.
- The cost of the sequence is the sum of the single-operation costs over all .
Input
The first line contains two integers (, ): the number of bricks and the number of configurations to visit. Each of the next three lines contains three integers. The -th integer on the -th line is (, ), the cost of moving one brick from Park to Park . The next three lines describe the initial configuration, followed by blocks of three lines describing the desired configurations. Every configuration uses the following format.
One configuration is written in three lines. The -th line starts with an integer (), the number of bricks in Park , followed by integers () naming the bricks in Park . For all , holds, and every integer from to appears exactly once across the three lines.
Output
Print the minimum total cost that satisfies the governor's demand, in one line.
Hint
Explanation of the first case
In the first case, Park 1 initially holds bricks and while Park 3 holds brick . Since , it suffices to gather all bricks into one park, which can be done with cost as follows.
- Move brick from Park 3 to Park 2. This costs .
- Move brick from Park 1 to Park 2. This costs .
- Move brick from Park 2 to Park 3. This costs .
- Move brick from Park 1 to Park 2. This costs .
- Move brick from Park 3 to Park 2. This costs .
All bricks are now in Park 2 with total cost , and no cheaper sequence exists. Note that the number of operations is not minimized.
Explanation of the second case
In the second case, the optimum visits the second desired configuration before the first one. From the initial configuration, the second configuration is reachable in operations with total cost . From the second configuration, the first configuration is reachable in operations with total cost . Since the first configuration already stacks all bricks in one park, no further operation is needed. The total cost is therefore .