POJ 2392 Space Elevator(多重背包)

http://poj.org/problem?id=2392

 

Description

The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h_i (1 <= h_i <= 100) and is available in quantity c_i (1 <= c_i <= 10). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i (1 <= a_i <= 40000). 

Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.

Input

* Line 1: A single integer, K 

* Lines 2..K+1: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+1 describes block type i.

Output

* Line 1: A single integer H, the maximum height of a tower that can be built

Sample Input

3

7 40 3

5 23 8

2 52 6

Sample Output

48


题意:有一头奶牛要上太空,他有很多种石头,每种石头的高度是hi,
但是不能放到ai之上的高度,并且这种石头有ci个
将这些石头叠加起来,问能够达到的最高高度。




把多重背包转化为01背包
#include<cstdio>//把多重背包转化为01背包

#include<iostream>

#include<cstring>

#include<algorithm>

using namespace std;

struct DP

{

    int h,a,c;

};

DP num[400+10];

int dp[40000+100];

int cmp(DP x,DP y)

{

   return  x.a<y.a;

}

int main()

{

    int n,i,j,k;

    while(scanf("%d",&n)!=EOF)

    {

        memset(dp,0,sizeof(dp));

        dp[0]=1;

        for(i=1;i<=n;i++)

        {

            scanf("%d%d%d",&num[i].h,&num[i].a,&num[i].c);

        }

        sort(num+1,num+n+1,cmp);



        for(i=1;i<=n;i++)

        {

            for(j=1;j<=num[i].c;j++)

            {

                for(k=num[i].a;k>=0;k--)

                {

                    if(dp[k]==1&&k+num[i].h<=num[i].a)

                    {

                        dp[k+num[i].h]=1;

                    }

                }

            }

        }

        for(i=40000;i>=0;i--)

        {

            if(dp[i]==1) break;

        }

        printf("%d\n",i);

    }

    return 0;

}

  

 

直接使用多重背包

#include <cstring>

#include <cmath>

#include <cstdio>

#include <cstdlib>

#include <algorithm>

#include <queue>

#include <iostream>

using namespace std;

int n,dp[45000];

struct node

{

    int a,b,c;

}e[500];

bool cmp(node a, node b)

{

    return a.b < b.b;

}

int main()

{

    int i,j,k;

    while(scanf("%d",&n)!=EOF)

    {

        for(i = 0; i < n ; i++)

        {

            scanf("%d %d %d",&e[i].a,&e[i].b,&e[i].c);

        }

        memset(dp, -1 , sizeof(dp));

        sort(e,e + n, cmp);

        dp[0] = 1;

        for(i = 0; i < n; i++)

        {

            int sum = 1;

            int q;

            for( k = 1; k<=e[i].c; k *= 2)

            {

                q = k * e[i].a;

                for(j = e[i].b; j >=0; j --)

                {

                    if(dp[j] != -1 && j + q <= e[i].b)

                    {

                        dp[j + q] = 1;

                    }

                }

                e[i].c -= k;

            }

            q = e[i].c * e[i].a;

            for(j = e[i].b; j >=0; j --)

            {

                if(dp[j] != -1 && j + q <= e[i].b)

                {

                    dp[j + q] = 1;

                }

            }

        }

        for(i = 40000; i >= 0; i--)

        {

            if(dp[i] == 1)

                break;

        }

        cout<<i<<endl;

    }

    return 0;

}

  

你可能感兴趣的:(poj)