01 The deformation of the backpack

Link: https://www.nowcoder.com/acm/contest/119/F
Source: Niuke.com

Beautiful Land
Time limit: C/C++ 1 second, other languages ​​2 seconds
Space limit: C/C++ 131072K, other languages ​​262144K
64bit IO Format: %lld

Topic description

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
Now HUST got a big land whose capacity is C to plant trees. We have n trees which could be plant in it. Each of the trees makes HUST beautiful which determined by the value of the tree. Also each of the trees have an area cost, it means we need to cost c i area of land to plant.
We know the cost and the value of all the trees. Now HUSTers want to maximize the value of trees which are planted in the land. Can you help them?

Enter description:

There are multiple cases.
The first line is an integer T(T≤10), which is the number of test cases.
For each test case, the first line is two number n(1≤n≤100) and C(1≤C≤10
8
), the number of seeds and the capacity of the land. 
Then next n lines, each line contains two integer c
i
(1≤c
i
≤10
6
) and v
i
(1≤v
i
≤100), the space cost and the value of the i-th tree.

Output description:

For each case, output one integer which means the max value of the trees that can be plant in the land.
Example 1

enter

1
3 10
5 10
5 10
4 12

output

22
#include <cstdio> value backpack
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define max(a,b)(a>b?a:b)
#define min(a,b)(a<b?a:b)
typedef long long ll;
using namespace std;
#define N 10005
ll dp[N],w[N]; ///The dp[i] at this time means yes; the minimum capacity when the value is i is dp[i];
int v[N];
 
intmain()
{
    int T,i,n,sum;
    ll V;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%I64d",&n,&V);
        sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%I64d%d",&w[i],&v[i]);
            sum = sum + v [i];
        }
 
        memset(dp,1000000010,sizeof(dp)); ///The minimum capacity is required, initialized to the maximum value;
        dp[0]=0;
        for(i=1;i<=n;i++)
        {
            for(int j=sum;j>=v[i];j--)
                dp[j]=min(dp[j],dp[j-v[i]]+w[i]);
        }
 
        for(i=sum;i>=0;i--)
        {
            if(dp[i]<=V)
            {
               printf("%d\n",i); ///Output i here, which is the maximum value that satisfies the condition
               break;
            }
        }
    }
    return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325345480&siteId=291194637