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 ...
随机推荐
- Vue内置的Component标签用于动态切换组件
html <div id="app"> <component :is="cut"></component> <butt ...
- VHDL 类型转换
STD_LOGIC_1164包集合 函 数 名 功 能 TO_STDLOGICVECTOR(A) 由BIT_VECTOR转换为STD_LOGIC_VECTOR TO_BITVECTOR(A) 由STD ...
- windows下的安装及使用 python
出处 https://www.cnblogs.com/daysme/ - 2017-12-30 本文只讲在 vscode 中如何运行起 python - 2017-12-30 ## windows下的 ...
- Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)
http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...
- 动态 hover 使用变相使用
使用 onmouseover 和 onmouseout 代替 hover foreach (var menu in Model.OrderBy(x => x.Order).Where(x ...
- progressBar显示百分比
this.lab_AllFiles.Text = progressBarAllFile.Value * 100 / progressBarAllFile.Maximum + "%" ...
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp
E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...
- hrbust 2080链表 【贪心】
仔细看题想想就是个贪心题,两个sort就可以解决了 #include<stdio.h> #include<string.h> #include<math.h> #i ...
- 第 8 章 容器网络 - 052 - overlay 如何实现跨主机通信?
overlay 如何实现跨主机通信? 在 host2 中运行容器 bbox2: docker run -itd --name bbox2 --network ov_net1 busybox bbox2 ...
- windows安装使用docker
doker就是一个容器,如果想要在windows安装还必须要用另外一个工具docker-toolbox.下载地址:https://mirrors.aliyun.com/docker-toolbox/w ...