POJ-3260

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

题目大意:顾客和老板都有n种硬币,其中,每种硬币的面值为v[i],顾客分别有c[i]个硬币,而老板每种硬币都有无限个。顾客要买t分的物品,求顾客经手的硬币数量最少是多少。顾客经手的硬币数量=顾客买东西付给老板的硬币数量+老板找零给顾客的硬币数量。

分析:顾客与老板的硬币种类一样多,而顾客每种硬币的数量都有限,所以顾客是多重背包,老板每种硬币都有无限的数量,所以老板是完全背包。这里应用了鸽笼原理(具体是啥,问度娘)。我们把最大的面值记为maxi,则完全背包的最大容量为maxi*maxi,多重背包的最大容量为maxi*maxi+t,这样分别求出dp1和dp2在每种状态下的最小硬币数,则顾客买t分物品所经手的最小硬币数为dp1[i]+dp2[i-t]。

代码:

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int N=1e6+5;
#define inf 0x3f3f3f
int n,t,maxi=-1;//记录最大面值  
int v[105];//面值  
int c[105];//数量  
int dp1[N],dp2[N];//dp1:顾客的多重背包 dp2:老板的完全背包  
void multiplepack()
{
	int val=maxi*maxi+t;//多重背包的最大容量为最大值的平方加t  
	for(int i=0;i<n;i++)
	{
		int k=1,sum=0;
		while(sum<c[i])
		{
			for(int j=val;j>=v[i]*k;j--)
				dp1[j]=min(dp1[j],dp1[j-v[i]*k]+k);
			sum += k;
			if(sum+k*2>c[i])
				k=c[i]-sum;
			else
				k *= 2;
		}
	}
}
void completepack()
{
	int val=maxi*maxi;//完全背包最大容量为最大值的平方 
	for(int i=0;i<n;i++) 
	{
		for(int j=v[i];j<val;j++)
		{
			dp2[j]=min(dp2[j],dp2[j-v[i]]+1);
		}
	}
}
int main()
{
	cin>>n>>t;
	for(int i=0;i<n;i++)
	{
		cin>>v[i];
		maxi=max(maxi,v[i]);
	}
	for(int i=0;i<n;i++)
		cin>>c[i];
	memset(dp1,inf,sizeof(dp1));
	memset(dp2,inf,sizeof(dp2));
	dp1[0]=0;
	dp2[0]=0;
	completepack();//完全背包求解  
	multiplepack();//多重背包求解 
	int ans=inf;
	int val=maxi*maxi+t;
	for(int i=t;i<=val;i++)
		ans=min(ans,dp1[i]+dp2[i-t]);
	if(ans==inf)
		cout<<"-1"<<endl;
	else
		cout<<ans<<endl;
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值