题目:
http://acm.hdu.edu.cn/showproblem.php?pid=1864
思路:
如果背包容量是小数, 可以根据答案保留的小数的位数来将容量转换成整数.
AC.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int cnt[35], dp[3000005];
double b[4], num[35], S;
int T;
void init()
{
memset(dp, 0, sizeof(dp));
memset(num, 0, sizeof(num));
memset(cnt, 0, sizeof(cnt));
}
int main()
{
//freopen("in", "r", stdin);
while(~scanf("%lf %d", &S, &T)) {
if(T == 0) break;
init();
int s = S*100, n;
double a;
char c;
for(int i = 1; i <= T; ++i) {
scanf("%d", &n);
memset(b, 0, sizeof(b));
bool ok = 0;
for(int nn = 0; nn < n; ++nn) {
scanf(" %c:%lf", &c, &a);
if(c <= 'C' && c >= 'A') b[c-'A'] += a;
else ok = 1;
}
for(int j = 0; j < 3; ++j) {
if(b[j] > 600) {
num[i] = 0;
break;
}
num[i] += b[j];
}
if(num[i] > 1000 || ok) num[i] = 0;
if(num[i] > 0) cnt[i] = num[i]*100;
}
for(int i = 1; i <= T; ++i) {
for(int j = s; j >= cnt[i]; --j) {
dp[j] = max(dp[j-cnt[i]]+cnt[i], dp[j]);
}
}
printf("%.2lf\n", dp[s]/100.0);
}
return 0;
}