您的位置:首页 > 其它

HDOJ 4501 小明系列故事——买年货 DP

2015-09-04 09:34 323 查看
普通DP


小明系列故事——买年货

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 2872    Accepted Submission(s): 1280


Problem Description

  春节将至,小明要去超市购置年货,于是小明去了自己经常去的都尚超市。

  刚到超市,小明就发现超市门口聚集一堆人。用白云女士的话说就是:“那家伙,那场面,真是人山人海,锣鼓喧天,鞭炮齐呤,红旗招展。那可真是相当的壮观啊!”。好奇的小明走过去,奋力挤过人群,发现超市门口贴了一张通知,内容如下:

  

  值此新春佳节来临之际,为了回馈广大顾客的支持和厚爱,特举行春节大酬宾、优惠大放送活动。凡是都尚会员都可用会员积分兑换商品,凡是都尚会员都可免费拿k件商品,凡是购物顾客均有好礼相送。满100元送bla bla bla bla,满200元送bla bla bla bla bla...blablabla....

  

  还没看完通知,小明就高兴的要死,因为他就是都尚的会员啊。迫不及待的小明在超市逛了一圈发现超市里有n件他想要的商品。小明顺便对这n件商品打了分,表示商品的实际价值。小明发现身上带了v1的人民币,会员卡里面有v2的积分。他想知道他最多能买多大价值的商品。

  由于小明想要的商品实在太多了,他算了半天头都疼了也没算出来,所以请你这位聪明的程序员来帮帮他吧。

 

Input

输入包含多组测试用例。

每组数据的第一行是四个整数n,v1,v2,k;

然后是n行,每行三个整数a,b,val,分别表示每个商品的价钱,兑换所需积分,实际价值。

[Technical Specification]

1 <= n <= 100

0 <= v1, v2 <= 100

0 <= k <= 5

0 <= a, b, val <= 100

Ps. 只要钱或者积分满足购买一件商品的要求,那么就可以买下这件商品。

 

Output

对于每组数据,输出能买的最大价值。

详细信息见Sample。

 

Sample Input

5 1 6 1
4 3 3
0 3 2
2 3 3
3 3 2
1 0 2
4 2 5 0
0 1 0
4 4 1
3 3 4
3 4 4

 

Sample Output

12
4

 

Source

2013腾讯编程马拉松初赛第〇场(3月20日)

 

/* ***********************************************
Author :CKboss
Created Time :2015年09月04日 星期五 08时51分26秒
File Name :HDOJ4501.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

int n,v1,v2,K;
struct WP
{
int a,b,val;
}wp[110];

int dp[2][110][110][110];

int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);

while(scanf("%d%d%d%d",&n,&v1,&v2,&K)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&wp[i].a,&wp[i].b,&wp[i].val);
}
memset(dp,0,sizeof(dp));
int now=1; int ans=0;
for(int i=1;i<=n;i++)
{
for(int j1=K;j1>=0;j1--) /// i-1 物品的 k
{
for(int j2=v1;j2>=0;j2--)
{
for(int j3=v2;j3>=0;j3--)
{
int last=now^1;
dp[now][j1][j2][j3]=max(dp[now][j1][j2][j3],dp[last][j1][j2][j3]);
if(j1>0)
dp[now][j1-1][j2][j3]=max(dp[now][j1-1][j2][j3],dp[last][j1][j2][j3]+wp[i].val);
if(j2>=wp[i].a)
dp[now][j1][j2-wp[i].a][j3]=max(dp[now][j1][j2-wp[i].a][j3],dp[last][j1][j2][j3]+wp[i].val);
if(j3>=wp[i].b)
dp[now][j1][j2][j3-wp[i].b]=max(dp[now][j1][j2][j3-wp[i].b],dp[last][j1][j2][j3]+wp[i].val);
}
}
}
if(i==n)
{
for(int j1=0;j1<=K;j1++)
{
for(int j2=0;j2<=v1;j2++)
{
for(int j3=0;j3<=v2;j3++)
{
int& t=dp[now][j1][j2][j3];
if(t>ans) ans=t;
}
}
}
}
now=now^1;
}
printf("%d\n",ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: