(01背包搜索) zoj 3013

Watashi‘s BG


Time Limit: 3 Seconds      Memory Limit: 65536 KB


Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer training camp, students are divided into several groups and each day one of the groups will design some problems to hold a contest. Today students of Group C are required to design the problems, and they spent the whole night to check to test data which made them very tired. Watashi decides to give some money as a reward to group C so that they can buy the lunch for free.

There are N days in the training schedule, and all students have booked their lunch for N days so we know how much money they will spend in each day. Now the leader of group C needs to decide how to use Watashi‘s money. Since the money is limited, it may not be possible that they can have free lunch every day. So each day the leader can choose to pay for the whole group‘s lunch by themselves or use Watashi‘s money. Of course, the leader wants to spend Watashi‘s money as much as possible, but he is too busy to write a program to calculate the maximum money he can spend from Watashi‘s reward. Can you help him?

Input

The input contains multiple test cases ( no more than 50 test cases ).
In each test case, first there are two integer, N ( 1 <= N <=30 ) , which is the number of training days, M ( 0 <= M <=10000000 ) , which is the reward money from Watashi.
Then there is a line containing N positive integers with the ith integer indicating the money group C need to pay for the lunch of the ith day. All these integers are no more than 10000000 and integers are seperated by a space.

Output

For each test case, output one line with an integer which is the maximum money group C can spend from Watashi‘s reward

Sample Input

3 10
8 4 5

Sample Output

9
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<cstring>
using namespace std;
int n,m,w[35],ans;
void dfs(int x,int sum)
{
    if(ans==m)
        return ;
    if(sum>ans)
        ans=sum;
    for(int i=x;i<n&&sum+w[i]<=m;i++)
        dfs(i+1,sum+w[i]);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int sum=0;
        ans=0;
        for(int i=0;i<n;i++)
            scanf("%d",&w[i]),sum+=w[i];
        if(sum<=m)
        {
            printf("%d\n",sum);
            continue;
        }
        sort(w,w+n);
        dfs(0,0);
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-10-23 22:25:46

(01背包搜索) zoj 3013的相关文章

HDU 5887 Herbs Gathering(搜索求01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做,但是需要一些剪枝,先按体积排序,优先考虑体积大的物品,这样剪枝会剪得多一些(当然按照性价比排序也是可以的). 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdi

01背包 暴力搜索

/** 01背包,recursive * 05.08/2014 */ #include <cstdio> #include <cstring> #include <algorithm> #define MAXN 30000 using namespace std; int N,W; int w[MAXN],v[MAXN]; int solve(int i, int tw) { int res; if( i == N) //已经全部搜索完 res = 0; else if

HDU3810 Magina(搜索+用优先队列模拟01背包)经典

Magina Time Limit: 60000/30000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 528    Accepted Submission(s): 177 Problem Description Magina, also known as Anti-Mage, is a very cool hero in DotA (Defense of the Anci

bnu 28890 &amp;zoj 3689——Digging——————【要求物品次序的01背包】

Digging Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 368964-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory 2-SAT Articulation

zoj 2822 Sum of Different Primes (01背包)

///给你n 求他能分解成多少个的不同的k个素数相加之和 ///01背包,素数打表 # include <stdio.h> # include <algorithm> # include <string.h> # include <math.h> # include <iostream> using namespace std; int cot; int used[1500]; int prime[1500]; void sushu()///素数

ZOJ 3703 Happy Programming Contest(0-1背包)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3703 Happy Programming Contest Time Limit: 2 Seconds      Memory Limit: 65536 KB In Zhejiang University Programming Contest, a team is called "couple team" if it consists of only two s

超大背包(挑战编程之01背包)

先来温习01背包: 01背包是在M件物品取出若干件放在空间为W的背包里,每件物品的体积为W1,W2--Wn,与之相对应的价值为P1,P2--Pn. 求出获得最大价值的方案. 注意:在本题中,所有的体积值均为整数. 思路: 考虑用动态规划的方法来解决,这里的:阶段是:在前N件物品中,选取若干件物品放入背包中:状态是:在前N件物品中,选取若干件物品放入所剩空间为W的背包中的所能获得的最大价值:决策是:第N件物品放或者不放:由此可以写出动态转移方程:我们用f[i,j]表示在前 i 件物品中选择若干件放

0-1背包-回溯法

算法描述: 0-1背包的回溯法,与装载问题的回溯法十分相似.在搜索解空间树时,只要其左儿子结点是一个可行结点,搜索就进入其左子树.当右子树中有可能包含最优解时才进入右子树进行搜索.否则将右子树剪去. 计算右子树上界的更好算法是: 将剩余物品依其单位重量价值排序,然后依次装入物品,直至装不下时,再装入该物品的一部分而装满背包. 算法实现: 由Bound函数计算当前节点处的上界. 类Knap的数据成员记录解空间树的节点信息,以减少参数传递及递归调用所需的栈空间. 在解空间树的当前扩展结点处,仅当要进

hdu3448 01背包+dfs

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 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 ca