Power Plants
InterviewTime limit2sMemory limit128 MB
Given restart costs between plants and which plants are already on, find the minimum total cost to reach at least P working plants, or -1.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Bit manipulation, Greedy, Graph
- Solved
- No attempts yet
Problem
Eunjin works at a power plant facility. While she briefly fell asleep, several power plants broke down, and her boss is now walking toward her office. She must restore enough plants before the boss arrives.
A broken plant can be restarted by using a plant that is already working. The restart cost depends on which working plant is used and which broken plant is restarted.
Find the minimum total cost needed to make at least P power plants working.
Input
The first line contains the number of power plants, N. N is a positive integer no greater than 16.
The next N lines contain the cost matrix. The j-th value on the i-th line is the cost of restarting plant j by using plant i.
The following line contains a string of length N describing the current state of each plant. Y means the plant is on, and N means it is off.
The last line contains the target count P. Every cost is a nonnegative integer no greater than 36, and P is an integer from 0 to N.
Output
Print the minimum cost required to make at least P plants working. If it is impossible, print -1.