• 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;
    }

  • 相关阅读:
    DataTable 中varchar 转换为 Double 后重新 排序。
    asp.net 后台实现删除,划掉效果
    word2007二级标题自动编号不从大标题开始的解决方法
    asp.net 多个文件同时下载
    asp 时间倒数后按钮可用
    js获取gridview模板列中textbox行列的值
    JS错误 theForm.submit();SCRIPT3: 找不到成员。
    JS验证 只能输入小数点,数字,负数。
    关于SQLServer2008数据如何导入SQL2005的解决办法,高版本数据导入低版本中。
    asp.net中TreeView的大数据加载速度优化
  • 原文地址:https://www.cnblogs.com/372465774y/p/2614316.html
Copyright © 2020-2023  润新知