hdu 1864 01背包

来源:互联网 发布:诺基亚n8软件安装 编辑:程序博客网 时间:2024/06/06 06:30

分析:因为报销的额度是小数,所以把总共符合要求的发票数作为背包容量,报销金额作为价值,最后再扫一遍,找到在给定报销金额范围内最大的金额:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;double d[30009];double w[39];int main(){    double Q;    int n,m;    char c,l;    double t;   // freopen("f.txt","r",stdin);    while(~scanf("%lf %d",&Q,&n)&&n){        int cnt=0;        for(int i=0;i<n;i++){            scanf("%d",&m);            double a=0,b=0,C=0;            bool flag=true;            for(int j=0;j<m;j++){                getchar();                scanf("%c%c%lf",&c,&l,&t);                //cout<<                if(c=='A')a+=t;                else if(c=='B')b+=t;                else if(c=='C')C+=t;                else{flag=false;break;}            }            if(flag&&a<=600&&b<=600&&C<=600&&a+b+C<=1000){                w[cnt++]=a+b+C;            }        }        memset(d,0,sizeof(d));        for(int i=0;i<cnt;i++){            for(int j=cnt;j>=1;j--){                d[j]=max(d[j],d[j-1]+w[i]);            }        }        for(int j=cnt;j>=0;j--){            if(d[j]<=Q){                printf("%.2lf\n",d[j]);                break;            }        }    } }


0 0