zoukankan      html  css  js  c++  java
  • POJ Dividing (多重背包 || DFS)

    Dividing

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
    Total Submission(s) : 1   Accepted Submission(s) : 1
    Problem 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.
     
    Source
    PKU
     
     
    多重背包:
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int num[7],V,flag;
    int dp[100010];
    
    void ZeroOnePack(int c,int w){
        for(int i=V;i>=c;i--){
            if(dp[i]<dp[i-c]+w)
                dp[i]=dp[i-c]+w;
            if(dp[i]==V){   //剪枝,当能够平分SumValue时退出 
                flag=1;
                return ;
            }
        }
    }
    
    void CompletePack(int c,int w){
        for(int i=c;i<=V;i++){
            if(dp[i]<dp[i-c]+w)
                dp[i]=dp[i-c]+w;
            if(dp[i]==V){
                flag=1;
                return ;
            }
        }
    }
    
    void MultiplyPack(int c,int w,int amount){
        if(c*amount>=V){
            CompletePack(c,w);
            return ;
        }
        if(flag)
            return ;
        int k=1;
        while(k<=amount){
            ZeroOnePack(k*c,k*w);
            if(flag)
                return ;
            amount-=k;
            k<<=1;
        }
        ZeroOnePack(amount*c,amount*w);
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int cases=0;
        while(1){
            int sum=0;
            for(int i=1;i<=6;i++){
                scanf("%d",&num[i]);
                sum+=i*num[i];
            }
            if(sum==0)
                break;
            if(sum%2){
                printf("Collection #%d:\n",++cases);
                printf("Can't be divided.\n\n");
                continue;
            }
            V=sum/2;
            //printf("V=%d\n",V);
            flag=0;
            memset(dp,-1,sizeof(dp));
            dp[0]=0;
            for(int i=1;i<=6;i++){
                MultiplyPack(i,i,num[i]);
                if(flag)
                    break;
            }
            //printf("flag=%d\n",flag);
            if(flag){
                printf("Collection #%d:\n",++cases);
                printf("Can be divided.\n\n");
            }else{
                printf("Collection #%d:\n",++cases);
                printf("Can't be divided.\n\n");
            }
        }
        return 0;
    }

    DFS:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int num[7];
    int sum,half;
    int flag;
    
    void DFS(int value,int pre){
        if(flag)
            return ;
        if(value==half){
            flag=1;
            return ;
        }
        for(int i=pre;i>=1;i--){
            if(num[i]){
                if(value+i<=half){
                    num[i]--;
                    DFS(value+i,i);
                    if(flag)
                        return ;
                }
            }
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int cases=0;
        while(1){
            sum=0;
            for(int i=1;i<=6;i++){
                scanf("%d",&num[i]);
                sum+=i*num[i];
            }
            if(sum==0)
                break;
            if(sum%2){
                printf("Collection #%d:\n",++cases);
                printf("Can't be divided.\n\n");
                continue;
            }
            half=sum/2;
            flag=0;
            DFS(0,6);
            if(flag){
                printf("Collection #%d:\n",++cases);
                printf("Can be divided.\n\n");
            }else{
                printf("Collection #%d:\n",++cases);
                printf("Can't be divided.\n\n");
            }
        }
        return 0;
    }
     
     
     
  • 相关阅读:
    Laex/Delphi-OpenCV
    webbrowser 防止读取 缓存
    tesnsorflow 版本安装错了。 可以这样删除。
    python中%代表什么意思?
    python 访问 网页 获得源码
    3.2.2 break 与 continue 语句
    3.2.1 for循环与while循环的基本语法
    3.2 循环结构
    3.1.2 选择结构的几种形式
    3.1.1 条件表达式
  • 原文地址:https://www.cnblogs.com/jackge/p/3035341.html
Copyright © 2011-2022 走看看