混合背包 (多重背包+完全背包)

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

题意是用户买一件T元的商品,货币有n个,面值是v1,v2,...vn.数量是c1,c2,...cn。商家有无限个。求用户负的数量加上商家找会来的数量的总和最小。没有就输出-1.
可以用两个dp数组,第一个是多重背包。dp1[i]表示买i元的商品用户最少付多少的数量。第二个是完全背包,dp2[i]表示找i元给用户要付的最少的数量。然后求下dp1[T+i]+dp2[i]的最小值
就是答案了。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 const int Inf = 1<<25;
 6 int c[110],v[110];
 7 int dp1[20000+5],dp2[20000+5];
 8 void zero_or_pack(int *dp, int val, int num){
 9     for(int i = 20000; i >= val; i--){
10         dp[i] = min(dp[i],dp[i-val]+num);
11     }
12 }
13 void complPack(int *dp, int val){
14     for(int i = val; i <= 20000; i ++)
15         dp[i] = min(dp[i],dp[i-val]+1);
16 }
17 void multiPack(int *dp, int val, int num){
18     if(val*num >= 20000){
19         complPack(dp,val);
20         return;
21     }
22     int k = 1;
23     while(k < num){
24         zero_or_pack(dp,val*k,k);
25         num -= k;
26         k*=2;
27     }
28     zero_or_pack(dp,val*num,num);
29 }
30 
31 int main(){
32     int n, t, k = 1;
33     while(scanf("%d %d",&n,&t)&&n){
34         for(int i = 0; i < n; i ++)scanf("%d",&v[i]);
35         for(int i = 0; i < n; i ++)scanf("%d",&c[i]);
36         for(int i = 0; i <= 20000; i ++)dp1[i] = dp2[i] = Inf;
37         dp1[0] = dp2[0] = 0;
38         for(int i = 0; i < n; i ++)
39             multiPack(dp1,v[i],c[i]);
40         for(int i = 0; i < n; i ++)
41             complPack(dp2,v[i]);
42         int ans = dp1[t];
43         for(int i = t+1; i <= 20000; i ++)
44             ans = min(ans,dp1[i]+dp2[i-t]);
45         printf("Case %d: %d\n",k++,(ans==Inf)?-1:ans);
46         memset(dp1,0,sizeof(dp1));
47         memset(dp2,0,sizeof(dp2));
48     }
49     return 0;
50 }

 

posted @ 2017-07-17 09:59  starry_sky  阅读(775)  评论(0编辑  收藏  举报