HDU 1085 multiple knapsack problem
Multiple Knapsack Problem
The Meaning of the problem
You have three kinds of coins. Their face values are 1,2 and 5. Of cause you only have a limited number of coins. You need to find the minimum value that you can’t represent exactly with these coins.
Solution
There are lots of people solve this problem using Generating function. That way is too hard to understand. We just need to think of this problem as a multiple knapsack problem.
I learned from this blog
dp[j] = 1
means that j
value can be represent by the coins.
Set dp[0] = 1
initially. And the transfer equation is dp[j] = max(dp[j],dp[j-k*money[i]])
Let’s assume there is only one 1 yuan coin, one 2 yuan coin and one 5 yuan coin. When d[j]
need changing, we should see whether d[j-value]
can be represent.
The top row of the table shows the total amount of money. The leftmost column of the table shows the face value of the coins.
Here’s the picture to show how the sequence is changed.
The top row of the table shows the total amount of money. The leftmost column of the table shows the face value of the coins.
The answer is i
where dp[i] = 0
.
Code
1 |
|