给定重量上限,背包问题

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

给定重量上限,背包问题

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

  1. 查找两个字符串中相同字符串_使两个字符串相同的最低成本

    查找两个字符串中相同字符串 Problem statement: 问题陈述: Given two strings string1 and string2 find the minimum cost r ...

  2. 如何打印出给定尺寸的方格_打印给定号码的表格| 8086微处理器

    如何打印出给定尺寸的方格 Problem statement: 问题陈述: Write an assembly language program in 8086 to print the table ...

  3. 如何打印出给定尺寸的方格_打印给定号码的表格| 8085微处理器

    如何打印出给定尺寸的方格 Problem statement: 问题陈述: Write an assembly language program in 8085 to print the table ...

  4. tdd 单元测试_何时给定在单元测试和TDD中的重要性

    tdd 单元测试 最近,我一直在写与自动测试有关的更高级的概念(主要与Spock有关). 但是,在进行测试培训时,我清楚地看到,通常对特定工具的知识并不是主要问题. 即使使用Spock,也可以编写肿且 ...

  5. java编写铝材公式_铝材的重量和单价的计算公式

    满意答案 nfpa272 推荐于 2017.11.18 采纳率:51%    等级:12 已帮助:23470人 1, 铝材重量计算公式大全: 方铝棒重量(公斤)=0.0028×边宽×边宽×长度 方紫铜 ...

  6. python重量计算月球上物体体重是地球上的16.5%_质量和重量一样吗?他们怎么转换?...

    在我们的白话里,"质量"和"重量"是经常无缝互换的术语,但从技术上讲,它们绝不是双胞胎.在科学术语中,"重量"和"质量" ...

  7. 12个乒乓球,其中有11个球每个球重量一模一样,另外1个球重量和那11个球不一样.用天平称三次,把单独的球(和那11个重量不一样的球)找出来

    12个乒乓球,其中有11个球每个球重量一模一样,另外1个球重量和那11个球不一样.用天平称三次,把单独的球(和那11个重量不一样的球)找出来 代码展示 import random a = random ...

  8. 一维,多维背包问题(体积,重量)

    这里列出了在只存在体积和存在体积,重量两种情况下背包问题的解决方法 第一种情况: 某人从外地贩货物回本省出售 有3种货物: A货物,单个重量80KG,单个价值60块 B货物,单个重量50KG,单个价值 ...

  9. 编译原理实验报告_任意给定一个正规式 r (包括连接、或、闭包运算),根据 Thompson算法设计一个程序,生成与该正规式等价的 NFA N 。

    任意给定一个正规式 r (包括连接.或.闭包运算),根据 Thompson算法设计一个程序,生成与该正规式等价的 NFA N . 百度网盘下载 传送门 提取码:bzjn

最新文章

  1. ExtJS(3)- 自定义组件(星级评分)
  2. NHibernate之旅(8):巧用组件之依赖对象
  3. win服务器自动发邮件,windows关机前执行脚本设置与关机blat自动发送邮件脚本模板...
  4. (转载)WebStorm 2018.3.2 激活方式(永久)亲测好用!!!!!!
  5. WEB安全基础-Javascrp相关知识点之DOM
  6. Python开发人员最喜欢的工具
  7. 获取css style值
  8. 航模飞机设计基础知识
  9. php探针作用,X 探针(刘海探针)一款开源又好用的PHP探针
  10. matlab中rgb2ycbcr函数,RGB转YCbCr
  11. wordpress 漂亮的Cosy主题
  12. 数组常用操作。以逗号隔开、以逗号+单引号隔开、转List等
  13. 基于深度学习的物体识别系统
  14. html 画梯形容器,css怎么画梯形?
  15. 15.1 计算几何 (用海伦公式)——【三角形的面积】
  16. 手机桌面左右滑屏不成功问题log分析
  17. 如何在公司里体现前端的价值以及提升自己的议价能力
  18. [SSD固态硬盘技术 8] 固件概述和固件升级
  19. win7常用工具软件记录之爱奇艺视频格式qsv转flv工具(附加下载地址)
  20. 学习笔记——SpringBoot使用nutz框架时报错

热门文章

  1. python如何删除对象属性_如何优雅的删除对象中的指定属性?
  2. python做什么模型_主题模型初学者指南[Python]
  3. android条形图,MPAndroid组条形图未显示
  4. java类构造方法成员方法练习_面向对象方法论总结 练习(一)
  5. mysql 查询表的key_mysql查询表和字段的注释
  6. fifo页面置换算法设计思路_千万级并发!如何设计一个多级缓存系统?
  7. android layout.inflater,Android - LayoutInflater
  8. 说说python程序的执行过程_表示说的词语
  9. 如果删除网上服务器登陆账号密码,怎么清除SVN的用户名和密码
  10. ios 点击出现另外一套tabbar_IOS 点击tabbaritem跳转到一个新界面,且隐藏tabbar