hdu 3591 多重加完全DP
题目:
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
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.
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.
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.
#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赋值
hdu 3591 多重加完全DP的更多相关文章
- HDU 3591 多重背包
给出N种钱币和M 给出N种钱币的面值和个数 NPC拿着这N些钱币去买价值M的物品,能够多付.然后被找零,找零的钱也为这些面值.但没有数量限制 问最少经手的钱币数量 对于NPC做一个付款多重背包 然后对 ...
- HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)
HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...
- hdu 3591 The trouble of Xiaoqian
hdu 3591 The trouble of Xiaoqian 题意:xiaoqi要买一个T元的东西,当前的货币有N种,xiaoqi对于每种货币有Ci个:题中定义了最小数量即xiaoqi拿去买东西 ...
- hdu 5445 多重背包
Food Problem Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- HDU 1114 完全背包 HDU 2191 多重背包
HDU 1114 Piggy-Bank 完全背包问题. 想想我们01背包是逆序遍历是为了保证什么? 保证每件物品只有两种状态,取或者不取.那么正序遍历呢? 这不就正好满足完全背包的条件了吗 means ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- hdu 4568 Hunter 最短路+dp
Hunter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
随机推荐
- WebPack填坑笔记
loader使用时不需要用require引入,在使用plugins(插件)才需要使用require引入 压缩js代码会导致热更新失效 所以开发环境先不要进行压缩 给css加前缀的 postcss-lo ...
- 剥开比原看代码12:比原是如何通过/create-account-receiver创建地址的?
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- k8s2
1.主节点与子节点如何沟通,交互 apiServer <==> kublet 2. pod之间如何共享, 使用volumn(数据卷 ) kube-proxy 和 service 配置好网络 ...
- 洛谷P1244 青蛙过河 DP/思路
又是一道奇奇怪怪的DP(其实是思路题). 原文戳>>https://www.luogu.org/problem/show?pid=1244<< 这题的意思给的挺模糊,需要一定的 ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- js代码点击触发事件
js触发按钮点击事件 function load(){ //下面两种方法效果是一样的 document.getElementById("target").onclick(); do ...
- sqlserver 中常见的函数字符串函数
---字符中操作函数 UPPER(S) 将字符串统一为大写字母 SELECT UPPER('asasA') --ASASA LOWER(S) 将字符串统一为小写字母 SELECT LOWER('asa ...
- SQLServer 中的存储过程中判断临时表是否存在,存在则删除临时表
IF OBJECT_ID('TEMPDB..#BCROSSREFERENCE ') IS NOT NULL DROP TABLE #BCROSSREFERENCE IF OBJECT_ID('TEMP ...
- SqlParameter的两种用法【二】
private void Loadprovince() { string sql = "select * from Tables where ArealdPid=@pid"; /第 ...
- 浅谈Nginx负载均衡与F5的区别
前言 笔者最近在负责某集团网站时,同时用到了Nginx与F5,如图所示,负载均衡器F5作为处理外界请求的第一道“墙”,将请求分发到web服务器后,web服务器上的Nginx再进行处理,静态内容直接访问 ...