zoukankan      html  css  js  c++  java
  • (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
    ",sum);
                continue;
            }
            sort(w,w+n);
            dfs(0,0);
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    【存货管理】存货的计价方法
    【NHibernate】列“ReservedWord”不属于表 ReservedWords
    【MySQL】MySQL中where条件的执行分析
    brew卸载&重新安装
    mac nvm安装&使用&一些问题解决方案
    python初始环境配置
    股票数据api整理
    输入一个url到页面渲染完毕过程
    自己简单封装一个promise
    节流&防抖
  • 原文地址:https://www.cnblogs.com/water-full/p/4532528.html
Copyright © 2011-2022 走看看