Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5257 | Accepted: 1586 |
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 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 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 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
Sample Input
3 70 5 25 50 5 2 1
Sample Output
3
Hint
Source
购买商品是多重背包,找钱是完全背包
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
#define maxn 15005
#define inf 0x7fffff
pair <int ,int > p;
int dp1[maxn];
int dp2[maxn];
int v[105];
int used[maxn];
int c[maxn];
int main()
{
int k,t,n;
while(scanf("%d%d",&n,&t)!=EOF){
for(int i=1;i<=n;i++){
scanf("%d",&v[i]);
}
for(int j=1;j<=n;j++){
scanf("%d",&c[j]);
}
for(int i=1;i<maxn;i++){
dp1[i]=dp2[i]=inf;
}
dp1[0]=dp2[0]=0;
for(int i=1;i<=n;i++){
memset(used,0,sizeof(used));
for(int j=v[i];j<maxn;j++){
if(dp1[j-v[i]]+1<dp1[j]&&used[j-v[i]]<c[i]){
dp1[j]=dp1[j-v[i]]+1;
used[j]=used[j-v[i]]+1;
}
}
}
for(int i = 1;i <= n;i++){
for(int j = v[i];j < maxn;j++){
dp2[j]=min(dp2[j],dp2[j-v[i]]+1);
}
}
int ans = inf;
for(int i = t; i < maxn;i++){
ans = min(ans,dp1[i]+dp2[i-t]);
}
if(ans >= inf)
ans=-1;
printf("%d\n",ans);
}
return 0;
}