An energy company needs a control center to manage a distributed wind farm. Due to…
- User
An energy company needs a control center to manage a distributed wind farm. Due to terrain and other considerations, the cost to reach a group of windmills is specific to each location. Locate the control center to minimize the cost. Return the minimum cost.
• The control center is located at integral coordinates (cx, cy).
• Wind farm / is located at integral coordinates (x[i], y[i]).
• The distance farm i is the Manhattan distance, i.e., distance[i] = [x[i]-cx|+ (y[i]-су/.
• The cost to connect farm i is premium[i] *distance[i].
• The total cost to connect all farms to the control center is the sum of the cost to connect each farm.
Determine the minimum cost to connect to the control center.
Example
x = [1, 3, 2, 4]
y = [1, 2, 3, 4]
premium = [1, 3, 2, 4] The optimal location is at position [3, 3].
left
Farm #
Location
Distance
Premium
Cost
L
0
(1, 1)
|1 - 3| + |1 - 3| = 4
1
4 * 1 = 4
1
(3, 2)
|3 - 3| + |2 - 3| = 1
3
1 * 3 = 3
2
(2,3)
|2 - 3| + |3 - 3| = 1
2
1^ * 2 = 2
3
(4,4)
|4 - 3| + |4 - 3| = 2
4
2^ * 4 = 8
The cost to connect all wind farms is 17.
Function Description
Complete the function windFarms in the editor below.
windFarms has the following parameter(s):
int premium[n]: the distance multiplier for the cost to connect
int x[n]: x coordinates of the wind farms
int y[n]: y coordinates of the wind farms
Returns
int: the minimum cost to connect the wind farms
Constraints
• 1 <= n <= 10 ^ 3
• 1 <= x[i], y[i] <= 10 ^ 4
• 1 s premium[i] ≤ 50 ▼Input Format for Custom Testing
Input from stdin will be processed as follows and passed to the function.
The first line contains an integer n, the size of the array premium.
The next n lines each contain an element, premium[i], where 0 <= i < n
The next line contains an integer n, the size of x[.
The next n lines each contain an element, x[i].
The next line contains an integer n, the size of y.
The next n lines each contain an element, y[i].
Note: n is the same value in each instance.
- Assistant