Hdu3591The trouble of Xiaoqian backpack + full backpack, hdu3591thexiaoqian
// Provide the value of Xiaoqian coins and the number of each kind of money in it. // The number of each kind of money in the seller is infinite, xiaoqian can pay a maximum of 20000 yuan at a time // ask how to pay for the minimum number of coins in the transaction // Xiaoqian is a multi-backpack // the merchant is a full backpack # include <cstdio> # include <cstring> # include <iostream> using namespace std; const int maxn = 20010; const int inf = 0x3f3f3f3f; int dp [maxn]; int dp_1 [maxn]; int c [maxn], v [maxn]; int n, t; int cas = 0; int main () {// freopen ("in.txt", "r", stdin); while (scanf ("% d", & n, & t) & (n + t) {for (int I = 1; I <= n; I ++) scanf ("% d", & v [I]); for (int I = 1; I <= n; I ++) scanf ("% d", & c [I]); for (int I = 1; I <maxn; I ++) dp [I] = dp_1 [I] = inf; dp_1 [0] = 0; dp [0] = 0; for (int I = 1; I <= n; I ++) for (int j = v [I]; j <maxn; j ++) dp_1 [j] = min (dp_1 [j], dp_1 [j-v [I] + 1); for (int I = 1; I <= n; I ++) {for (int k = 1; k <= c [I]; k * = 2) {for (int j = maxn-1; j> = k * v [I]; j --) dp [j] = min (dp [j], dp [j-k * v [I] + k); C [I]-= k;} for (int j = maxn-1; j> = c [I] * v [I]; j --) dp [j] = min (dp [j], dp [j-c [I] * v [I] + c [I]);} int ans = inf; for (int I = t; I <= maxn-10; I ++) if (dp [I]! = Inf & dp_1 [I-t]! = Inf) ans = min (dp [I] + dp [I-t], ans); printf ("Case % d:", ++ cas ); if (ans = inf) puts ("-1"); else cout <ans <endl ;}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.