Zoj 3164 cookie choice (multiple backpacks + group backpacks, update queue optimization)

Source: Internet
Author: User


Cookie choice

Time Limit: 2 seconds memory limit: 32768 KB

MM enjoyed cookies very much. on Saint Valentine's Day, when she stepped into a big cookie store again, she wouldn't leave unless dd spent all his money in pocket!

There areNKinds of cookies, labeled from1ToN, And all can be bought without any restriction by the store. But actually, for some kinds of cookies, MM wanted to buy one piece at most, and for some kinds
Of cookies, MM wanted to buyKiPieces at most, and for some other kinds of cookies, there didn't exist an upper bound that MM wanted to buy.

There is another requirement from mm: There are some groups of cookies, MM considered their tastes similar, so she wanted to buy at most one kind of cookies in each group. A kind of cookie wouldn't appear in more than one group.

ForITh kind of cookies, MM has an "Enjoyable value"EI, If dd boughtAIPieces of this kind for her, andAIDidn't exceed her upper bound, MM getEiaiOf
Enjoyable value. After buying cookies, MM's total enjoyable value will be the sumEiai.

But actually, poor dd had onlyDDollars, and the price forITh kind of cookies isPiDollars per piece. dd must spend all hisDDollars to buy cookies, to meet requirements
About Amount and taste from mm, and to make MM's enjoyable value as high as possible. what's more, as you know, a legal plan's enjoyable value must be non-negative.

Input

There are multiple test cases. Each test case consists of three parts.

The first part is one line with two integersNAndD.

The second part hasNLines, lineIConsists of three IntegersKi,EIAndPi. IfKiEquals0,
It meansITh kind of cookies, there didn't exist an upper bound that MM wanted to buy, otherwiseKiIs the upper boundITh kind of cookies.

The third part describes the groups. A non-negative integerGRepresents the number of groups, and thenGLines, each line consists of some integers represents labels of kinds of cookies in this group.

One blank line between test cases.

Output

If the proper and optimal plan exists, output the maximal total enjoyable value ΣEiai, Otherwise output "I'm sorry ...".

Output one line per text case.

Data Restriction

1 <=N& Lt; = 1024, 0 & lt; =D<= 1024.

0 <=Ki<= 1024,-1024 <=EI<= 1024, 0 <Pi<=D.

0 <=G<= 8.

All numbers referred are integers.

Number of test cases is no more than 80.

Sample Input

2 10240 1 30 0 1010 10231 1 11 1 21 1 41 1 81 1 161 1 321 1 641 1 1283 -1 2561 1 51219 1010 10231 1 11 1 21 1 41 1 81 1 161 1 321 1 641 1 1281 1 2561 1 51219 10

Sample output

3415i'm sorry...
Author: Cui, Tianyi

Source:Zoj monthly, February 2009

Question: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3164 analysis: This is the answer from Cui Tianyi, the author of the paper bag 9. This is a complicated question. It uses multiple backpacks, multiply the idea, and group backpacks... It's a little slow to write.
I saw the xch paper in last night. I had a new understanding of the optimization of the backpack. Then I chose this question and used the queue Optimization of multiple backpacks, there is also the optimization of merging generalized items, that is, an optimization of grouping backpacks. The rank1 is immediately refreshed.

Code:

#include<cstdio>#include<iostream>using namespace std;const int mm=1111;const int lm=-10000000;int f[mm],tmp[mm],s[mm],e[mm],p[mm],t[mm],w[9][mm];int i,j,k,v,l,r,n,d,g;bool get(int g){    int a,b;    while(((b=getchar())<'0'||b>'9')&&b!='\n');    if(b=='\n')return 0;    for(a=0;b>='0'&&b<='9';b=getchar())a=a*10+b-'0';    t[a-1]=g;    return b!='\n';}void clear(int f[]){    for(int i=1;i<=d;++i)f[i]=-1000000000;    f[0]=0;}void CompletePack(int v,int e,int f[]){    for(int i=v;i<=d;++i)f[i]=max(f[i],f[i-v]+e);}void ZeroOnePack(int v,int e,int f[]){    for(int i=d;i>=v;--i)f[i]=max(f[i],f[i-v]+e);}int main(){    while(scanf("%d%d",&n,&d)!=-1)    {        for(i=0;i<n;++i)scanf("%d%d%d",&s[i],&e[i],&p[i]),t[i]=0;        scanf("%d",&g);        for(getchar(),i=1;i<=g;++i)while(get(i));        for(i=1;i<=g;++i)clear(w[i]);        clear(f);        for(i=0;i<n;++i)            {                if(t[i])clear(tmp);                if(!s[i]||s[i]*p[i]>=d)CompletePack(p[i],e[i],t[i]?tmp:f);                else                {                    j=1;                    while(j<s[i])                    {                        ZeroOnePack(p[i]*j,e[i]*j,t[i]?tmp:f);                        s[i]-=j;                        j<<=1;                    }                    ZeroOnePack(p[i]*s[i],e[i]*s[i],t[i]?tmp:f);                }                if(t[i])for(j=1;j<=d;++j)w[t[i]][j]=max(w[t[i]][j],tmp[j]);            }        for(k=1;k<=g;++k)            for(i=d;i>=0;--i)                for(j=1;j<=i;++j)                    if(f[i-j]>lm&&w[k][j]>lm)                        f[i]=max(f[i],f[i-j]+w[k][j]);        if(f[d]>=0)printf("%d\n",f[d]);        else puts("i'm sorry...");    }    return 0;}

Code 2:

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int mm=1111;int f[mm],f1[mm],f2[mm],t[mm],e[mm],p[mm],s[mm],id[mm],q[mm],qw[mm];int i,j,k,n,d,g;bool get(int g){    int a,b;    while((b=getchar())<'0'||b>'9');    for(a=0;b>='0'&&b<='9';b=getchar())a=a*10+b-'0';    t[a-1]=g;    return (b!='\n');}bool cmp(int a,int b){    return t[a]<t[b];}void CompletePack(int e,int p){    for(int i=0;i<=d;++i)f2[i]=f1[i];    for(int i=p;i<=d;++i)    {        f2[i]=max(f2[i],f2[i-p]+e);        f[i]=max(f[i],f2[i]);    }}void MultiPack(int s,int e,int p){    int i,j,k,l,r,now,m;    for(i=0;i<p;++i)        for(m=(d-i)/p,j=l=0,r=-1;j<=m;++j)        {            now=f1[k=j*p+i]-j*e;            while(l<=r&&qw[r]<=now)--r;            q[++r]=j,qw[r]=now;            if(j-q[l]>s)++l;            f[k]=max(f[k],qw[l]+j*e);        }}int main(){    while(~scanf("%d%d",&n,&d))    {        for(i=0;i<n;++i)            t[i]=0,id[i]=i,scanf("%d%d%d",&s[i],&e[i],&p[i]);        scanf("%d",&g);        for(i=1;i<=g;++i)while(get(i));        sort(id,id+n,cmp);        for(i=0;i<=d;++i)f1[i]=f[i]=-1e8;        f1[0]=f[0]=0;        for(i=0;i<n;++i)        {            k=id[i];            if(!s[k]||s[k]*p[k]>=d)CompletePack(e[k],p[k]);            else MultiPack(s[k],e[k],p[k]);            if(!t[k]||t[k]!=t[id[i+1]])                for(j=0;j<=d;++j)f1[j]=f[j];        }        if(f[d]>=0)printf("%d\n",f[d]);        else puts("i'm sorry...");    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.