• ZOJ 3631 Watashi's BG DFS


    J - Watashi's BG

    Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

    Appoint description:
     

    Description

    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
    

    本来是一个很简单的背包问题的,背包容量太大,就T了

    于是就只有DFS然后搜索,注意剪枝

    bool cmp(int i,int j)
    {
        return i>j;
    }
    int ans=0;
    int a[40];
    int n,m;
    int vis[40];
    void dfs(int i,int v)
    {
        if(ans==m)
            return;
        if(v>m)
            return;
        if(v==m)
        {
            ans=v;
            return;
        }
        int sum=0;
        for(int j=i+1;j<n;j++)
        {
            if(!vis[j])
                sum+=a[j];
        }
        if(v+sum<=ans)
            return;
        ans=max(ans,v);
        for(int j=i+1;j<n;j++)
        {
            if(vis[j])
                continue;
            vis[j]=1;
            dfs(j,v+a[j]);
            vis[j]=0;
        }
    }
    int main()
    {
        while(cin>>n>>m)
        {
            ans=0;
            for(int i=0;i<n;i++)
                scanf("%d",&a[i]);
            sort(a,a+n,cmp);
            for(int i=0;i<n;i++)
            {
                vis[i]=1;
                dfs(i,a[i]);
                vis[i]=0;
            }
            cout<<ans<<endl;
        }
    }
  • 相关阅读:
    【URAL 1004】 floyd最小环
    【UVA 10881】 经典模拟题
    【HDU 1541】 树状数组(入门题)
    【HDU 4000】 树状数组
    【HDU 3391 && HDU 4431】 dfs+模拟
    【HDU 1058 & HDU 3199 类似丑数】 简单DP思想
    Acdream原创群赛3(部分题解)
    vfor实现双层循环嵌套
    vue获取当前时间并实时刷新时间
    vue+element ui实现左侧导航栏动态路由跳转
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4316711.html
Copyright © 2020-2023  润新知