先对数据按照限制从小到大排序,接着根据限制进行多重背包
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 401
#define MAXV 400001
#define TOP 40001
int dp[MAXV];
typedef struct $ {
int val, cnt, limit;
}Point;
Point p[MAXN];
int cmp(const Point &a, const Point &b)
{
return a.limit < b.limit;
}
int knapsack(int max_v, int n)
{
int ans(0);
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i ++) {
for(int j = 1; j <= p[i].cnt; j ++) {
if( p[i].val*j > p[i].limit ) {
break;
}
for(int v = max_v; v >= p[i].val; v --) {
if( dp[v] < dp[v-p[i].val]+p[i].val && dp[v-p[i].val]+p[i].val <= p[i].limit) {
dp[v] = dp[v-p[i].val]+p[i].val; ans = max(ans, dp[v]);
}
}
}
}
return ans;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n, max_v;
while( ~scanf("%d", &n) ) {
max_v = 0;
for(int i = 0; i < n; i ++) {
scanf("%d %d %d", &p[i].val, &p[i].limit, &p[i].cnt);
max_v += p[i].cnt*p[i].val;
}
max_v = min(max_v, TOP);
sort(p, p+n, cmp);
printf("%d\n", knapsack(max_v, n));
}
return 0;
}
poj_2392
最新推荐文章于 2019-03-06 21:01:23 发布