您的位置:首页 > 其它

HDU 1864 —— 最大报销额 01背包

2015-12-15 10:19 513 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1864

注意点:1、将小数*100(均只有两位小数);2、数组大小不要开错;3、单项物品指的是A项、B项、C项,所以是每张发票所有的A钱数不能超过600;4、注意输入;

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxv = 3000000+10;
int dp[maxv];
float q;
int n;
int a[35];

int main()
{
while(~scanf("%f%d", &q, &n))
{
if(n == 0)	break;
int cnt = 0;
memset(a, 0, sizeof a);
memset(dp, 0, sizeof dp);
for(int i = 1;i<=n;i++)
{
bool flag = true;
int x;
scanf("%d", &x);
double suma = 0, sumb = 0, sumc = 0;
while(x--)
{
getchar();
char c;
float k;
scanf("%c:%f", &c, &k);
if(c == 'A')	suma += k;
else if(c == 'B')	sumb += k;
else if(c == 'C')	sumc += k;
else	flag = false;

}
if(flag && suma <= 600 && sumb <=600 && sumc <= 600)
a[cnt++] = (suma+sumb+sumc)*100*1.0;
}
int sum = q*100*1.0;
for(int i = 0;i<cnt;i++)
{
for(int j = sum;j>=a[i];j--)
dp[j] = max(dp[j], dp[j-a[i]]+a[i]);
}
printf("%.2f\n", (float)dp[sum]/100);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: