zoukankan      html  css  js  c++  java
  • Bag Problem

    Bag Problem
    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/131072 K (Java/Others)
    Total Submission(s): 1449 Accepted Submission(s): 405

    Problem Description
    0/1 bag problem should sound familiar to everybody. Every earth man knows it well. Here is a mutant: given the capacity of a bag, that is to say, the number of goods the bag can carry (has nothing to do with the volume of the goods), and the weight it can carry. Given the weight of all goods, write a program that can output, under the limit in the above statements, the highest weight.

    Input
    Input will consist of multiple test cases The first line will contain two integers n (n<=40) and m, indicating the number of goods and the weight it can carry. Then follows a number k, indicating the number of goods, k <=40. Then k line follows, indicating the weight of each goods The parameters that haven’t been mentioned specifically fall into the range of 1 to 1000000000.

    Output
    For each test case, you should output a single number indicating the highest weight that can be put in the bag.

    Sample Input

    5 100
    8
    8 64 17 23 91 32 17 12
    5 10
    3
    99 99 99

    Sample Output

    99
    0
    01背包问题,由于数据比较大,所以只能是搜索来模拟01背包

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <queue>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    typedef pair<int,int>p;
    const int INF = 0x3f3f3f3f;
    int n,k;
    int w[55];
    bool vis[55];
    int sum,m;
    int M;
    void DFS(int s,int num,int va)
    {
        if(s>=k||num>n)
        {
            return ;
        }
        M=max(M,va);
        for(int i=s; i<k; i++)
        {
            if(!vis[i])
            {
                if(w[i]+va>m)//由于序列是递增的,所以当质量大于m时,后面的都不符合
                {
                    return ;
                }
                vis[i]=true;
                DFS(i,num+1,va+w[i]);
                vis[i]=false;
            }
        }
    }
    int main()
    {
        while(~scanf("%d %d",&n,&m))
        {
            scanf("%d",&k);
            sum=0;
            for(int i=0; i<k; i++)
            {
                scanf("%d",&w[i]);
            }
            sort(w,w+k);
            for(int i=k-1; i>k-1-n; i--)
            {
                sum+=w[i];
            }
            if(w[0]>m)//特殊情况处理就是所有的货物都比背包容量大,
            {
                sum=0;
            }
            if(sum<m)
            {
                printf("%d
    ",sum);
                continue;
            }
            M=0;
            memset(vis,false,sizeof(vis));
            DFS(0,0,0);
            printf("%d
    ",M);
        }
        return 0;
    }
    
  • 相关阅读:
    第二次结对编程作业
    第一次结对编程作业
    第9组 团队展示
    第一次个人编程作业
    第一次博客作业
    2016-2017-1 20155215 信息安全技术 补课上测试
    2017-2018-1 20155215 第九周 加分项 PWD命令的实现
    2017-2018-1 20155215 《信息安全系系统设计基础》实验三
    2017-2018-1 20155215 《信息安全系统设计基础》第9周学习总结
    2017-2018-1 20155215 《信息安全系统设计基础》第8周学习总结
  • 原文地址:https://www.cnblogs.com/juechen/p/5255974.html
Copyright © 2011-2022 走看看