HUNNU11405:Fundraised(完全背包)

Guess what?! Dalia won the money (an amount X), and now it’s time to spend it. Mohamed Fouad is
the deputy regional contest director and he wants to spend the money but he doesn’t know what to
spend it on. There are N items that he can buy like name-tags, T-shirts, helium balloons, trophies,
etc. each having a different importance value and a cost to buy one unit from (you can buy as much
units of any item as you want as long as you don't exceed the budget).
Mohamed wants your help; tell him the  maximum importance value he can  get after he chooses
optimally to buy any number of items without exceeding his budget.
Input Specification
The first line of input contains an integer T, the number of test cases. T test cases follow, the first
line of each test case contains two integers (1 <= N <= 100), (1<= X <= 10000). The second line of
the test case contains  N space separated integers  Ii (1<=Ii<=400000), each representing the
importance value Fouad earns from item  i  (0 <=  i <  N). The following line  contains  N space
separated integers Ci (1<= Ci <=1000), each representing the cost of an item i (0 <= i < N).
Output Specification
For each test case, on a separate line, output the maximum importance value Fouad can get.
Sample Input
1
5 20
1 2 3 4 5
2 6 3 5 4
Sample Output
25

 

最裸的完全背包题,直接模板即可

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

__int64 dp[10005];
__int64 val[105],wei[105];

int main()
{
    __int64 t,i,j;
    scanf("%I64d",&t);
    while(t--)
    {
        __int64 n,x;
        scanf("%I64d%I64d",&n,&x);
        for(i = 1;i<=n;i++)
        scanf("%I64d",&val[i]);
        for(i = 1;i<=n;i++)
        scanf("%I64d",&wei[i]);
        memset(dp,0,sizeof(dp));
        for(i = 1;i<=n;i++)
        {
            for(j = wei[i];j<=x;j++)
            {
                dp[j] = max(dp[j],dp[j-wei[i]]+val[i]);
            }
        }
        printf("%I64d\n",dp[x]);
    }

    return 0;
}


 

你可能感兴趣的:(背包,hunnu)