HDU 1712 分组背包
2024-10-11 08:31:08
ACboy needs your help
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4912 Accepted Submission(s): 2651
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.
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
Source
题目意思:
有n个课程,每个课程在不同天数得到不同价值,总共m天,问m天之内得到最大的价值。
有n个课程,每个课程在不同天数得到不同价值,总共m天,问m天之内得到最大的价值。
思路:
每个课程若在i天完成,就不能在j天完成。以n个课程为组,天数为体积,价值为价值,即得到分组背包的模型。
每个课程若在i天完成,就不能在j天完成。以n个课程为组,天数为体积,价值为价值,即得到分组背包的模型。
dp[i][j]=max(dp[i-1][j],dp[i-1][j-v[k]]+w[k])//dp[i][j]表示前i组已占用j体积时最大价值。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 105 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int a[N][N];
int n, m;
int dp[N]; main()
{
int i, j, k;
while(scanf("%d %d",&n,&m)==){
if(!n&&!m) break;
for(i=;i<=n;i++){
for(j=;j<=m;j++)
scanf("%d",&a[i][j]);
}
memset(dp,,sizeof(dp));
for(i=;i<=n;i++){
for(j=m;j>=;j--){
for(k=;k<=j;k++){
dp[j]=max(dp[j],dp[j-k]+a[i][k]);
}
}
}
printf("%d\n",dp[m]);
}
}
最新文章
- Extjs API - JS Duck
- 【MSDN原版】Windows 7 with SP1各版本下载
- 深度学习入门教程UFLDL学习实验笔记一:稀疏自编码器
- 小组开发项目NABC分析
- HW-文件恢复-测试300
- 使用 Infragistics 的 NetAdvantage 组件时替换部分菜单语言的方法
- 贝叶斯分类器(Bayes分类器)
- Playfair 加密
- 原生JS实现banner图的滚动与跳转
- /*+ hint*/用法,该如何解决
- PHP从入门到精通(四)
- MySQL5.6复制技术(3)-MySQL主从复制线程状态转变
- 【Python】基础知识
- StanFord ML 笔记 第十部分
- django序列化单表的4种方法的介绍
- 如何通俗理解贝叶斯推断与beta分布?
- python16_day01【介绍、基本语法、流程控制】
- crm--业务点详细概述
- C++ 调用Python3
- C++关于堆的函数