Problem Statement
Alice and Bob are playing a popular game called Overcooked. In this game, players work together to cook dishes and serve them to customers. We'll be discussing a simplified version of the game in this problem.
Alice and Bob wish to prepare a single dish. The recipe consists of n steps, numbered 0 through n-1. The numbering is arbitrary and does not influence the order in which the steps can be completed.
Step i takes s[i] seconds to complete. Each step takes as input some (possibly none) ingredients formed in other steps, and produces an ingredient that can be used in future steps. Note that once an ingredient is produced, it can be used in arbitrarily many future steps.
Formally, we can model the recipe as a directed acyclic graph. Each node represents one step of the recipe. An arc from node a[i] to node b[i] means that the ingredient produced in step a[i] is used as input for step b[i]. There will always be exactly one node with outdegree zero. This node represents the final product. (Note that there may be more than one node with indegree zero.)
Each step of the recipe has to be executed exactly once. The steps may be executed in any valid order. Alice and Bob are expert players, hence they are able to execute arbitrarily many steps at the same time, and still complete each of them in the specified number of seconds. However, they can only start a step when all input ingredients for that step are ready.
Note that Alice and Bob do not have to start a step as soon as the resources become available. Instead, they may wait and start the step some seconds later. The only restriction is that the starting time of each step is required to be an integer.
Some ways to execute the recipe are better than others. In particular, whenever there is a long time between the moment an ingredient is produced and the moment it is used, the ingredient will become stale and this will negatively influence the quality of the dish.
We can measure this as follows: At the beginning, the staleness of the dish is zero. Once a step finishes and an ingredient has been produced, we start a timer for that ingredient. Whenever that ingredient is later used in another step of the recipe, we increase the staleness of the dish by t^2, where t is the time elapsed since that ingredient was produced. (Note that each ingredient contributes to the staleness of the dish each time it is used.)
You are given the int[] s with the times needed to perform the individual steps. You are also given the int[]s a and b that describe the dependences between steps: for each valid i, the ingredient produced in step a[i] is used as one of the inputs for step b[i].
Compute and return the smallest possible total staleness of the dish if Alice and Bob cook it according to the above rules. (Note that Alice and Bob are not required to finish as quickly as possible, we only care about the total staleness as defined above.)
Definition
- Class:
- Overcooked
- Method:
- minStale
- Parameters:
- int[], int[], int[]
- Returns:
- int
- Method signature:
- int minStale(int[] s, int[] a, int[] b)
- (be sure your method is public)
Constraints
- s will have between 1 and 20 elements, inclusive.
- Each element of s will be between 1 and 20.
- The sum of elements of s will be at most 100
- a,b will have the same number of elements.
- a will have between len(s)-1 and 20 elements, inclusive.
- a,b will describe a valid directed acyclic graph with len(s) nodes..
- There will be exactly one vertex with outdegree 0.
Examples
{10,10,3,4}{0,1,0,3,0}{1,2,3,2,2}Returns: 118
There are four steps. One optimal strategy is as follows: Do step 0 at time 0 Do step 1 at time 10 Do step 3 at time 13 Do step 2 at time 20 For step 1, the single input ingredient has been left on the counter for 0 seconds, so the staleness is 0. For step 3, the single input ingredient has been left on the counter for 3 seconds, so the staleness is 9. For step 2, there are three input ingredients. The one from step 1 has staleness 0. The one from step 3 has staleness 16. The one from step 0 has staleness 100. It can be shown that there is no other strategy with a strictly smaller staleness.{12,19,18,1,3,2}{0,1,2,3,4}{1,2,3,4,5}Returns: 0
{12,19,18,1,3,2}{0,1,2,3,4,0,1,2,3,0,1,2,0,1,0}{1,2,3,4,5,2,3,4,5,3,4,5,4,5,5}Returns: 6050
{1,2,3,4,5,6,7,8,9,10}{3,3,3,3,3,3,3,3,3,0,1,2,4,5,6,7,9}{0,1,2,4,5,6,7,9,8,8,8,8,8,8,8,8,8}Returns: 226
{6,7,11,7,6,7,5,11,11,10,19}{0,2,2,2,3,3,3,4,4,4,4,5,6,6,7,7,8,9,10,10}{1,0,1,8,0,7,8,0,1,3,5,9,1,3,0,1,5,7,1,9}Returns: 14464