给定重量上限,背包问题_满足给定重量的袋子的最低成本
給定重量上限,背包問題
Problem statement:
問題陳述:
You are given a bag of size W kg and you are provided costs of packets different weights of oranges in array cost[] where cost[i] is basically cost of i kg packet of oranges. cost[i] = -1 means that i kg packet of orange is unavailable.
給您一個大小為W kg的袋子,并為您提供數組cost []中重量不同的橙子的包裝成本,其中cost [i]基本上是i kg橙子包裝的成本。 cost [i] = -1表示i kg包橙色不可用。
Find the minimum total cost to buy exactly W kg oranges and if it is not possible to buy exactly W kg oranges then print -1. It may be assumed that there is infinite supply of all available packet types.
找到購買正好為W千克橘子的最低總成本,如果不可能購買正好為W千克橘子,則打印-1 。 可以假定存在所有可用分組類型的無限供應。
Input:
輸入:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains integers N and W where N denotes the size of array cost[ ] and W is the size of the bag.
輸入的第一行包含一個整數T,表示測試用例的數量。 然后是T測試用例。 每個測試用例的第一行包含整數N和W ,其中N表示數組cost []的大小, W表示袋子的大小。
The second line of each test case contains N space separated integers denoting elements of the array cost[ ].
每個測試用例的第二行包含N個由空格分隔的整數,它們表示數組cost []的元素。
Output:
輸出:
Print the minimum cost to buy exactly W kg oranges. If no such answer exists print "-1".
打印最低成本以購買正好為W公斤的橘子。 如果不存在這樣的答案,請打印“ -1” 。
Constraints:
限制條件:
1 <= T <= 100 1 <= N, W <= 1000 1 <= cost[] <= 1000Example:
例:
Input: 2 5 5 20 10 4 50 100 6 5 -1 -1 4 5 -1 -1Output: 14 -1Explanation:
說明:
So, for the first test case, The 1 kg orange packet costs: 20 The 2 kg orange packet costs: 10 The 3 kg orange packet costs: 4 The 4 kg orange packet costs: 50 The 5 kg orange packet costs: 100We need total of 5 Kg orangeSo, there are many options to pick orange packets Like picking five 1 kg packets costing 100 two 2 kg and one 1kg packet costing 40 one 2kg and one 3kg packet costing 14 one 1kg and one 4kg costing 70 one 5kg costing 100 so minimum cost one would be one 2kg and one 3kg bag combination which costs 14For the second test case, There is no possible combination to sum total 5kg oranges Since, only available weight packets are of 3kg and 4kg We can't take fractional parts So, it's not possible and answer is -1.Solution approach
解決方法
This is kind of a duplicate knapsack problem with some constraints. Here we need to keep the check for available packets too.
這是一個重復的背包問題,但有一些約束。 在這里,我們還需要檢查可用數據包。
For example,
例如,
Say we have total weights 8
假設我們有總重量8
kg and for an instance, we are checking with packet weight 5
公斤,例如,我們正在檢查包裝重量5
In such a case, we need to check to things.
在這種情況下,我們需要檢查一下事情。
Whether the 5kg packet is available or not.
5公斤小包是否可用。
Whether the sub-problem of size (8-5) kg is already solved or not.
(8-5)kg大小的子問題是否已解決。
If both satisfies then only we can proceed with this instance to compute the current problem.
如果兩個都滿足,則只有我們可以繼續進行本實例計算當前問題。
The above concepts lead to the recurring formula which can be easily converted to Dynamic Programming like below,
上面的概念導致了重復出現的公式,可以很容易地將其轉換為動態編程,如下所示,
Initialize dp[w+1] with INT_MAX where w is the total weight which is to be computed;
用INT_MAX初始化dp [w + 1],其中w是要計算的總權重;
Set dp[0]=0 as base case,
將dp [0] = 0設置為基本情況,
C++ Implementation:
C ++實現:
#include <bits/stdc++.h> using namespace std;int minimumCost(vector<int> a, int n, int w) {int dp[w + 1];dp[0] = 0;for (int i = 1; i <= w; i++)dp[i] = INT_MAX;for (int i = 1; i <= w; i++) {for (int j = 0; j < n; j++) {if (a[j] != -1 && (j + 1) <= i && dp[i - (j + 1)] != INT_MAX && dp[i - (j + 1)] + a[j] < dp[i]) {dp[i] = dp[i - (j + 1)] + a[j];}}}if (dp[w] == INT_MAX)return -1;return dp[w]; }int main() {int t, n, item, w;cout << "Enter number of test cases\n";cin >> t;for (int i = 0; i < t; i++) {cout << "Enter number of elements\n";cin >> n;cout << "Enter total weight\n";cin >> w;cout << "Enter the cost of weights, -1 if not available\n";vector<int> a;for (int j = 0; j < n; j++) {scanf("%d", &item);a.push_back(item);}cout << "minimum cost is: " << minimumCost(a, n, w) << endl;}return 0; }Output:
輸出:
Enter number of test cases 2 Enter number of elements 5 Enter total weight 5 Enter the cost of weights, -1 if not available 20 10 4 50 100 minimum cost is: 14 Enter number of elements 6 Enter total weight 5 Enter the cost of weights, -1 if not available -1 -1 4 5 -1 -1 minimum cost is: -1翻譯自: https://www.includehelp.com/icp/minimum-cost-to-fill-given-weight-in-a-bag.aspx
給定重量上限,背包問題
總結
以上是生活随笔為你收集整理的给定重量上限,背包问题_满足给定重量的袋子的最低成本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java DataOutputStrea
- 下一篇: [转载] python 元组tuple