01背包怎么不重復
Problem statement:
問題陳述:
Weights and values are given for n items along with the maximum capacity allowed W. What is the maximum value we can achieve if we can pick any weights, any number of times for the total allowed capacity of W?
給出了n個項目的權重和值以及允許的最大容量W。 如果可以為W的總允許容量選擇任意權重,任意次數,我們可以實現的最大值是多少?
Input:
輸入:
First line contains two integers N and W denoting the number of items and the total allowed weight.
第一行包含兩個整數N和W,分別表示項目數和允許的總重量。
In the next two lines are space separated values of the array denoting values of the items val[n] and their weights weights[n] respectively.
在接下來的兩行中,數組的空格分隔值分別表示項val [n]的值及其權重weight [n] 。
Output:
輸出:
Output the maximum value which we can achieve if we can pick any weights any number of times for a total weight capacity of W.
如果我們可以選擇任意數量的砝碼來獲得總重量W,則輸出可以達到的最大值。
Constraints:
限制條件:
1 <= N,W <= 100
1 <= weight[i], val[i] <= 100
Example:
例:
Test case: 1
Input:
N=2,W= 3
val[]=1 1
weight[]=2 1
Output:
Maximum value that can be achieved: 3
Test case: 2
Input:
N=3,W= 4
val[]=2 5 3
weight[]=1 2 1
Output:
Maximum value that can be achieved: 12
Explanation:
說明:
For the first test case,
Will use the second item with weight 1 only.
We use will 3 such item to have maximum value 3.
Any other combinations will give less value
For the second test case,
Some possible combinations can be
Writing as (value, weight) pair
(2,1), (2,1), (2,1), (2,1)- total weight 4, value 8
(2,1), (2,1), (5,2) - total weight 4, value 9
...
(3,1),(3,1), (3,1),(3,1)- total weight 4, value 12
The last combination is the most optimal one
and that's produces the maximum.
Solution Approach:
解決方法:
The difference between this problem and the general knapsack problem is here we can use the same item several times. So the simple recursion about either choosing an item or leaving the item will not work here as using the same item may lead to the most optimized solution.
這個問題和一般背包問題之間的區別在于,我們可以多次使用同一項目。 因此,關于選擇一個項目或離開該項目的簡單遞歸在這里不起作用,因為使用同一項目可能會導致最優化的解決方案。
Hence, the algorithm will be like following,
因此,該算法將如下所示,
Initialize a dp[w+1] to store for each sub-problem up to total capacity W.
初始化dp [w + 1]為每個子問題存儲直到總容量W。
Reset all the values to zero initially.
最初將所有值重置為零。
memset(dp,0,sizeof(dp));
memset(dp,0,sizeof(dp));
Fill up the DP table with base value dp[0]=0,
用基本值dp [0] = 0填充DP表,
for sub-problem weight,i=1 to w for a weight,wj in the weight array if i>=wj dp[i] = std∷max?(dp[i],dp[i-wj]+valj) end if end for end for
Maximum value that can be achieved is dp[w]
可以達到的最大值是dp [w]
Let's compute the above for our test case two,
讓我們為測試案例二計算以上內容,
N = 3, W = 4
val[] = 2 5 3
weight[] = 1 2 1
Initially the DP table is,
最初,DP表是
To compute dp[1],
要計算dp [1],
weight[0]<=1
So,
dp[1] = max(dp[1],dp[1-1]+val[0]) = 2
Weight[1]>1. So it can't have any role while computing DP[1]
Weight[2]<=1
So,
dp[1] = max(dp[1],dp[1-1]+val[2]) = 3
Similar way you can hand compute the DP table to understand how the algo is performing is reaching to the optimal answer.
您可以用類似的方法手工計算DP表,以了解算法的性能如何達到最佳答案。
The final DP table for the above test case would look like,
上述測試用例的最終DP表如下所示:
Where 12 is the final case.
最后的情況是12。
C++ Implementation:
C ++實現:
#include <bits/stdc++.h>
using namespace std;
void print(vector<int> a, int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int my(vector<int> price, vector<int> weight, int n, int w)
{
int dp[w + 1];
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= n; j++) {
if (i >= weight[j - 1])
dp[i] = std::max(dp[i], dp[i - weight[j - 1]] + price[j - 1]);
}
}
return dp[w];
}
int main()
{
int n, item, w;
cout << "enter number of item and total weights\n";
cin >> n >> w;
cout << "Enter the prices\n";
vector<int> price;
for (int j = 0; j < n; j++) {
scanf("%d", &item);
price.push_back(item);
}
cout << "Enter the weights\n";
vector<int> weight;
for (int j = 0; j < n; j++) {
scanf("%d", &item);
weight.push_back(item);
}
cout << "result is: " << my(price, weight, n, w) << endl;
return 0;
}
Output:
輸出:
enter number of item and total weights
3 4
Enter the prices
2 5 3
Enter the weights
1 2 1
result is: 12
翻譯自: https://www.includehelp.com/icp/knapsack-with-duplicate-items.aspx
01背包怎么不重復