JZOJ 1620. 【Usaco2009 gold 】电视游戏问题

目录:


题目:

单击查看题目


分析:

跟精明的预算是一样的,都是有依赖的背包,只需要把主件、附件,换成平台和游戏就可以了
我们在实现代码时,可以用 f [ i ] [ j ] 表示选到第i个组,已花费j的代价所能获得的最大收益,然后在每次 d p 前将选择该游戏平台的代价减去,再进行普通背包
最后想让自己的代码更优秀,还可以考虑滚动


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
using namespace std;
inline LL read()
{
    LL d=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
    return d*f;
}
struct a{
    int cost,val;
}p[51],a[51][11];
int f[2][100001];
int main()
{
    /*freopen("vidgame.in","r",stdin);
    freopen("vidgame.out","w",stdout);*/
    int n=read(),m=read();
    for(int i=1;i<=n;i++)
    {
        p[i].cost=read();p[i].val=read();
        for(int j=1;j<=p[i].val;j++)
          a[i][j].cost=read(),a[i][j].val=read();
    }
    int w=0,ans=0;
    for(int i=1;i<=n;i++)
    {
        w^=1;
        for(int j=1;j<p[i].cost;j++) f[w][j]=f[w^1][j];
        for(int j=p[i].cost;j<=m;j++) f[w][j]=f[w^1][j-p[i].cost];//这两个是不选择p[i]这个平台
        for(int j=1;j<=p[i].val;j++)
          for(int k=m-a[i][j].cost;k>=p[i].cost;k--)
            f[w][k+a[i][j].cost]=max(f[w][k+a[i][j].cost],f[w][k]+a[i][j].val);
        //这个是选择p[i]这个平台,然后在里面选择游戏求最优解
        for(int j=p[i].cost;j<=m;j++)
          f[w][j]=max(f[w][j],f[w^1][j]),ans=max(ans,f[w][j]);
        //这个则是在选平台和不选平台间选择最优解
    }
    printf("%d",ans);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35786326/article/details/80960769