HLG 1053 Warcraft III 完全背包
Description
dccmx likes playing Warcraft III. Now, he is teaching his girlfriend to play it. In Warcraft III, there are many kinds of units. Every unit costs some gold and lumber. Different units have different attack value.
Now question comes. Given some amount of gold and a list of types of units, how to arrange your units to maximize the attack value of your units. Assume you have infinite lumbers.
Input
Line 1 contains an integer T: the number of test cases.
Next T blocks, each starts with two integers: G and U, represents the amount of gold and number of unit type. Next U lines, each contains two integers: the attack value of a type of unit and the cost.
Output
For each test case, output the maximum total attack value in one line.
Sample Input
2
100 1
20 10
300 4
100 60
250 120
120 100
35 20
Sample Output
200
605
code:
#include<stdio.h>
#include<string.h>
#define max(a,b)(a)>(b)?(a):(b)
int a[100000];
int b[100000];
int f[100000];
int main()
{
int i,m,v,k,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&v,&m);
memset(f,0,sizeof(f));
for(i=0;i<m;i++)
scanf("%d%d",&a[i],&b[i]);
for(i=0;i<m;i++)
for(j=b[i];j<=v;j++)
f[j]=max(f[j],f[j-b[i]]+a[i]);
printf("%d\n",f[v]);
}
return 0;
}