HDU2191 多重背包(二进制思想)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define mxn 202
int n, v;
int p[mxn], h[mxn], c[mxn];
int dp[mxn];
int main() {
	int cas;
	scanf( "%d", &cas );
	while( cas-- ) {
		scanf( "%d%d", &v, &n );
		for( int i = 1; i <= n; ++i )
			scanf( "%d%d%d", &p[i], &h[i], &c[i] );
		memset( dp, 0, sizeof( dp ) );
		for( int i = 1; i <= n; ++i ) {
			int num = c[i];
			for( int k = 1; num > 0; k <<= 1 ) {
				int mul = min( num, k );
				for( int j = v; j >= p[i] * mul; --j )
					dp[j] = max( dp[j], dp[j - p[i] * mul] + mul * h[i] );
				num -= mul;
			}
		}
		printf( "%d\n", dp[v] );
	}
	return 0;
}

你可能感兴趣的:(HDU2191 多重背包(二进制思想))