hdu 3591 The trouble of Xiaoqian 多重背包+完全背包。。。

154 篇文章 1 订阅
26 篇文章 0 订阅

The trouble of Xiaoqian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1250    Accepted Submission(s): 430


Problem Description
In the country of ALPC , Xiaoqian is a very famous mathematician. She is immersed in calculate, and she want to use the minimum number of coins in every shopping. (The numbers of the shopping include the coins she gave the store and the store backed to her.)
And now , Xiaoqian 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). Xiaoqian 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 .But Xiaoqian is a low-pitched girl , she wouldn’t like giving out more than 20000 once.
 

Input
There are several test cases in the 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
The end of the input is a double 0.
 

Output
Output one line for each test case like this ”Case X: Y” : X presents the Xth test case and Y presents the minimum number of coins . If it is impossible to pay and receive exact change, output -1.
 

Sample Input
  
  
3 70 5 25 50 5 2 1 0 0
 

Sample Output
  
  
Case 1: 3
 
题意:货币系统有 N 种不同面值的钱,每种钱的价值分别为 V1,V2,...,VN 一个人要买价值和为 T 的商品,他每种分别相应的带了 C1,C2,...,CN ,然后问你交易完成后所需要经手的钱币最少数目
思路:先对人进行多重背包,然后对售货员进行完全背包
一直情况就是那个人所带的钱刚好凑成了 T ,那么就不需要找钱了,否则就需要找钱,相当于那个人支付了他所能付的超过 T 的钱 M,然后售货员找钱 M-T ,把这两者的经手的钱数加起来就行了
初始化的时候除了 dp[0]=0 之外,其它的都为 inf 表示 i 这个状态是可以由商品组合起来的
否则如果全部初始化为 0 的话,i 只能说是一个上界

我们不应该把10000当做背包的容量,因为付款可能超过T的最大值。
代码:
#include <stdio.h>
#include <string.h>
#define MAX 20100
#define INF 1000000000

int dpx[MAX] , dpy[MAX];
int v[110] , c[110] , total;

int min(int a , int b)
{
	return a>b?b:a ;
}

void zeroOnePack(int dp[] , int v , int c)
{
	for(int i = MAX-1 ; i >= v ; --i)
	{
		dp[i] = min(dp[i],dp[i-v]+c) ;
	}
}

void completePack(int dp[] , int v)
{
	for(int i = v ; i < MAX ; ++i)
	{
		dp[i] = min(dp[i],dp[i-v]+1) ;
	}
}

void multiplePack(int dp[] , int v, int c)
{
	if(c*v>=total)
	{
		completePack(dp,v) ;
	}
	else
	{
		int k = 1 ; 
		while(k<c)
		{
			zeroOnePack(dp , k*v,k) ;
			c -= k ;
			k *= 2 ;
		}
		zeroOnePack(dp,c*v,c) ;
	}
}

int main()
{
	int n , t = 1;
	while(~scanf("%d%d",&n,&total) && (n+total))
	{
		for(int i = 1 ; i < MAX ; ++i)
		{
			dpx[i] = INF ;
			dpy[i] = INF ;
		}
		dpx[0] = dpy[0] = 0 ;
		for(int i = 0 ; i < n ; ++i )
		{
			scanf("%d",&v[i]) ;
		}
		for(int i = 0 ; i < n ; ++i )
		{
			scanf("%d",&c[i]) ;
		}
		for(int i = 0 ; i < n ; ++i)
		{
			multiplePack(dpx,v[i],c[i]) ;
		}
		for(int i = 0 ; i < n ; ++i)
		{
			for(int j = v[i] ; j < MAX ; ++j)
			{
				dpy[j] = min(dpy[j-v[i]]+1,dpy[j]) ;
			}
		}
		int ans = INF ;
		printf("Case %d: ",t++) ;
		for(int i = total ; i < MAX ; ++i)
		{
			ans = min(dpx[i]+dpy[i-total] , ans) ;
		}
		if(ans == INF)
		{
			puts("-1") ;
		}
		else
		{
			printf("%d\n",ans) ;
		}
	}
	return 0 ;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值