HDU 2955 Roberies 背包问题

来源:互联网 发布:黑马程序员上课时间表 编辑:程序博客网 时间:2024/06/05 02:46

疑问点:如何转化成0-1背包问题,有点不好理解,先打个大大的问号???

上限概率转化为最小的安全概率即p=1-p; 被活捉的概率为pj,安全概率即为pj=1-pj;  定义的容量为M=sumv,对应的钱数为M1,M2,M3...,求最大的安全概率,且要求安全概率大于p;

#include <iostream>#include <stdio.h>#include <algorithm>#include <math.h>#define  MAXN 101#define  MAXV 10001using namespace std; int cost[MAXV]; double weight[MAXV],d[MAXV];int main(){    int t,n,sumv;    scanf("%d",&t);    double  p;    while(t--)    {        sumv=0;        scanf("%lf%d",&p,&n);        p=1-p;        for(int i=0;i<n;i++)        {           scanf("%d%lf",&cost[i],&weight[i]);           weight[i]=1-weight[i];           sumv+=cost[i];        }        for(int i=0;i<=sumv;i++)          d[i]=0;        d[0]=1;        for(int i=0;i<n;i++)        {          for(int j=sumv;j>=cost[i];j--)          {            d[j]=max(d[j],d[j-cost[i]]*weight[i]);           }        }        //bool flag=false;        for(int i=sumv;i>=0;i--)        {            if(d[i]-p>0.0000000001)            {              printf("%d\n",i);              break;            }        }    }    return 0;}


0 0
原创粉丝点击