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.
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.
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;
}