HDU 1059 dividing multiple backpack

Source: Internet
Author: User

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

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.