POJ3260: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 hasN (1 ≤ N ≤ 100) different coins, with values V1,V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carryingC1 coins of value V1, C2 coins of valueV2, ...., 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
 
题意:给出钱币的方案数和总价值,然后给出每种钱币的价值与数量,而老板也是每种钱币都拥有,但是没有数量限制,购买东西的时候,价值超过给定价值的话,老板会找钱,要求最小的交流钱币的数量
 
思路:这题想了很久没有想出思路,虽然知道是背包,但是不知道该如何让运用,看了别人的代码,感觉人家的思路真心碉堡了,讲解在代码中
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int v[105],c[105],MAX,n,sum;
int dp[33333],inf = 100000000;

void ZeroOnePack(int cost,int cnt)
{
    int i;
    for(i = sum+MAX; i>=cost; i--)
        dp[i] = min(dp[i],dp[i-cost]+cnt);//找出最小数量的方案
}

void CompletePack(int cost,int cnt)
{
    int i;
    for(i = sum+MAX+cost; i>=0; i--)
        dp[i] = min(dp[i],dp[i-cost]+cnt);
}

int MultiplePack()
{
    int i,j,k;
    for(i = 1; i<=sum+MAX; i++)
        dp[i] = inf;
    dp[0] = 0;//dp数组用来记录钱币数量
    for(i = 1; i<=2*n; i++)
    {
        if(i<=n)//这是顾客购买时所给的钱的数量
        {
            k = 1;
            while(k<c[i])
            {
                ZeroOnePack(k*v[i],k);
                c[i]-=k;
                k*=2;
            }
            ZeroOnePack(c[i]*v[i],c[i]);
        }
        else
            CompletePack(-v[i-n],1);//只所以是负数,是因为这是老板找钱的数目
    }
    if(dp[sum]==inf)
        return -1;
    else
        return dp[sum];
}

int main()
{
    int i;
    while(~scanf("%d%d",&n,&sum))
    {
        MAX = 0;
        for(i=1; i<=n; i++)
        {
            scanf("%d",&v[i]);
            MAX = max(MAX,v[i]);
        }
        MAX*=MAX;//保证背包足够大
        for(i=1; i<=n; i++)
            scanf("%d",&c[i]);
        printf("%d\n",MultiplePack());
    }

    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值