POJ3260——The Fewest Coins(多重背包+完全背包)
The Fewest Coins
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 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 V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., 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
题目大意:
FJ同学去买东西,东西的价值为T,他和卖家都有N种金币,FJ希望交易完成时金币变化最小。
求最少的金币变化数量。FJ的金币个数有限,奸商的金币数目无限。
解题思路:
背包问题,FJ的每种金币个数有限可以看做是多重背包问题,奸商的金币数目无限可以看做是完全背包问题。
设F1[i]为FJ付款为i时的最小金币数,设F2[i]为奸商找钱为i时的最小金币数。
则F1[i+T]+F2[i]就是所求的最小金币变化数量。(F1用多重背包求解,F2用完全背包求解)
PS:这里的背包求得是最小价值,且要恰好装满。故初始化数组时应 F[0]=0,F[1-MAXN]=INT_MAX;(好久没做背包了,下意识把F[1]=0了,结果T==1时总是输出0,查了好久。。。)
Code:
1 #include<string> 2 #include<iostream> 3 #include<stdio.h> 4 #include<cstring> 5 #include<limits.h> 6 #define MAXN 1000000 7 #define INF 9999999 //背包被调 直接抄的背包九讲,因为有两个数组,增加一个数组参数 8 using namespace std; 9 int N,V,c[MAXN+10],a[MAXN+10],w=1,f1[MAXN+10],f2[MAXN+10]; 10 int min(int a,int b) 11 { 12 return a>b?b:a; 13 } 14 void ZeroOnePack(int cost,int weight,int f[]) //01背包 15 { 16 for (int v=V; v>=cost; v--) 17 f[v]=min(f[v],f[v-cost]+weight); 18 } 19 void CompletePack(int cost,int weight,int f[]) //完全背包 20 { 21 for (int v=cost;v<=V;v++) 22 f[v]=min(f[v],f[v-cost]+weight); 23 } 24 void MultiplePack(int cost,int weight,int amount,int f[]) //多重背包 25 { 26 if (cost*amount>=V) 27 { 28 CompletePack(cost,weight,f); 29 return ; 30 } 31 int k=1; 32 while (k<amount) 33 { 34 ZeroOnePack(k*cost,k*weight,f); 35 amount=amount-k; 36 k*=2; 37 } 38 ZeroOnePack(amount*cost,amount*weight,f); 39 } 40 void init(int M,int f[]) 41 { 42 f[0]=0; //保证背包装满 具体原因参见背包九讲 43 for (int i=1; i<=M; i++) //求最小价值要把初值赋值为正无穷(INT_MAX可能会导致整型溢出) 44 f[i]=INF; 45 } 46 int main() 47 { 48 while (cin>>N>>V) 49 { 50 51 int V2=V; 52 int max=0; 53 for (int i=1; i<=N; i++){ 54 cin>>c[i]; 55 if (c[i]>max) max=c[i];} 56 for (int i=1; i<=N; i++) 57 cin>>a[i]; 58 V=max*max+V2+10; //要找钱,V要比T大很多才行 59 init(V,f1); 60 init(V,f2); 61 for (int i=1;i<=N;i++) 62 MultiplePack(c[i],1,a[i],f1); 63 for (int i=1;i<=N;i++) 64 CompletePack(c[i],1,f2); 65 int ans=INF; 66 for (int i=0;i<=V-V2;i++) 67 if (f1[i+V2]!=INF&&f2[i]!=INF) ans=min(ans,f1[i+V2]+f2[i]); 68 if (ans!=INF) printf("%d\n",ans); //ans==INF表示数据没有变过,则表示无解 69 else printf("-1\n"); 70 } 71 return 0; 72 }