Link: poj 3260
Question:FJ buys things. The value of things is T. He and the seller both have n kinds of gold coins. fj wants to minimize the change of gold coins when the transaction is completed.
Calculate the minimum change quantity of gold coins. FJ has a limited number of gold coins and sellers have an unlimited number of gold coins.
Ideas:For a backpack, the limited number of each gold coin of Fj can be viewedMultiple backpacksThe problem is that the seller's gold coins are infinite.Full backpackProblem.
Set F1 [I] to the minimum number of gold coins when the payment for FJ is I, and set F2 [I] to the minimum number of gold coins when the seller finds the money as I.
Then F1 [I + T] + F2 [I] is the minimum change quantity of gold coins.(F1 uses multiple Backpacks for solving, F2 uses a full backpack for solving)
PS: the backpack here is the minimum value, and it must be filled with exactly. Therefore, when initializing an arrayF [0] = 0, F [1 ~ Maxn] = int_max;
# Include <stdio. h> # define INF 9999999int F1 [1000010], F2 [1000010], V; int min (int A, int B) {return a <B? A: B;} void zeroone (INT cost, int weight, int * f) // 01 backpack {int I; for (I = V; I >= cost; I --) f [I] = min (F [I], F [I-cost] + weight);} void complete (INT cost, int weight, int * F) // complete backpack {int I; for (I = cost; I <= V; I ++) f [I] = min (F [I], f [I-cost] + weight);} void multiple (INT num, int cost, int weight, int * f) // multiple backpacks {int K; if (Num * weight> = V) {complete (cost, weight, f); return;} For (k = 1; k <num; K * = 2) {zeroone (K * cost, K * weight, f); num-= K ;} Zeroone (Num * cost, num * weight, f);} int main () {int I, T, Max, N, num [110], C [110], k; while (scanf ("% d", & N, & T )! = EOF) {max = 0; for (I = 1; I <= N; I ++) {scanf ("% d", & C [I]); if (C [I]> MAX) max = C [I] ;}for (I = 1; I <= N; I ++) scanf ("% d ", & num [I]); V = max * MAX + T; // because you want to find money, V is much larger than T, and F1 [0] = F2 [0] = 0; for (I = 1; I <= V; I ++) f1 [I] = F2 [I] = inf; for (I = 1; I <= N; I ++) Multiple (Num [I], C [I], 1, F1); for (I = 1; I <= N; I ++) complete (C [I], 1, F2); k = inf; for (I = 0; I <= V-T; I ++) if (F1 [I + T]! = Inf & F2 [I]! = Inf) k = min (K, F1 [I + T] + F2 [I]); If (k = inf) k =-1; printf ("% d \ n", k);} return 0 ;}