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;
    }
    
  • 相关阅读:
    2017-2018-2 《密码与安全新技术》课程总结
    2017-2018-2 《密码与安全新技术》论文总结
    2017-2018-2 20179226 《网络攻防》第14周作业
    2017-2018-2 《密码与安全新技术》第6周作业
    2017-2018-2 20179226 《网络攻防》第12周作业
    2017-2018-2 20179226 《网络攻防》第11周作业
    2017-2018-2 《密码与安全新技术》第5周作业
    2017-2018-2 20179226 《网络攻防》第10周作业
    2017-2018-2 《密码与安全新技术》第4周作业
    2017-2018-2 20179226 《网络攻防》第8周作业
  • 原文地址:https://www.cnblogs.com/juechen/p/5255974.html
Copyright © 2011-2022 走看看