POJ 3260 The Fewest Coins (混合背包--多重背包+完全背包)

The Fewest Coins
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3846 Accepted: 1141

Description

Farmer 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 the number of coins he receives in change is minimized. Help him to determine what this minimum number is.

FJ 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). Farmer John 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 (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).

Input

Line 1: Two space-separated integers: N and T. Line 2: N space-separated integers, respectively V 1, V 2, ..., VN coins ( V 1, ... VN) Line 3: N space-separated integers, respectively C 1, C 2, ..., CN

Output

Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.

Sample Input

3 70
5 25 50
5 2 1

Sample Output

3

思路:顾客的硬币有限,属于多重背包,店主有无穷多的硬币,属于完全背包。令dp[x]表示价值为x的时候,最少的硬币个数。

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define INF 0xfffffff
 6 #define MAX 25005
 7 #define SIZE 125
 8 using namespace std;
 9 
10 int dp[MAX],dp2[MAX];
11 int v[SIZE],c[SIZE];
12 int N,M;
13 
14 void multiPack(int vv,int cc)
15 {
16     int k = 1;
17     int num = cc;
18     while(k <= num)
19     {
20         for(int i=M+10000; i>=k*vv; i--)
21             dp[i] = min(dp[i],dp[i-k*vv]+k);
22         num -= k;
23         k *= 2;
24     }
25     if(num)
26     {
27         for(int i=M+10000; i>=num*vv; i--)
28             dp[i] = min(dp[i],dp[i-num*vv]+num);
29     }
30 }
31 
32 int main()
33 {
34     while(~scanf("%d %d",&N, &M))
35     {
36         for(int i=1; i<=N; i++)
37             scanf("%d",&v[i]);
38         for(int i=1; i<=N; i++)
39             scanf("%d",&c[i]);
40         for(int i=1; i<=M+10000; i++)
41             dp[i] = dp2[i] = INF;
42         dp[0] = dp2[0] = 0;
43         for(int i=1; i<=N; i++)
44         {
45             multiPack(v[i],c[i]);
46         }
47         for(int i=1; i<=N; i++)
48         {
49             for(int j=v[i]; j<=M+10000; j++)
50                 dp2[j] = min(dp2[j],dp2[j-v[i]]+1);
51         }
52         int ans = INF;
53         for(int i=M; i<=M+10000; i++)
54             ans = min(ans,dp[i]+dp2[i-M]);
55         if(ans == INF)
56             printf("-1\n");
57         else
58             printf("%d\n",ans);
59     }
60     return 0;
61 }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值