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 }
  • 相关阅读:
    wcf布到服务器下载文件大小限制问题
    动态修改母版页中的DIV标签中的LI的A的CLASS属性
    c# 分页的方法
    Java学习笔记-异常处理-有风险的行为
    ASP.NET Core中的静态文件
    在ASP.NET Core中进行配置
    ASP.NET Core中的中间件和请求管道
    Kestrel:ASP.NET Core的Web服务器
    ASP.NET Core Program.cs
    ASP.Net Core Startup类
  • 原文地址:https://www.cnblogs.com/creativepower/p/7469023.html
Copyright © 2011-2022 走看看