[原]hdu2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 (这个只是题目名字) (多重背包)

本文出自:http://blog.csdn.net/svitter

原题:http://acm.hdu.edu.cn/showproblem.php?pid=2191

题意:多重背包问题。转换成为01背包解。多重背包转化为01背包的关键在于把件数从整体中孤立出来作为一个新的个体,也就是说不管分类,有多少件就有多少种。

AC代码:

//============================================================================
// Name : 动态规划.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
#define max(a,b) a > b ? a : b struct Rice{
int pr;
int val;
}; int f[110];
Rice r[4000]; void ace(){
//work point
int t, i , j ,k; //num
int n, m;
int p, h, c;
int num;//转换为01背包后的数量 scanf("%d", &t);
while(t--){
memset(f, 0, sizeof(f));
scanf("%d%d", &n, &m); num = 0;
for(i = 0; i < m; i++){
scanf("%d%d%d", &p, &h, &c);
for(j = num; j < num + c; j++){
r[j].pr = p;
r[j].val = h;
}
num = j;
} //背包最小额
for(i = 0; i < num; i++){
for(j = n; j >= r[i].pr; j--){
f[j] = max(f[j], f[j - r[i].pr] + r[i].val);
}
} printf("%d\n", f[n]);
}
} int main() {
ace();
return 0;
}
作者:svitter 发表于2014-5-3 9:28:44 原文链接
阅读:217 评论:0 查看评论
上一篇:[原创作品]手把手教你怎么写jQuery插件


下一篇:netty实现远程调用RPC功能