Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1059
Test instructions: There are six marbles worth 1 to 6, each of which is given the amount of marbles. Determine whether the marbles can be divided into two parts to make them equal in value.
Converted to multiple knapsack problems. The cost of the item equals the value of the item, and the backpack capacity is half the total cost. Multi-backpack run again after the interpretation of the maximum value of the backpack is equal to half of the total value.
Why do you do this? Because the price of an item equals value. If the maximum value in this state equals the capacity of the backpack, there must be a combination to fill the backpack.
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define N 122000using namespace Std;int d[n],m,s[7];void completepack (int c,int W) {for (int i=c;i<=m;i++) if (D[i-c]+w>d[i]) d[ I]=d[i-c]+w;} void Zeroonepack (int c,int W) {for (int i=m;i>=c;i--) if (d[i-c]+w>d[i]) d[i]=d[i-c]+w;} void Multiplepack (int c,int w,int t) {if (t*w>=m) completepack (C,W); else {int k=1; while (k<t) {zeroonepack (c*k,w*k); T-=k; k<<=1; } zeroonepack (C*t,w*t); }}int Main () {int kase=0; while (1) {m=0; for (int i=1;i<=6;i++) {scanf ("%d", &s[i]); M+=s[i]*i; }; if (!m) break; printf ("Collection #%d:\n", ++kase); if (m&1) {printf ("Can ' t be divided.\n\n"); Continue } m/=2; Memset (d,0,sizeof (d)); for (int i=1;i<=6;i++) Multiplepack (I,i,s[i]); if (d[m]==m) printf ("Can be divided.\n\n"); else printf ("Can ' t be divided.\n\n"); }}
HDU 1059 dividing multiple backpack