ACboy needs your help————杭电OJ

题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1712
思路
分组背包模板题(刚开始忘了清空dp数组,一直WA,QAQ),压缩成一维后**f [ j ]**表示在只能选 i 门的情况下,不超过 j 天的收益。然后状态转移方程是分组背包模板,f [ j ] = max ( f [ j ] ,f [ j - k ] + a [ i ] [ k ])
关于分组背包:https://www.acwing.com/problem/content/video/9/(y总闫氏分析法很强)。
c++代码

#include<bits/stdc++.h>
using namespace std;
const int N=105;
int n,m;
int f[N];
int main()
{
    
    
    while(1)
    {
    
    
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)
        {
    
    
            break;
        }
        else
        {
    
    
            memset(f,0,sizeof f);
            int a[n+1][m+1];
            int v[N][N];
            int w[N][N];
            for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
    
    
                scanf("%d",&a[i][j]);
            }
            for(int i=1;i<=n;i++)
            {
    
    
                for(int j=m;j>=1;j--)
                {
    
    
                    for(int k=1;k<=j;k++)
                    {
    
        
                        f[j]=max(f[j],f[j-k]+a[i][k]);  
                    }
                }
            }
            printf("%d\n",f[m]);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Star_Platinum14/article/details/111995574
今日推荐