【DP】HDU 1712 ACboy needs your help 背包
将一门课学习几天当成一个物品
一门课中的m天 只能选择其中的一天
所以对于第i门课来说 用dp数组储存当前这门课的最大值
用out储存这门课选择了一天后的最大值
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #include <queue> #include <stack> #include <vector> #include <deque> #include <set> #include <map> #define cler(arr, val) memset(arr, val, sizeof(arr)) #define IN freopen ("in.txt" , "r" , stdin); #define OUT freopen ("out.txt" , "w" , stdout); typedef long long LL; const int MAXN = 111;//点数的最大值 const int MAXM = 20006;//边数的最大值 const int INF = 11521204; const int mod=1000000007; int n,m,cost[MAXN][MAXN],dp[MAXN],out[MAXN]; int main() { while(scanf("%d%d",&n,&m),n+m) { cler(out,0); cler(dp,0); cler(cost,0); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&cost[i][j]); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { for(int k=m;k>=j;k--) { dp[k]=max(dp[k],out[k-j]+cost[i][j]); } } for(int k=0;k<=m;k++) out[k]=dp[k]; } printf("%d\n",out[m]); } }