"Dp| Multi-backpack feasibility" POJ-1014 dividing

Source: Internet
Author: User

Dividing
Time limit:1000ms Memory limit:10000k

Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so, both receive an equal share of the marbles. This would is easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles is larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so, each of the them gets the same total value. Unfortunately, they realize that it might is impossible to divide the marbles on this the-to Marbles is even). For example, if there is one marble of value 1, one of value 3 and both of the value 4, then they cannot is split into sets of Equal value. So, they ask for write a program that checks whether there is a fair partition of the marbles.

Input
Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1, ..., N6, where NI is the number of marbles of value I. So, the example from above would is described by the Input-line "1 0 1 2 0 0". The maximum total number of marbles would be 20000.
The last line of the input file would be "0 0 0 0 0 0"; Do not process this line.

Output
For each collection, Output ' collection #k: ', where k is the number of the ' Test case ', and then either ' Can be divided. ' OR "Can ' t be divided."
Output a blank line after each test case.

Sample Input

1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0

Sample Output

Collection #1:
Can ' t be divided.

Collection #2:
Can be divided.

Source
Mid-Central European Regional Contest 1999

Test instructions: gives six kinds of stones, from 1 to 6 in volume. A number of each. Ask if it can be divided into two piles to make it the same size.
idea: multiple knapsack problem, no value, just ask the feasibility. Dp[i][j],i represents the former I-type Stone, J indicates the current backpack capacity, Dp[i][j] said that the use of the former I stone, backpack capacity of J when the number of pieces of I-type stone.


The code is as follows:

/* * ID:J.SURE.1 * PROG: * lang:c++ * *#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <iostream>#define PB push_back#define LL Long Longusing namespace STD;Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-8;/****************************************/Const intN =Ten, M =12e4+5;intNum[n], dp[n][m];BOOLSolveintP) {memset(DP,0,sizeof(DP)); for(intj =1; J <= P; J + +) {dp[0][J] =-1;//initial state except dp[0][0] not up} for(inti =1; I <=6; i++) { for(intj =0; J <= P; J + +) {if(dp[i-1][J] >=0) Dp[i][j] = Num[i];//Assuming DP[I-1][J] can be reached, then the same volume is unconditional. Used to initialize            Else{Dp[i][j] =-1;//cannot be reached by an unreachable state}        } for(intj =0; J <= P-i; J + +) {if(Dp[i][j] >0) {Dp[i][j+i] = max (Dp[i][j]-1, Dp[i][j+i]); }//dp[i][j+i] More than dp[i][j] use a volume I of goods}    }if(~dp[6][P])return true;return false;}intMain () {#ifdef j_sureFreopen ("000.in","R", stdin);//freopen ("999.out", "w", stdout);#endif    intCAS =1; while(1) {intall =0; for(inti =1; I <=6; i++) {scanf("%d", &num[i]);        All + = i * num[i]; }if(!all) Break;printf("Collection #%d:\n", cas++);BOOLOkif(All &1) {puts("Can ' t be divided.\n");Continue; } OK = Solve (all >>1);if(OK)puts("Can be divided.\n");Else puts("Can ' t be divided.\n"); }return 0;}

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

"Dp| Multi-backpack feasibility" POJ-1014 dividing

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.