HDU 2844 Coins (multiple knapsack problem DP)

Source: Internet
Author: User

Test instructions: Given n kinds of coins, each value is a, the quantity is C, so that you can not be greater than the number of different values given V, that is, let you use these coins to make up the number of different prices, and the price is not greater than v.

Analysis: A look should know is a dynamic planning knapsack problem, but is the deformation, then we statistics not more than the different price of V, also easy to achieve,

For multiple backpacks we turned it into 01 backpacks and a complete backpack to solve.

The code is as follows:

#include <cstdio> #include <iostream> #include <cstring>using namespace std;typedef long Long ll;const int MAXN = 100000 + 10;int d[maxn];int C[MAXN], A[MAXN], v;void zeroonepack (int V, int val) {for (int i = V; I >= V; -i) D[i] = max (D[i], d[i-v]+val);} void Completepack (int v, int val) {for (int i = v; I <= v; ++i) d[i] = max (D[i], d[i-v]+val);}  void Multiplepack (int v, int val, int num) {if (v <= num * v) {completepack (V, Val); return;    } int k = 1;        while (k <= num) {zeroonepack (v*k, val*k);        num-= k;    K <<= 1; } zeroonepack (Num*v, num*val);}    int main () {int n; while (scanf ("%d%d", &n, &v)) {if (!n &&!)        V) break;        for (int i = 0; i < n; ++i) scanf ("%d", &a[i]);        for (int i = 0; i < n; ++i) scanf ("%d", &c[i]);        memset (d, 0, sizeof (d));        for (int i = 0; i < n; ++i) Multiplepack (A[i], a[i], c[i]);        int cnt = 0;for (int i = 1; I <= V; ++i) if (d[i] = = i) ++cnt;    printf ("%d\n", CNT); } return 0;}

HDU 2844 Coins (multiple knapsack problem DP)

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.