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

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

Hint

Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.

                                                      

思路: 支付的时候对硬币数量没有限制,所以用多重背包求解,dp2【】表示支付i 元时需要的最少硬币数。找零时对硬币数量没有限制,所以用完全背包求解,dp1【】表示找i 元时需要的硬币数。给钱的上界是t + maxn * maxn。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int inf = 0xfffffff;
const int Mn = 105;
const int Mt = 10005 + 105 * 105;
int a[Mn], b[Mn], u[10005];
int dp1[Mt], dp2[Mt];

int main()
{
    //freopen("in", "r", stdin);
    int n, m;
    while(~scanf("%d %d", &n, &m)){
        int maxn = 0;
        for(int i = 0; i < n; ++i){
            scanf("%d", &a[i]);
            maxn = max(maxn, a[i]);
        }
        maxn = maxn * maxn;
        int maxt = maxn + m;
        for(int i = 0; i < n; ++i){
            scanf("%d", &b[i]);
        }
        for(int i = 0; i <= maxt; ++i) dp1[i] = dp2[i] = inf;
        dp1[0] = dp2[0] = 0;

        for(int i = 0; i < n; ++i){
            for(int j = a[i]; j < maxn; ++j){
                dp1[j] = min(dp1[j - a[i]] + 1, dp1[j]);
            }
        }

        for(int i = 0; i < n; ++i){
            int k = 1;
            int s = 0;
            while(s < b[i]){
                for(int j = maxt; j >= a[i]*k; --j){
                    dp2[j] = min(dp2[j], dp2[j - a[i]*k] + k);
                }
                s += k;
                if(s + k*2 > b[i]) k = b[i] - s;
                else k *= 2;
            }
        }

        int minn = inf;
        for(int i = m; i <= maxt; ++i){
            minn = min(minn, dp2[i] + dp1[i - m]);
        }
        if(minn == inf) printf("-1\n");
        else printf("%d\n", minn);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值