zoukankan      html  css  js  c++  java
  • 多重背包,附上例题(POJ

    直接上例题

    Dividing
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 72067   Accepted: 18813

    Description

    Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be 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 are 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 that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to 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 be described by the input-line "1 0 1 2 0 0". The maximum total number of marbles will be 20000. 
    The last line of the input file will 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.


     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 int cnt ;
     7 int dp[120004];
     8 void ZeroOne(int v, int w) {
     9     for(int i = cnt; i >= v ; i--){
    10         dp[i] = max(dp[i],dp[i - v] + w);
    11     }
    12 }
    13 void complete(int v, int w) {
    14     for(int i = v; i <= cnt ; i++){
    15         dp[i] = max(dp[i],dp[i - v] + w);
    16     }
    17 }
    18 void multiply(int v,int w,int num) {
    19     if(v*num < cnt){
    20         for(int i = 1; i < num ; ){
    21             ZeroOne(i*v,i*w);//二进制优化
    22             num -= i;
    23             i <<= 1;
    24         }
    25         ZeroOne(num*v,num*w);
    26     }
    27     else
    28         complete(v,w);
    29 }
    30 int main()
    31 {
    32     int a[10];
    33     int Case  = 1;
    34 
    35     while( scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]) != EOF ) {
    36         cnt = 0;
    37         for(int i = 1; i <= 6; i++) {
    38             cnt += a[i]*i;
    39         }
    40         if(!cnt) break;
    41         printf("Collection #%d:
    ",Case++);
    42         if(cnt%2) { printf("Can't be divided.
    
    ");continue;}
    43         cnt /= 2;
    44         memset(dp,0,sizeof(dp));
    45         for(int i =1; i <= 6; i++) {
    46             multiply(i,i,a[i]);
    47         }
    48         if(dp[cnt] == cnt) printf("Can be divided.
    
    ");
    49         else printf("Can't be divided.
    
    ");
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    实战MEF(4):搜索范围
    实战MEF(3):只导出类的成员
    Mac 配置 php-fpm 时出现'/private/etc/php-fpm.conf': No such file or directory (2)
    如何实现在H5里调起高德地图APP?
    PHPExcel 基本用法详解
    最简单的css实现页面宽度自适应
    去掉IntelliJ IDEA 中 mybatis 对应的 xml 文件警告
    SQLyog恢复数据库报错解决方法【Error Code: 2006
    解决svn log显示no author,no date的方法之一
    Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
  • 原文地址:https://www.cnblogs.com/creativepower/p/7469023.html
Copyright © 2011-2022 走看看