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;
    }
    
  • 相关阅读:
    C语言:运算结果保留两位小数
    FFmpeg: AVFrame中的data和extend_data的区别
    android studio: 配置版权信息(转)
    C++: C++11 成员函数作为pthread线程 (转)
    android studio: 取消行注释在第一列
    C 语言 int 读写是否需要加锁
    【转】浅析Linux中的零拷贝技术--简单形象
    【转】Linux 内核态与用户态--简明清晰的介绍
    delete如何找到多重继承对象的内存块起始地址
    【转】内存管理内幕mallco及free函数实现--简易内存分配器、内存池、GC技术
  • 原文地址:https://www.cnblogs.com/juechen/p/5255974.html
Copyright © 2011-2022 走看看