HDU 1203 I NEED A OFFER!(简单01背包)

来源:互联网 发布:bigbang知乎 编辑:程序博客网 时间:2024/04/28 21:36

题目链接:Click here~~

题意:

中文题不解释。至少收到一份的概率 = 1 - 1份都收不到的概率 = 1 - Pk1*Pk2*……Pki。(ki表示他申请的学校)

解题思路:

由题意可知,我们需要找到最小的Pk1*Pk2*Pki。

联系到01背包问题,我们把钱数看做费用,概率看做价值。

则状态转移方程应该是 dp[i]=min(dp[i],dp[i-c]*w),初始设所有的dp[i] = 1。

#include <stdio.h>#define min(a,b) a<b ? a : b#define N 10005double dp[N];int main(){    int V,n,c;    double w;    while(~scanf("%d%d",&V,&n),V||n)    {        for(int i=0;i<=V;i++)            dp[i] = 1;        while(n--)        {            scanf("%d%lf",&c,&w);            w = 1-w;            for(int i=V;i>=c;i--)                dp[i] = min(dp[i],dp[i-c]*w);        }        printf("%.1f%%\n",(1-dp[V])*100);    }return 0;}


原创粉丝点击