HDU3591-The trouble of Xiaoqian

29 篇文章 0 订阅

The trouble of Xiaoqian

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

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
 
Author
alpc97
 
Source

题意:货币系统有 N 种不同面值的钱,每种钱的价值分别为 V1,V2,...,VN 一个人要买价值和为 T 的商品,他每种分别相应的带了 C1,C2,...,CN ,然后问你交易完成后所需要经手的钱币最少数目
思路:先对人进行多重背包,然后对售货员进行完全背包
那个人所带的钱刚好凑成了 T ,那么就不需要找钱了,否则就需要找钱,相当于那个人支付了他所能付的超过 T 的钱 M,然后售货员找钱 M-T ,把这两者的经手的钱数加起来就行了

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;

const int INF=100010;
int v[INF],c[INF],dp1[INF],dp2[INF],n,t;

int main()
{
    int cas=0;
    while(~scanf("%d %d",&n,&t))
    {
        if(n==0&&t==0) break;
        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=1;i<INF;i++)
            dp1[i]=dp2[i]=INF;
        dp1[0]=dp2[0]=0;
        for(int i=0;i<n;i++)
        {
            if(v[i]*c[i]>=t)
            {
                for(int j=v[i];j<=20000;j++)
                    dp1[j]=min(dp1[j],dp1[j-v[i]]+1);
                continue;
            }
            int k=1;
            while(k<c[i])
            {
                for(int j=20000;j>=k*v[i];j--)
                    dp1[j]=min(dp1[j],dp1[j-k*v[i]]+k);
                c[i]-=k;
                k*=2;
            }
            for(int j=20000;j>=c[i]*v[i];j--)
                dp1[j]=min(dp1[j],dp1[j-c[i]*v[i]]+c[i]);
        }
        for(int i=0;i<n;i++)
          for(int j=v[i];j<=20000;j++)
                dp2[j]=min(dp2[j],dp2[j-v[i]]+1);
        int ans=INF;
        for(int i=t;i<=20000;i++)
            ans=min(ans,dp1[i]+dp2[i-t]);
        if(ans==INF) ans=-1;
        printf("Case %d: %d\n",++cas,ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值