ACboy needs your help(分组背包)

本文介绍了一个课程安排优化问题,通过动态规划求解如何在有限的学习天数内安排多个课程以获得最大收益。输入包括课程数量及每天的学习效益,输出则是最大可能的总效益。
Problem Description
ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the profit?
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers N and M, N is the number of courses, M is the days ACboy has.
Next follow a matrix A[i][j], (1<=i<=N<=100,1<=j<=M<=100).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j].
N = 0 and M = 0 ends the input.
Output
For each data set, your program should output a line which contains the number of the max profit ACboy will gain.
Sample Input
2 2
1 2
1 3
2 2
2 1
2 1
2 3
3 2 1
3 2 1
0 0
Sample Output
3
4

6

伪代码:for 1...i组
for V....0
for 所有的k属于i组
dp[j]=max(dp[j],dp[j-v[i]]+w[i]);

#include<stdio.h>
#include<string.h>
#define Max 105
int w[Max][Max],dp[Max];
int m,n;
int main()
{
	//freopen("b.txt","r",stdin);
	int i,j,k;
	while(scanf("%d %d",&m,&n)==2)
	{
		if(m+n==0) break;
		for(i=1;i<=m;i++)
			for(j=1;j<=n;j++)
				scanf("%d",&w[i][j]);
			memset(dp,0,sizeof(dp));
			for(i=1;i<=m;i++)
				for(j=n;j>=0;j--)
				{
					for(k=1;k<=n;k++)
						if(j>=k) dp[j]=dp[j]>dp[j-k]+w[i][k]?dp[j]:dp[j-k]+w[i][k];
				}
				printf("%d\n",dp[n]);
	}
	return 0;
}


HDU1702—ACboy 请求帮助问题 时间限制:1000ms,空间限制:32768K 问题描述:ACboy 被绑架了!他非常想念母亲并且十分恐慌。你无法想象他呆的房间 多么黑暗,那么可怜。作为一个聪明的 ACMer,你想让 ACboy 从怪物的迷宫中走出来。 但是当你到达迷宫的大门时,怪物说:“我听说你很聪明,但如果不能解决我的问题,你 会和 ACboy 一起死。”墙上显示了怪物的问题:每个问题的第一行是一个整数 N(命令数)和一个单词“FIFO”或“FILO”(你很高兴因为你知道“FIFO”代表“先进先出”,“FILO”意思是“先进后出”。以下 N 行,每行是“IN M”或“OUT”,(M 代表一个整数)。问题的答案是门的密码,所以如果你想拯救 ACboy,请仔细回答问题! 输入格式:输入包含多个测试用例。第一行有一个整数,表示测试用例的数量,并且每个子问题的输入像前面描述的方式。 输出格式: 对于每个命令“OUT”,应根据单词是“FIFO”还是“FILO”输出一个整数,如果没有任何整数,则输出单词“None”。 输入样例: 4 4 FIFO IN 1 IN 2 OUT OUT 4 FILO IN 1 IN 2 OUT OUT 5 FIFO IN 1 IN 2 OUT OUT OUT 5 FILO IN 1 IN 2 OUT IN 3 OUT 输出样例: 1 2 2 1 1 2 None 2 3 要求:①提供详细的算法设计思路,思路分为输入、处理和输出三个部分。②生成解题的Java代码。允许存储所有输入,并一次性输出结果,解题代码需包含丰富的中文注释。③解题代码和算法设计思路分开显示,并使用文本格式而非Markdown格式显示。
最新发布
01-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值