hdu 3535 分组背包

题意:

  有n组工作,现在有T分钟时间去做一些工作。每组工作里有m个工作,并且类型为s,s类型可以为0,1,2,分别表示至少选择该组工作的一项,至多选择该工作的一项,不限制选择。每个工作有ci,gi两个属性,表示需要花费ci时间去完成该项工作,完成后将会获得gi的快乐值,现在求快乐值最大多少,如果不能完成工作,输出-1;

原题如下:

AreYouBusy
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4161    Accepted Submission(s): 1655

Problem Description
Happy New Term!
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)?

Input
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.

Output
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 .

Sample Input
3 3
2 1
2 5
3 8
2 0
1 0
2 1
3 2
4 3
2 1
1 1

3 4
2 1
2 5
3 8
2 0
1 1
2 8
3 2
4 4
2 1
1 1

1 1
1 0
2 1

5 3
2 0
1 0
2 1
2 0
2 2
1 1
2 0
3 2
2 1
2 1
1 5
2 8
3 2
3 8
4 9
5 10

Sample Output
5
13
-1
-1
 

代码如下:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
#define INF (-99999)
const int Ni = 120;
int dp[Ni][Ni];
int main()
{
    int n,m,i,j,k,num,c,v,typ;
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&num,&typ);
            if(typ==0) for(int p=0;p<=m;p++) dp[i][p]=INF;

            else //把上一个包的值传递下去,目的是为了给某组可以选择不止一个的时候用
                for(int p=0;p<=m;p++) dp[i][p]=dp[i-1][p];
            for(j=1;j<=num;j++)
            {
                scanf("%d%d",&c,&v);
                for(k=m;k>=c;k--)
                {
                    int a1 = dp[i][k-c]+v;          //在已经选择该组的基础上,再次选择该组里的某个元素。
                    int a2 = dp[i-1][k-c]+v;        //在一个没有选择该组的基础上,选择该组里的元素
                    int a3 = dp[i-1][k];            //不选择该组的元素。
                    if(typ==0)//最少选一个
                        dp[i][k] = max(dp[i][k],max(a1,a2));
                    else if(typ==1)//最多选一个
                        dp[i][k] = max(dp[i][k],max(a2,a3));
                    else //不限
                        dp[i][k] = max(dp[i][k],max(max(a1,a2),a3));
                }
            }
        }
        dp[n][m] = dp[n][m] > 0 ? dp[n][m] : -1;
        printf("%d\n",dp[n][m]);

    }
    return 0;
}
时间: 2024-10-14 07:37:09

hdu 3535 分组背包的相关文章

HDU 3033 分组背包

http://www.hgy413.com/1319.html 简介DeviceIoControl的三种通信方式 HDU 3033 分组背包,布布扣,bubuko.com

hdu 1712 分组背包

背景:1Y,01背包多加了一个挑选循环而已. 分组背包的典型描述:对于很多背包,把它分为k个组,每个组内的组员是相互冲突的,所以只能选择一个. 我的代码: #include<cstdio> #include<iostream> #include<cstring> using namespace std; int main(void){ int n,m; while(scanf("%d%d",&n,&m),n*n+m*m){ int c

HDU 4341 分组背包

B - Gold miner Time Limit:2000MS Memory Limit:32768KB     Description Homelesser likes playing Gold miners in class. He has to pay much attention to the teacher to avoid being noticed. So he always lose the game. After losing many times, he wants you

HDU 3033 分组背包变形(每种至少一个)

I love sneakers! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4464    Accepted Submission(s): 1824 Problem Description After months of hard working, Iserlohn finally wins awesome amount of sc

hdu 1712 分组背包入门

对于每门课程,学习的时间不同,收获也不同,在一门课程上花费了两个不同的时间去学习是互斥的,即它们是属于同一个组内的物品,直接做分组背包即可. 需要注意三重循环的顺序不可变! 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int INF = -99999999; 7 const int N = 101; 8 int a[N][N

HDU 3535 分组混合背包

http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n组工作,T时间,每个工作组中有m个工作,改组分类是s,s是0是组内至少要做一件,是1时最多做一件,2时随意,每项工作的描述是花费的时间和获得的快乐值,求在T时间内可获的最大快乐值. memset放错位置了,折腾老半天. 分组混合背包,有的取一件或不取,有的随意,有的最少一个 分三种情况讨论 s==0 考虑前面取过时这次取或不取,前一组取过时这次取或不取 s==1 考虑前一组取过时这次取或不取

HDU 3535 【背包】

题意: 给出n组数据,每组数据有一个类型. 0代表至少选择一个,1代表至多选择一个,2代表任意选择. 给出背包容量. 如果背包不能满足最基本的要求输出-1. 思路: 背包问题变相考察~ 当0的时候初始化为-INF,然后就能保证至少选择一个. 当1或2的时候初始化上一层的值,然后1和2稍微有点区别,1只能从上一层得到下一层,2可以用本层更新. #include<stdio.h> #include<string.h> #include<algorithm> using na

hdu 3033 分组背包(每组至少选一个)

题意:有个小娃娃得了奖学金要去买东西,一共有n个东西分为k组,每个东西有一个花费和价值,问在每组东西至少买一个的条件下,小娃娃用他的奖学金买东西可以获得的最大价值. 思路:定义状态dp[i][v]表示在[1, i]组物品都至少有一个被购买时用v(背包容量)这么多钱能得到多少价值. 状态转移方程: if ( dp[i][v - cost[i][j]] != -1 ) dp[i][v] = max( dp[i][v], dp[i][v - cost[i][j]] + val[i][j] ); if

[HDU 3535] AreYouBusy (动态规划 混合背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n个任务集合,需要在T个时间单位内完成.每个任务集合有属性,属性为0的代表至少要完成1个,属性为1的为至多完成1个,属性为2的为任意完成. 每个任务做完后都有个价值,问在T个时间单位内完成n个任务集合的任务获得的最大价值是多少?如果不能满足要求输出-1 首先先分析什么情况下输出-1: 因为属性为0的代表至少要完成1个,当遇到一个属性为0的任务集合里一个都无法完成的时候,输出-1. 其他