HDU_3591_(多重背包+完全背包)

The trouble of Xiaoqian

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

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

题意:有n种面值的货币,val[i]为面值,num[i]为数量,要支付t元,求支付货币枚数和找零货币枚数的和最小的货币枚量。售货员每种货币有无数枚,且以最优方式找零。

思路:一种货币若val[i]*num[i]>10000,那么令num[i]=10000/val[i];对支付使用多重背包(使用二进制转化为01背包),对找零使用完全背包,答案为min(dp1[i]+dp2[i-t])  (t<=i<=t+maxval) maxval:最大面值。

代码:

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<cstring>
using namespace std;
#define INF 10000000

int numb[105],val[105];
int dp1[20005],dp2[125];
int charge[125];

void zero_one(int *dp,int can,int value,int num)
{
    for(int j=can; j>=value*num; j--)
        dp[j]=min(dp[j],dp[j-value*num]+num);
}

void coml_package(int *dp,int can,int value)
{
    for(int j=value; j<=can; j++)
        dp[j]=min(dp[j],dp[j-value]+1);
}

void multi_package(int *dp,int can,int value,int num)
{
    int tmp=1;
    while(num>=tmp)
    {
        zero_one(dp,can,value,tmp);
        num-=tmp;
        tmp=tmp<<1;
    }
    zero_one(dp,can,value,num);
}

int main()
{
    int n,k,Case=0;
    while(scanf("%d%d",&n,&k)!=EOF&&(n&&k))
    {
        for(int i=0; i<20005; i++)
        {
            dp1[i]=INF;
            if(i<125)
                dp2[i]=INF;
        }
        dp1[0]=0;
        dp2[0]=0;
        int maxval=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&val[i]);
            if(val[i]>maxval)
                maxval=val[i];
        }
        for(int i=0; i<n; i++)
        {
            scanf("%d",&numb[i]);
            if(val[i]*numb[i]>10000)
                numb[i]=10000/val[i];
        }

        for(int i=0; i<n; i++)
            multi_package(dp1,k+maxval,val[i],numb[i]);

        for(int i=0; i<n; i++)
            coml_package(dp2,maxval,val[i]);

        int res=dp1[k],g=k,r=0;
        for(int i=k+1; i<=k+maxval; i++)
          res=min(res,dp1[i]+dp2[i-k]);
        if(res>=INF)
            printf("Case %d: -1\n",++Case);
        else
            printf("Case %d: %d\n",++Case,res);
    }
    return 0;
}
时间: 2024-12-15 20:08:50

HDU_3591_(多重背包+完全背包)的相关文章

POJ 3260 多重背包+完全背包

前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168.1.1就能看到他的路由器的一切信息,包括密码,然后打开笔记本……好了,废话不多说,能连上网后第一时间当然是继续和队友之前约好的训练了. 今天翻看到之前落下的一道混合背包题目,然后在草稿本上慢慢地写递推方程,把一些细节细心地写好…(本来不用太费时间的,可是在家嘛,一会儿妈走来要我教她玩手机,一会儿有一个亲戚朋

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, 那肯定找钱)找钱给他. 售货员每次总是用

POJ3260——The Fewest Coins(多重背包+完全背包)

The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus

动态规划之背包问题-01背包+完全背包+多重背包

01背包 有n种不同的物品,每种物品分别有各自的体积 v[i],价值 w[i]  现给一个容量为V的背包,问这个背包最多可装下多少价值的物品. 1 for(int i = 1; i <= n; i++) 2 for(int j = V; j >= v[i]; j--) 3 dp[j] = max(dp[j], dp[j-v[i]]+w[i]); //dp[V]为所求 完全背包 01背包每种物品只能取一个, 完全背包即物品不记件数,可取多件. 1 for(int i = 1; i <= n

ZOJ 3164 Cookie Choice 分组背包 混合背包

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3181 题意: 就是混合背包加分组背包,有的物品是01背包,有的是多重背包,有的是完全背包,同时物品还有不超过8组的分组,如果在同一组则最多只能选一种.问能不能恰好地用掉D的容量,并且使所获价值最大. 分析: 开始的想法是多开一个下标,先把没有分组的做了,在0的下标,然后分组分别在组号的下标里按顺序处理,是什么背包就用什么做法,不过一直WA,对拍小数据大数据都没啥问题(可能随机

HDU1864_最大报销额(背包/01背包)

解题报告 题目传送门 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define inf 99999999 using namespace std; int v,w[35],d[4],dw1,sum,dp[31*1000*100]; int main() { double Q,dw; int n,i,j,m,t; char c; while(~sc

HDU1114_Piggy-Bank(背包/完全背包)

解题报告 题目传送门 题意: 给金币的面额和重量,求装满储蓄罐的最小价值. 思路: 完全背包基础,初始dp为最大,dp[0]=0表示储蓄罐为空价值为0; 状态转移方程就是dp[j]=min(dp[j],dp[j-w[i]]+c[i]) #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 using namespace std; int main() { int t,i

HDU2602_Bone Collector(背包/01背包)

解题报告 题目传送门 题意: 容量为v的大小,物品数n,每个物品有价值和容量,求能装进包的最大价值. 思路: 基础01背包. dp[j]=max(dp[j],dp[j-c[i]]+w[i]) #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 using namespace std; int main() { int t,i,j,n,v,w[1010],c[1010]

HDU1248_寒冰王座(背包/完全背包)

解题报告 题目传送门 无聊的水题. #include <iostream> #include <cstring> #include <cstdio> #define inf 0x3f3f3f3f using namespace std; int c[10100],w[10100],dp[11000],v,n; int main() { int t,i,j; scanf("%d",&t); while(t--) { memset(dp,0,si

B 二维背包+完全背包 Hdu2159

<span style="color:#3333ff;">/* ------------------------------------------------------------------------------------------------ author : Grant Yuan time : 2014.7.19 aldorithm: 二维背包+完全背包 ----------------------------------------------------