• HDU3535-AreYouBusy


    描述:

      As having become a junior, xiaoA recognizes that there is not much time for her to AC problems, because there are some other things for her to do, which makes her nearly mad. 

      What's more, her boss tells her that for some sets of duties, she must choose at least one job to do, but for some sets of things, she can only choose at most one to do, which is meaningless to the boss. And for others, she can do of her will. We just define the things that she can choose as "jobs". A job takes time , and gives xiaoA some points of happiness (which means that she is always willing to do the jobs).So can you choose the best sets of them to give her the maximum points of happiness and also to be a good junior(which means that she should follow the boss's advice)? 

      There are several test cases, each test case begins with two integers n and T (0<=n,T<=100) , n sets of jobs for you to choose and T minutes for her to do them. Follows are n sets of description, each of which starts with two integers m and s (0<m<=100), there are m jobs in this set , and the set type is s, (0 stands for the sets that should choose at least 1 job to do, 1 for the sets that should choose at most 1 , and 2 for the one you can choose freely).then m pairs of integers ci,gi follows (0<=ci,gi<=100), means the ith job cost ci minutes to finish and gi points of happiness can be gained by finishing it. One job can be done only once.

      One line for each test case contains the maximum points of happiness we can choose from all jobs .if she can’t finish what her boss want, just output -1 .

    代码:

      分组背包问题。组内有多种情况:至少选一个,至多选一个,无限制。

     

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<stdlib.h>
    #include <math.h>
    using namespace std;
    #define N 110
    #define MIN -1000000
    int MAX(int a,int b,int c){
        if( a<b ) a=b;
        if( a<c ) a=c;
        return a;
    }
    
    int main(){
        int n,t,val[N],time[N],dp[N][N];//dp第一维代表第n组物品;第二维代表背包容量,即时间
        int m,s;
        while( scanf("%d%d",&n,&t)!=EOF )
        {
            memset(dp,0,sizeof(dp));//没有要求装满
            for( int i=1;i<=n;i++ )
            {
                scanf("%d%d",&m,&s);
                for( int j=1;j<=m;j++ )
                    scanf("%d%d",&time[j],&val[j]);
                if( s==0 )//至少要选一个物品
                {
                    for( int j=0;j<=t;j++ )
                        dp[i][j]=MIN;//如果这一组的物品没有选,那么最后的解也将为负无穷,代表无解。
                    for( int j=1;j<=m;j++ )//m个物品
                    {
                        for( int k=t;k>=time[j];k-- )//背包容量递减,k-time[j]也递减,这样保证数据不会重叠
                        {
                            //dp[i][k-time[j]]+val[j]:从这一组物品的上一个状态累加。组内决策
                            //dp[i-1][k-time[j]]+val[j]:直接从上一组物品累加,代表第一次加入该物品。从上一个分组开始决策
                            dp[i][k]=MAX(dp[i][k],dp[i][k-time[j]]+val[j],dp[i-1][k-time[j]]+val[j]);
                        }
                    }
                }
                else if( s==1 )//最多选择一个
                {
                    for( int j=0;j<=t;j++ )
                        dp[i][j]=dp[i-1][j];//可以不选择任何一个物品,最后的解即为上一组的解。
                    for( int j=1;j<=m;j++ )
                    {
                        for( int k=t;k>=time[j];k-- )
                            dp[i][k]=max(dp[i][k],dp[i-1][k-time[j]]+val[j]);//只能从上一个分组开始决策,代表最多选择一个
                    }
                }
                else if( s==2 ){//无限制
                    for( int j=0;j<=t;j++ )
                        dp[i][j]=dp[i-1][j];//可以不选择任何一个物品,最后的解即为上一组的解。
                    for( int j=1;j<=m;j++ )
                    {
                        for( int k=t;k>=time[j];k-- )
                        {
                            //组内和组间决策
                            dp[i][k]=MAX(dp[i][k],dp[i][k-time[j]]+val[j],dp[i-1][k-time[j]]+val[j]);
                        }
                    }
                }
            }
            printf("%d
    ",max(-1,dp[n][t]));//无解
        }
        system("pause");
        return 0;
    }

     

     

  • 相关阅读:
    node实现将打包后的文件转压缩包
    Git/SVN忽略node_modules文件
    node实现发送邮件
    node搜索文件夹下的指定内容
    node批量修改文件文本内容
    微信小程序上线发布需要做的事情
    两件事 Jquery.form 锁
    .NET MVC 提交表单出现检测到有潜在危险的Request.Form值
    第一次使用TinyMCE
    第一次使用Entity Framework 的CodeFirst
  • 原文地址:https://www.cnblogs.com/lucio_yz/p/4744790.html
Copyright © 2020-2023  润新知