Hdu3591The trouble of Xiaoqian backpack + full backpack, hdu3591thexiaoqian

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.