Link: poj 1276
Question: The known amount cash.Number and nominal value of currencies with the same nominal value,You can use the given currency to generate
Less than or equal to the maximum cash amount
Analysis: Because the nominal value and quantity of each currency are known, it can be convertedMultiple backpacks,The capacity of the backpack is cash,
The value and cost of each item are the nominal values of each currency.
Multiple backpacks can be converted to 01 backpacks, but this will time out. To avoid this, it can be convertedFull backpack and binary thought 01 backpack
# Include <stdio. h> # include <string. h> int f [100010], V; int max (int A, int B) {return A> B? A: B;} void zeroone (INT cost, int weight) // 01 backpack {int I; for (I = V; I> = cost; I --) f [I] = max (F [I], F [I-cost] + weight);} void complete (INT cost, int weight) // complete backpack {int I; for (I = cost; I <= V; I ++) f [I] = max (F [I], F [I-cost] + weight );} void multiple (INT num, int cost, int weight) // multiple backpacks {int K; If (Num * weight> = V) {complete (cost, weight); return ;} for (k = 1; k <num; K * = 2) {zeroone (K * cost, K * weight); num-= K;} zeroone (Num * cost, num * Weight);} int main () {int I, n, num [1010], W [1010]; while (scanf ("% d", & V )! = EOF) {scanf ("% d", & N); for (I = 1; I <= N; I ++) scanf ("% d ", & num [I], & W [I]); memset (F, 0, sizeof (f); for (I = 1; I <= N; I ++) multiple (Num [I], W [I], W [I]); printf ("% d \ n", F [v]);} return 0 ;}