Practice Season
Time limit2sMemory limit128 MB
Both teams insert rest days into their fixed city orders to minimize combined stadium and hotel costs.
- Level
Medium6 of 10
- Topics
- Dynamic programming, String matching
- Solved
- No attempts yet
Problem
Two professional baseball teams, X and Y, spend a practice season visiting 7 cities in a fixed order. The 7 cities are labeled c1, c2, c3, c4, c5, c6, c7. Both teams must start the practice season on the same day and finish it on the same day.
During the practice season, each day a team either visits one city to train or checks into a hotel and rests for the whole day (a rest day). The order of cities each team must visit is already fixed and cannot be changed, but rest days may be inserted freely wherever it helps.
For example, if a team's order of cities is S = <c1, c2, c3, c1, c4, c1, c5, c4>, rest days R can be inserted to produce new schedules such as:
S1 = <c1, c2, R, c3, c1, R, R, c4, c1, c5, R, c4>S2 = <R, R, c1, c2, c3, c1, R, c4, R, c1, c5, c4>
Stadium fee. Training at a stadium costs for a team, the same for all 7 cities. If both teams X and Y use the same stadium on the same day, the total is , so each team pays only , which is cheaper. If they train at different stadiums, each pays , for a total of .
Hotel fee. On a rest day a team rents an entire hotel. Renting incurs a one-time exclusive-use fee , plus a per-day charge for the length of the stay. That is, staying consecutive days at a hotel costs . For example, with and , staying 5 consecutive days costs , whereas staying on 5 separate days pays the exclusive fee five times plus five days of charges, for .
If both teams keep their visiting orders with no adjustment, the schedule is Table 1. Here team Y rests on the last two days (days 7 and 8) so that it starts and ends together with team X.
Table 1. Simple schedule A
With stadium fee , hotel exclusive fee , and daily fee , following Table 1 costs the two teams as shown in Table 2.
Table 2. Cost of schedule A
So the total cost is . Now consider a slightly different schedule. As in Table 3, inserting rest days into team Y's schedule increases the number of days the two teams share a stadium, lowering the total cost.
Table 3. Schedule B
Here the total cost is , saving compared with schedule A. Note that the optimal schedule can differ depending on the values of , , and .
Given the orders of cities visited by teams X and Y, find the best schedule that minimizes the sum of the two teams' costs, and print that minimum total cost.
Input
Input is given on standard input. The first line contains the number of test cases . Each test case consists of three lines.
- The first line contains integers , , and separated by single spaces. All three are positive integers not greater than 10.
- The next two lines give the order of cities visited by team X and by team Y, respectively, separated by single spaces, with the end of each line marked by the number 0. Each city is an integer from 1 to 7.
The number of cities satisfies .
Output
For each test case, print on its own line the minimum cost to complete the practice season (the sum of X's and Y's costs).