HDU5410——01背包+完全背包——CRB and His Birthday

http://acm.hdu.edu.cn/showproblem.php?pid=5410

/*
先01背包一下再完全背包一下
*/
/************************************************
* Author        :Powatr
* Created Time  :2015-8-20 19:46:35
* File Name     :1005_1.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

int a[MAXN], b[MAXN], c[MAXN];
int dp[MAXN];
int main(){
    int T;
    int n, m;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d", &m, &n);
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++)
            scanf("%d%d%d", &a[i], &b[i], &c[i]);
        for(int i = 1; i <= n; i++){
            for(int j = m; j >= a[i]; j--)
                dp[j] = max(dp[j], dp[j-a[i]] + b[i] + c[i]);
            for(int j = a[i]; j <= m; j++)
                dp[j] = max(dp[j], dp[j-a[i]] + b[i]);
        }
        printf("%d\n", dp[m]);
    }
    return 0;
}

  

posted @ 2015-08-20 19:53  Painting、时光  阅读(150)  评论(0编辑  收藏  举报