HDU 3449 consumer [DP backpack]

Source: Internet
Author: User

Original question connection: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3449

Question: see it for yourself ~~~

Thought 1: first carry out a 01 backpack on the items in the box, then add the price of the box, group the backpack, and then TLE !! I wrote it for one afternoon !!!

Timeout code:

#include<stdio.h>#include<stdlib.h>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int Max = 1000000 +10;const int base = 2;int dp[Max],N,V;int loop[60];struct Goos{    int cost;    int value;}G[60][Max];void Zero_One(int cost , int value , int vb){    int i;    for(i = V-vb;i >= cost;i --)    {        dp[i] = max(dp[i] , dp[i-cost]+value);    }}void display(int m){    int i,j;    for(i = 1;i <= m;i ++)    {        for(j = 0;j < loop[i];j ++)            printf(" %d-%d ",G[i][j].cost,G[i][j].value);        printf("\n");    }}void Group_Package(int m){    int i,j,k;    memset(dp,0,sizeof(dp));    for(i = 1;i <= m;i ++)    {        for(j = V;j >= 0; j--)        {            for(k = 0;k < loop[i];k ++)            {                if(j >= G[i][k].cost)                    dp[j] = max(dp[j] , dp[j-G[i][k].cost]+G[i][k].value);            }        }    }    printf("%d\n",dp[V]);}int main(){    int i,j;    while(~scanf("%d%d",&N,&V))    {        int vb,k;        int m = 1;        //memset(Gdp,0,sizeof(Gdp));        memset(loop,-1,sizeof(loop));        for(i = 0;i < N;i ++)        {            scanf("%d%d",&vb,&k);            memset(dp,-9,sizeof(dp));            dp[0] = 0;            for(j = 0;j < k;j ++)            {                int cost,value;                scanf("%d%d",&cost,&value);                Zero_One(cost,value,vb);            }            int n = 0;            for(j = 1; j <= V-vb;j ++)                if(dp[j]>0)                {                    G[m][n].cost = j+vb;                    G[m][n++].value = dp[j];                }            loop[m] = n;            m++;        }        //display(m-1);        Group_Package(m-1);    }}

Idea 2: Check the code yourself ~~~

#include<stdio.h>#include<stdlib.h>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int Max = 1000000 +10;int dp[Max],N,V;int d[Max];void Zero_One(int cost , int value , int vb){    int i;    for(i = V;i >= vb+cost;i --)    {        dp[i] = max(dp[i] , dp[i-cost]+value);    }}int main(){    int i,j;    while(~scanf("%d%d",&N,&V))    {        int vb,k;        memset(d,0,sizeof(d));        for(i = 0;i < N;i ++)        {            scanf("%d%d",&vb,&k);            for(j = 0;j <= V-vb;j ++)                dp[j+vb] = d[j];            for(j = 0;j < k;j ++)            {                int cost,value;                scanf("%d%d",&cost,&value);                Zero_One(cost,value,vb);            }            for(j = vb;j <= V;j ++)                d[j] = max(d[j],dp[j]);        }        printf("%d\n",d[V]);    }}

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.