题目:

The trouble of Xiaoqian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1997    Accepted Submission(s):
711

Problem Description
In the country of ALPC , Xiaoqian is a very famous
mathematician. She is immersed in calculate, and she want to use the minimum
number of coins in every shopping. (The numbers of the shopping include the
coins she gave the store and the store backed to her.)
And now , Xiaoqian
wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1
≤ N ≤ 100) different coins, with values V1, V2, ..., VN (1 ≤ Vi ≤ 120). Xiaoqian
is carrying C1 coins of value V1, C2 coins of value V2, ...., and CN coins of
value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the
coins, and always makes change in the most efficient manner .But Xiaoqian is a
low-pitched girl , she wouldn’t like giving out more than 20000 once.
 
Input
There are several test cases in the input.
Line 1:
Two space-separated integers: N and T.
Line 2: N space-separated integers,
respectively V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated
integers, respectively C1, C2, ..., CN
The end of the input is a double
0.
 
Output
Output one line for each test case like this ”Case X:
Y” : X presents the Xth test case and Y presents the minimum number of coins .
If it is impossible to pay and receive exact change, output -1.
 
Sample Input
3 70
5 25 50
5 2 1
0 0
 Sample Output
Case 1: 3
 
题意 给出硬币的种类面额和数量,要购买的物品总价格,求交易时最小的经手货币量;
每种硬币数量可多达一万,所以使用二进制优化数量,又最多支付2w,所以默认背包为2w大小;
店家货币无限,所以为完全背包,小倩是一个多重背包;
代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define inf 999999999
int main()
{
int T,N,c[105],v[105],dp[20001],i,j,k,a[105],b[105],
dp2[20001],x=0;
while (cin>>N>>T&&N&&T){x++;
int min_num=inf;
memset(dp,63,sizeof(dp));dp[0]=0;
memset(dp2,63,sizeof(dp2));dp2[0]=0;
for (i=1;i<=N;i++) scanf("%d",&v[i]);
for (i=1;i<=N;i++) scanf("%d",&c[i]);
for (i=1;i<=N;i++){
int count=1;
for (k=1;c[i];k*=2){        //二进制优化硬币数量,顺便记录下每种情况所需硬币多少便于优化后01背包的完成
if (c[i]<k) k=c[i];
a[count]=k*v[i];
b[count++]=k;
c[i]-=k;
}
for (k=1;k<count;k++)       //优化之后进行一次01背包
    for (j=20000;j>=a[k];j--)
dp[j]=min(dp[j],dp[j-a[k]]+b[k]);
}
for (i=1;i<=N;i++)                  //对商店进行的完全背包
   for (j=v[i];j<=20000;j++)
dp2[j]=min(dp2[j],dp2[j-v[i]]+1);
for (i=T;i<=20000;i++)       //计算 T---2w间最小经手硬币数量
min_num=min(min_num,dp[i]+dp2[i-T]);
printf("Case %d: ",x);
if (min_num==inf) cout<<"-1"<<endl;
else cout<<min_num<<endl;
}
return 0;
}

小坑---inf和memset赋值

最好让两者在inf小于memset情况下两者总和不要超出int,否则爆出负数就会wa

hdu 3591 多重加完全DP的更多相关文章

  1. HDU 3591 多重背包

    给出N种钱币和M 给出N种钱币的面值和个数 NPC拿着这N些钱币去买价值M的物品,能够多付.然后被找零,找零的钱也为这些面值.但没有数量限制 问最少经手的钱币数量 对于NPC做一个付款多重背包 然后对 ...

  2. HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

    HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...

  3. hdu 3591 The trouble of Xiaoqian

    hdu 3591  The trouble of Xiaoqian 题意:xiaoqi要买一个T元的东西,当前的货币有N种,xiaoqi对于每种货币有Ci个:题中定义了最小数量即xiaoqi拿去买东西 ...

  4. hdu 5445 多重背包

    Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  5. HDU 1114 完全背包 HDU 2191 多重背包

    HDU 1114 Piggy-Bank 完全背包问题. 想想我们01背包是逆序遍历是为了保证什么? 保证每件物品只有两种状态,取或者不取.那么正序遍历呢? 这不就正好满足完全背包的条件了吗 means ...

  6. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  7. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  8. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  9. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. Learning to Track at 100 FPS with Deep Regression Networks ECCV 2016 论文笔记

    Learning to Track at 100 FPS with Deep Regression Networks   ECCV 2016  论文笔记 工程网页:http://davheld.git ...

  2. Docker Engine SDKs and API 的开发1

    Develop with Docker Engine SDKs and API Docker provides an API for interacting with the Docker daemo ...

  3. 用Qemu模拟vexpress-a9 --- 配置 qemu 的网络功能

    转载:http://wiki.sylixos.com/index.php/Linux%E7%8E%AF%E5%A2%83%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97 环境介 ...

  4. HDU 5726 GCD(RMQ+二分)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...

  5. ASP.NET开发总结

    ASP.NET的界面可以是.aspx,会对应有一个.aspx.cs的逻辑处理文件,.aspx的所有控件对应着变量,变量名就是控件的ID. 为了代码编写方便起见,一般将数据库表的新增字段,放在最后. 日 ...

  6. git core.autocrlf配置 解决Windows和Linux(Mac)换行问题

    格式化 格式化是许多开发人员在协作时,特别是在跨平台情况下,遇到的令人头疼的细小问题. 由于编辑器的不同或者Windows程序员在跨平台项目中的文件行尾加入了回车换行符, 一些细微的空格变化会不经意地 ...

  7. 改变input中的placeholder样式

    1.input[placeholder]{ color:#d5d5d5; } 2.input::-moz-placeholder { color: #d5d5d5; } input:-ms-input ...

  8. Codeforces 767E Change-free

    题目链接:http://codeforces.com/contest/767/problem/E 居然是一个瞎几把贪心(E比B水系列) 考虑要每一次操作至少要用${\left \lfloor \fra ...

  9. 学习笔记56—Endnote参考文献格式调整

    论文写作

  10. mint19 源码安装python3.7

    基于Ubuntu 18.04  自带了3.6,可惜 3.7秒出. 一个原则是: 自带的3.6不要动 防止用apt install python3-XXX时版本不对. 理由: 自带的2.7和3.6都是让 ...