HDU 3466 Proud Merchants

$01$背包。

这个由于有个$Q$的限制,为了去掉后效性,每一个物品第二层的循环$j$最小可能是$Q-P$,那么就按照$Q-P$从小到大对物品排序。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;

const int maxn=600;
struct X
{
    int p,q,v;
}s[maxn];
int n,m;
int dp[10*maxn];

bool cmp(X a,X b){ return a.q-a.p<b.q-b.p; }

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++) scanf("%d%d%d",&s[i].p,&s[i].q,&s[i].v);
        sort(s+1,s+1+n,cmp);
        memset(dp,0,sizeof dp);
        for(int i=1;i<=n;i++)
        {
            for(int j=m;j>=s[i].q;j--)
            {
                dp[j]=max(dp[j],dp[j-s[i].p]+s[i].v);
            }
        }
        printf("%d
",dp[m]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5824461.html