POJ_3260 The Fewest Coins(混合背包)

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 V1, V2, …, VN coins ( V1, … VN)
Line 3: N space-separated integers, respectively C1, C2, …, 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.
题解:

定义一共有N种货币,对应V1….Vn,N种价格。老农要拿一些钱去买价格为T的东西,老农有C1…..CN个对应货币,售货员有无限个货币。求如何才能使老农拿的硬币数与找零的硬币数和最小。

题意搞清楚我们就会发现对于老农和售货员不能同时dp,老农是明显的多重背包,而售货员则是完全背包。明白这一点我们确定思路用混合背包求解,dp可达钱数的最小硬币数。但是关键在于如何确定上界。

算法:
老农上界:maxv*maxv+T;售货员上界:maxv*maxv
证明(参考网上):如果John的付款数大于了maxv*maxv+T,即付硬币的数目大于了maxv,根据鸽笼原理,至少有两个的和对maxv取模的值相等,也就是说,这部分硬币能够用更少的maxv来代替。证毕。
解释:如果说假设我们使用了大于maxv*maxv+T的钱数去购买T的东西,我们多余的钱数一定大于maxv*maxv,也就是说我们付其他钱数目相同的情况下(取余相等)我们用了更多的maxv去购买T,有更优解,所以上届不能超过maxv*maxv+T.

定义:dp1[i]:老农得到钱i所用最少硬币数;dp2[i]:售货员得到钱i所用最少硬币数;
老农:多重背包
售货员:完全背包
时间复杂度:O(NV)

附代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <limits.h>
#define MAX_N 110
#define INF 0xfffffff
using namespace std;

int dp1[24401];
int dp2[14401];
int v[MAX_N];
int c[MAX_N];
int N,T;
int result,sum;
void CompletePack(int cost,int weight);
void MultiplePack(int cost,int weight,int amount);
int main()
{
    scanf("%d%d",&N,&T);
    sum=0;result=INT_MAX;
    for( int i = 0; i < N; i++ )
    {
        scanf("%d",&v[i]);
        sum=max(sum,v[i]);
    }
    for( int i = 0; i < N; i++ )
        scanf("%d",&c[i]);
    sum=(sum*sum)+T+1;
    fill(dp2,dp2+sum-T+1,INF);
    fill(dp1,dp1+sum+1,INF);
    //钱数为0,最少硬币数0,初始化很重要
    dp1[0]=0;dp2[0]=0;
    for( int i=0; i < N; i++ )
    {
        CompletePack(v[i],1);
        MultiplePack(v[i],1,c[i]);
    }
    for( int i = T; i <= sum; i++ )
        result=min(result,dp1[i]+dp2[i-T]);
    if( result == INF )
        printf("-1\n");
    else
        printf("%d\n",result);
    return 0;
}
void CompletePack(int cost,int weight)
{
    for( int j = cost; j <= sum-T; j++ )
        dp2[j]=min(dp2[j],dp2[j-cost]+1);
    return ;
}
void MultiplePack(int cost,int weight,int amount)
{
    if( cost*amount >= sum )
    {
        for( int j = cost; j <= sum; j++ )
            dp1[j]=min(dp1[j],dp1[j-cost]+1);
        return ;
    }
    else
    {
        int k = 1;
        while( k < amount )
        {
            for( int j=sum; j >= k*cost; j-- )
                dp1[j]=min(dp1[j],dp1[j-k*cost]+k);
            amount-=k;
            k*=2;
        }
        for( int j = sum; j >= amount*cost; j-- )
            dp1[j]=min(dp1[j],dp1[j-amount*cost]+amount);
    }
    return ;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值