HLG 1053 Warcraft III 完全背包

Warcraft III
Time Limit: 10000 MS Memory Limit: 65536 K
Total Submit: 593(198 users) Total Accepted: 263(176 users) Rating:  Special Judge: No
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

Author

dccmx

水题~

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
int a[maxn],b[maxn],f[maxn];
int loop,v,n;
int main(){
    rd(loop);
    while(loop--){
        rd2(v,n);MT(f,0);
        FOR(i,1,n)rd2(a[i],b[i]);
        FOR(i,1,n)FOR(j,b[i],v)
        f[j]=max(f[j],f[j-b[i]]+a[i]);
        printf("%d\n",f[v]);
    }
    return 0;
}


你可能感兴趣的:(C++,dp)