给定重量上限,背包问题_满足给定重量的袋子的最低成本

给定重量上限,背包问题

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测试用例。 每个测试用例的第一行包含整数NW ,其中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[] <= 1000 

Example:

例:

Input:
2
5 5
20 10 4 50 100
6 5
-1 -1 4 5 -1 -1

Output:
14
-1

Explanation:

说明:

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: 100

We need total of 5 Kg orange

So, 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 14


For 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.

在这种情况下,我们需要检查一下事情。

  1. Whether the 5kg packet is available or not.

    5公斤小包是否可用。

  2. 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,

上面的概念导致了重复出现的公式,可以很容易地将其转换为动态编程,如下所示,

  1. Initialize dp[w+1] with INT_MAX where w is the total weight which is to be computed;

    用INT_MAX初始化dp [w + 1],其中w是要计算的总权重;

  2. Set dp[0]=0 as base case,

    将dp [0] = 0设置为基本情况,

  3. for(int i=1;i<=w;i++)
        for(int j=0;j<n;j++)
            if ( a[j]  is availble  && (j+1)<=i && dp[i-(j+1)]  is already computed && dp[i-(j+1)]+a[j]<dp[i])
                dp[i]=dp[i-(j+1)]+a[j];
            end if
        end for
    end for
    
    
  4. if(dp[w]  is not computed over the process,contains still the initial value)
        return -1;
    else dp[w] is the minimum cost
    
    

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

给定重量上限,背包问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
最小重量机器设计问题可以通过使用Java编程语言来解决。该问题的目标是设计一个最小重量机器,使其能够满足给定的功能需求。 以下是一个简单的Java代码示例,用于解决最小重量机器设计问题: ```java public class MinWeightMachine { public static void main(String[] args) { // 定义输入数据 int[] functions = {4, 5, 6}; // 功能需求 int[] weights = {2, 3, 4}; // 设计机器的重量 // 计算最小重量 int minWeight = calculateMinWeight(functions, weights); // 输出结果 System.out.println("The minimum weight of the machine is " + minWeight); } // 计算最小重量的方法 public static int calculateMinWeight(int[] functions, int[] weights) { int minWeight = Integer.MAX_VALUE; // 初始化最小重量 // 遍历所有可能的机器组合 for (int i = 0; i < (1 << functions.length); i++) { int totalFunctions = 0; int totalWeight = 0; for (int j = 0; j < functions.length; j++) { if ((i & (1 << j)) != 0) { totalFunctions += functions[j]; totalWeight += weights[j]; } } if (totalFunctions == 15 && totalWeight < minWeight) { minWeight = totalWeight; } } return minWeight; } } ``` 在上述代码中,我们定义了一个`MinWeightMachine`类,该类包含一个`main()`方法和一个`calculateMinWeight()`方法。`main()`方法用于定义输入数据,并调用`calculateMinWeight()`方法来计算最小重量。`calculateMinWeight()`方法使用位运算来遍历所有可能的机器组合,并计算每种组合的总功能和总重量。如果机器的总功能满足需求且总重量小于当前最小重量,则更新最小重量。最终,`calculateMinWeight()`方法返回最小重量。 需要注意的是,上述代码中的`calculateMinWeight()`方法仅考虑了功能需求的总和等于15的情况。如果需要考虑其他功能需求的情况,可以修改代码中的`if`语句。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值