zoukankan      html  css  js  c++  java
  • ZOJ 3631 Watashi's BG(DFS)

    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
    

    Author: HUANG, Qiao
    Contest: ZOJ Monthly, July 2012

     

    //开始用动态规划、果断超时

    //然后当成01背包,搜索160Ms

    //不过这题的有些地方剪枝很重要

    #include <iostream>
    #include <algorithm>
    #include <stdio.h>
    #include <string.h>
    #include <cmath>
    #include <stack>
    using namespace std;
    bool cmp(const int &a,const int &b)
    {
        return a>b;
    }
    int df[40];
    int W,n;
    int Max;
    void dfs(int i,int s)//这里的剪枝
    {

      if(Max==W) return;//加了这句优化是0Ms 

      if(s>W) return;
        if(i>=n)
         {
             Max=max(Max,s);
             return ;
         }
         int j,sum=s;
         for(j=i;j<n;j++)//这里的剪枝
          sum+=df[j];
         if(sum<Max) return;
         dfs(i+1,s+df[i]);
         dfs(i+1,s);
    }
    int main()
    {  int i;
       while(scanf("%d%d",&n,&W)!=EOF)
       {
           for(i=0;i<n;i++)
          {
             scanf("%d",&df[i]);
          }
          sort(df,df+n,cmp);//这里为剪枝作准备
          Max=0;
          dfs(0,0);
          printf("%d\n",Max);
       }
    return 0;
    }

  • 相关阅读:
    使用node调用支付宝生成支付二维码(沙箱环境)
    nodejs+mysql嵌套查询的问题
    移动端H5开发问题记录
    在jquery中,使用ajax上传文件和文本
    css百分比值到底参考谁?
    vue cli3 + cube-ui 配置rem踩坑记录
    MFC【exe】工程中的文件大致信息(翻译的)
    ActiveX、OLE和COM/DCOM
    系统版本
    图形库
  • 原文地址:https://www.cnblogs.com/372465774y/p/2614316.html
Copyright © 2011-2022 走看看