HDU 3466 Proud Merchants(变种01背包 + 无后效性)
来源:互联网 发布:华师大网络教育好不好 编辑:程序博客网 时间:2024/06/11 18:55
思路:
一般的01背包转移方程为:
但此题加了个限定条件,只有当当前重量
即:
for(int i = 1;i <= n;i++){ for(int j = m;j - a[i].q >= 0;j--){ dp[j] = max(dp[j],dp[j- a[i].p]+a[i].v); }}
也就是说我们在对 j 进行选择的时候,我们一定得保证 **dp[ a[i].q-a[i].p ] 到 dp[m] 都得是当前最优解,然而我们发现,如果当前物品a[i].q-a[i].p 的值不是最小的,那么它所用的那个dp的值便不是最优解了,即 受到了影响。大概如此
#include <iostream>#include <cstdio>#include <string.h>#include <queue>#include <cmath>#include <algorithm>#include <map>#define pi acos(-1)#define eps 1e-9typedef long long int lli;using namespace std;struct it{ int p,q,v;}a[505];bool cmp(const it&a,const it&b){ return a.q-a.p < b.q-b.p;}lli dp[5050];int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ memset(dp,0,sizeof(dp)); for(int i = 1;i <= n;i++){ scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v); } sort(a+1,a+1+n,cmp); for(int i = 1;i <= n;i++){ for(int j = m;j - a[i].q >= 0;j--){ dp[j] = max(dp[j],dp[j- a[i].p]+a[i].v); } } printf("%lld\n",dp[m]); }}
1 0
- HDU 3466 Proud Merchants(变种01背包 + 无后效性)
- Hdu 3466 Proud Merchants(01背包变种)
- 3466Proud Merchants(需消除后效性的01背包)
- HDU 3466 Proud Merchants(01背包)
- hdu 3466 Proud Merchants(01背包)
- hdu 3466 Proud Merchants (01背包)
- hdu 3466 Proud Merchants 01背包 (顺序性)
- (step 3.3)hdu 3466(Proud Merchants——01背包变种)
- HDU 3466 Proud Merchants 01背包
- [01背包]HDU 3466 Proud Merchants
- HDU 3466 - Proud Merchants(01背包)
- hdu 3466 Proud Merchants(01 背包)
- HDU--3466--Proud Merchants--01背包
- HDU 3466--Proud Merchants【01背包变形】
- HDU 3466(Proud Merchants)有条件01背包
- HDU 3466 Proud Merchants 【01背包】
- HDU - 3466 Proud Merchants(01背包)
- HDU-3466-Proud Merchants(01背包问题)
- 设计模式(七)桥接模式
- java文件路径问题
- YUV420转换成RGB24
- python类
- spark Exception in thread "main" java.lang.IllegalArgumentException: java.net.URISyntaxException: Re
- HDU 3466 Proud Merchants(变种01背包 + 无后效性)
- L1-016. 查验身份证
- 《剑指offer》005-从尾到头打印链表
- 分金子
- EL表达式
- xsocket死锁问题排查
- rails创建项目报"please update your path to include build tools...."
- CodeForces 785D Anton and School
- oc——类——__strong __weak __unsafe_unretained