一、Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the
exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.

You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

Input

The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The
last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

二、题解

       这道题之前做过一道背包的题,但是当时觉得看不下去,就搁置了。做这道题时也没太多考虑背包,就是随着感觉做了一通,最后虽然做出来了,但是TLE。于是就狠心地看了背包的内容。参考了背包问题九讲背包之01背包、完全背包、多重背包详解
— TankyWoo
,对照着看了一遍,渐渐有了感觉了。

       背包问题其实主体是动态规划,背包分为:

01背包(ZeroOnePack): 有N件物品和一个容量为V的背包,每种物品均只有一件。第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。

完全背包(CompletePack): 有N种物品和一个容量为V的背包,每种物品都有无限件可用。第i种物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。

多重背包(MultiplePack): 有N种物品和一个容量为V的背包,第i种物品最多有n[i]件可用。每件费用是c[i],价值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。

比较三个题目,会发现不同点在于每种背包的数量,01背包是每种只有一件,完全背包是每种无限件,而多重背包是每种有限件。

        关于背包的知识请点击上面的两个链接。

        对于这道题目呢,用到的是多重背包,即每种钱币有有限个。相对背包问题而言,此问题钱币有价值没有重量,它只有一个属性。而且不要求求出最大的值,只要求求出最多能构成几种价格。所以,这里用到了boolean 类型的dp数组记录,下标为价格,值为true表示可以由钱币构成,false则不行。至于具体实现,跟完全背包的实现差不多,内层循环采用顺序,但是不同的是要控制次数。

import java.util.Scanner;
public class Main{
public static void main(String args[])
{
int N, M;
Scanner sc=new Scanner(System.in);
while(sc.hasNext())
{
N=sc.nextInt();
M=sc.nextInt();
if(N==0&&M==0)
break;
int a[]=new int[100];
int c[]=new int [100];
for (int i=0; i< N; i++)
a[i]=sc.nextInt();
for (int i=0; i< N; i++)
c[i]=sc.nextInt(); int nRes=0;
boolean dp[]=new boolean[100001];
dp[0]=true; for (int i=0; i< N; i++){
int num[]=new int[100001];
for (int j=a[i]; j<=M; j++){
if (!dp[j] && dp[j-a[i]] && num[j-a[i]]< c[i]){
dp[j]=true;
num[j]=num[j-a[i]]+1;
nRes++;
}
}
}
System.out.printf("%d\n", nRes);
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj 1742 Coins(多重背包)的更多相关文章

  1. POJ 1742 Coins(多重背包, 单调队列)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  2. POJ 1742 Coins (多重背包)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 Descriptio ...

  3. poj 1742 coins_多重背包

    题意:给你N个种硬币,价值和数量,知道手表不大于m,问能组成(1~m)的价格有多少种情况 套套上次那题的模板直接就行了,http://blog.csdn.net/neng18/article/deta ...

  4. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  5. hdu 2844 poj 1742 Coins

    hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...

  6. poj 1742 Coins (多重背包)

    http://poj.org/problem?id=1742 n个硬币,面值分别是A1...An,对应的数量分别是C1....Cn.用这些硬币组合起来能得到多少种面值不超过m的方案. 多重背包,不过这 ...

  7. poj 1742 Coins(二进制拆分+bitset优化多重背包)

    \(Coins\) \(solution:\) 这道题很短,开门见山,很明显的告诉了读者这是一道多重背包.但是这道题的数据范围很不友好,它不允许我们直接将这一题当做01背包去做.于是我们得想一想优化. ...

  8. POJ 1742 Coins ( 经典多重部分和问题 && DP || 多重背包 )

    题意 : 有 n 种面额的硬币,给出各种面额硬币的数量和和面额数,求最多能搭配出几种不超过 m 的金额? 分析 : 这题可用多重背包来解,但这里不讨论这种做法. 如果之前有接触过背包DP的可以自然想到 ...

  9. POJ 1742 Coins 【多重背包DP】

    题意:有n种面额的硬币.面额.个数分别为A_i.C_i,求最多能搭配出几种不超过m的金额? 思路:dp[j]就是总数为j的价值是否已经有了这种方法,如果现在没有,那么我们就一个个硬币去尝试直到有,这种 ...

随机推荐

  1. hdu 2036 改革春风吹满地【求多边形面积模板】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2036 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. [note]fhq_treap

    fhq_treap 这东西据说是某个叫范浩强的神仙搞出来的, 他的这种treap可以不用旋转并且资磁很多平衡树操作, 复杂度通过随机的键值来保证(树大致平衡,期望一次操作复杂度\(logn\)) 依靠 ...

  3. bash: /home/aprilpeng/.bashrc: Permission denied

    bash: /home/aprilpeng/.bashrc: Permission denied 出现这样的权限问题,一般是在切换用户的时候,用户没有该目录的权限 可以$chown -R git:us ...

  4. full_case parallel_case学习心得

    一般情况下,DC把case语句综合成选择器电路,但也可能把case语句综合成优先权译码电路.有时,优先权译码电路是不必要的,这是可以使用“// synopsys parallel_case”引导语句强 ...

  5. python3 生成随即激活码

    import string import random #激活码中的字符和数字 field = string.ascii_letters + string.digits #获得四个字母和数字的随即组合 ...

  6. 关于Unicode转为str的方法

    unicode_a=u'\u810f\u4e71' str_a=unicode_a.encode('unicode-escape').decode('string_escape')

  7. mysql 中 all any some 用法

    -- 建表语句 CREATE TABLE score( id INT PRIMARY KEY AUTO_INCREMENT, NAME ), SUBJECT ), score INT); -- 添加数 ...

  8. vs2008 发布网站时丢失文件问题

    右键指定的文件->属性, 将生成操作更改成为"内容"就可以了.

  9. CodeForces - 580C Kefa and Park 【BFS】

    题目链接 http://codeforces.com/problemset/problem/580/C 题意 根节点是 1 然后所有的叶子结点都是饭店 从根节点到叶子结点的路径上 如果存在 大于m 个 ...

  10. python neo4j

    https://neo4j.com/developer/python/ http://debian.neo4j.org/