Poj 1014 Dividing // multiple backpacks

Source: Internet
Author: User

What are multiple backpacks?

There are N items and a backpack with a capacity of V. A maximum of Mi pieces are available for the I-th item. The space consumed by each item is Ci, and the value is Wi. Solving which items are loaded into a backpack makes the total space consumed by these items not exceed
Backpack capacity, and the total value is the largest.

Among them, if Mi * Ci> = V, then multiple backpacks are equivalent to a full backpack problem. Otherwise, convert multiple backpacks into 01 backpacks, but optimize them. (The optimization idea is clever ,).

For the pseudo code, see:

The code for this question is as follows:

/* Poj 1014 Dividing DP multiple backpack transfer equation: 01 The transfer equation of the backpack and the full backpack */# include <cstdio> # include <cstring> using namespace std; const int maxn = 120005; // do not enable const int N = 6; int f [maxn]; int a [N + 1]; int v; int totval; bool flag; int Max (int a, int B) {return a> B? A: B;} void ZeroOnePack (int c, int w) {for (int I = v; I> = c; I --) {f [I] = Max (f [I], f [I-c] + w); if (f [I] = v) flag = true ;}} void CompletePack (int c, int w) {for (int I = c; I <= v; I ++) {f [I] = Max (f [I], f [I-c] + w); if (f [I] = v) flag = true ;}} void MultiplePack (int c, int w, int m) {if (c * m> = v) {CompletePack (c, w); return;} int k = 1; while (k <m) {ZeroOnePack (k * c, k * w); m = m-k; k = k * 2;} Zer OOnePack (m * c, m * w);} int main () {int t = 0; while (scanf ("% d", & a [1], & a [2], & a [3], & a [4], & a [5], & a [6]) {t ++; totval = 0; for (int I = 1; I <= N; I ++) totval + = (I * a [I]); // printf ("totval = % d \ n", totval); if (totval = 0) break; if (totval % 2! = 0) {printf ("Collection # % d: \ n", t); printf ("Can't be divided. \ n "); continue;} memset (f,-1, sizeof (f); f [0] = 0; flag = false; v = totval/2; for (int I = 1; I <= N; I ++) {flag = false; MultiplePack (I, I, a [I]); if (flag = true) break;} if (flag) {printf ("Collection # % d: \ n", t); printf ("Can be divided. \ n ");} else {printf (" Collection # % d: \ n ", t); printf (" Can't be divided. \ n ") ;}} return 0 ;}

The code for multiple backpacks is very simple, but there are a few things to pay attention;

1) determine the transfer equation of the 01 backpack and the full backpack and the second cyclic order.

Ii). 01 the relationship between a backpack and multiple backpacks.

(3) Correctness of splitting.

4) f array initialization issues.

References: backpack 9 lecture

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.