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

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

时间: 2024-10-12 16:16:53

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

HDU 3591 多重背包

给出N种钱币和M 给出N种钱币的面值和个数 NPC拿着这N些钱币去买价值M的物品,可以多付,然后被找零,找零的钱也为这些面值,但没有数量限制 问最少经手的钱币数量 对于NPC做一个付款多重背包 然后对于找零做一个完全背包 ans=Min(dp1[i]+dp2[i-m],ans); #include "stdio.h" #include "string.h" int n,m; int dp1[20010],dp2[20010],c[20010],v[20010]; v

HDU 3591 The trouble of Xiaoqian(多重背包+01背包)

HDU 3591 The trouble of Xiaoqian(多重背包+01背包) http://acm.hdu.edu.cn/showproblem.php?pid=3591 题意: 有一个具有n种货币的货币系统, 每种货币的面值为val[i]. 现在小杰手上拿着num[1],num[2],-num[n]个第1种,第2种-第n种货币去买价值为T(T<=20000)的商品, 他给售货员总价值>=T的货币,然后售货员(可能,如果小杰给的钱>T, 那肯定找钱)找钱给他. 售货员每次总是用

hdu 1171 Big Event in HDU(母函数|多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:有n种物品,给出每种物品的价值和数目,要将这些物品尽可能的分成相等的两份A和B且A>=B ,输出A,B. 母函数可以过,但感觉最直接的方法应该是多重背包. 母函数的话,也是按总价值的一半求,从一半到小枚举,直到找到系数不为0的就是B. #include <stdio.h> #include <iostream> #include <map> #include <

hdu 3591 The trouble of Xiaoqian

hdu 3591  The trouble of Xiaoqian 题意:xiaoqi要买一个T元的东西,当前的货币有N种,xiaoqi对于每种货币有Ci个:题中定义了最小数量即xiaoqi拿去买东西的钱的张数加上店家找的零钱的张数(店家每种货币有无限多张,且找零是按照最小的数量找零的):问xiaoqi买元东西的最小数量? 多重背包+完全背包: 思路:这个最小数量是拿去买东西的张数和找零的张数之和,其实我们只需要将这两个步骤分开,开算出能买T元东西的前i最少f[i]张,这里涉及到容量问题:容量只

HDU 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

hdu 5445 多重背包

Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1243    Accepted Submission(s): 368 Problem Description Few days before a game of orienteering, Bell came to a mathematician to sol

HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. 简单的区间dp,哎,以为很神奇的东西,其实也是dp,只是参数改为区间,没做过此类型的题,想不到用dp,以后就 知道了,若已经知道[0,i],推[0,i+1], 显然还要从i+1 处往回找,dp方程也简单: dp[j][i]=(dp[j+1][i]+dp[j][i-1]+10007-dp[j+1][

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe