ZOJ 3258 Wistone and Owenwater 01背包+完全背包

点击打开链接

 

Wistone and Owenwater

 


 

Time Limit: 1 Second                                     Memory Limit: 32768 KB                            

 


DD and his girlfriend love travelling, one day they go to a strange cave. DD suddenly found some valuable stones and water named "wistone" and "owenwater". If DD could take them to the outer world, he could earn a lot:) Unfortunately, DD only takes a bottle with capacity W with him. He can only use this bottle to take them back.

As you know, each type of "wistone" and "owenwater" has different value, and "wistone" cannot be divided into smaller ones while "owenwater" can be taken as much as you want if it is avaliable.

Now, you are asked to calculate the maximum value DD can take back.

Input

This problem contains several test cases, you should process it to the end of input.

For each test case, there are two integers in the first line, N (1 <=N <= 100) and W ( 0 <= W <= 50000), N indicates the total type number of "wistone" and "owenwater".W indicates the capacity of bottle.

Then next n lines, each line contains three integers, ai, bi (1 <= ai, bi <= 1000) and ti. ai indicates the amount, bi indicates the total value and if ti is 0, it means this one is "wistone" otherwise it is "owenwater".

Output

For each test case, you should only output one number(accurate to 2 digits to the right of the decimal point), namely the maximum value DD can get.

Sample Input

3 150
100 100 0
100 100 0
130 10 1

Sample Output

103.85

 

 

 

省赛之前,来道背包练练手。

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,w,i,j;
double maxx[500007];
struct note
{
    int a,b;
    double value;
}water[107],stone[107];

int cmp(const note a,const note b)
{
    return a.value>b.value;
}

int main()
{
    while(scanf("%d%d",&n,&w)!=EOF)
    {
        int a,b,c,count_w=0,count_s=0;
        while(n--)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(!c)
            {
                stone[count_s].a=a;
                stone[count_s++].b=b;
            }
            else
            {
                water[count_w].a=a;
                water[count_w].b=b;
                water[count_w++].value=(double)b/(double)a;
            }
        }
        memset(maxx,0,sizeof(maxx));
        for(i=0;i<count_s;i++)//01背包 
        for(j=w;j>=stone[i].a;j--)
        {
            if(maxx[j]<maxx[j-stone[i].a]+stone[i].b)
            maxx[j]=maxx[j-stone[i].a]+stone[i].b;
        }
        sort(water,water+count_w,cmp);
        int tv=0,tw=0;
        double ans=maxx[w];
        for(i=0;i<count_w;i++)//完全背包 
        {
        	for(j=0;j<=water[i].a;j++)
	        {
	        	if(w-j-tw<0)break;
	        	if(ans<maxx[w-j-tw]+j*water[i].value+tv)
	        	ans=maxx[w-j-tw]+j*water[i].value+tv;
	        }
	        tv+=water[i].b;
	        tw+=water[i].a;
        }
        printf("%.2f\n",ans);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值