zoukankan      html  css  js  c++  java
  • hdu 2955 0-1背包问题

    http://acm.hdu.edu.cn/showproblem.php?pid=2955

    Problem Description
    The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable job at a university.


    For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.


    His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.
     
    Input
    The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj . 
    Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
     
    Output
    For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.

    Notes and Constraints
    0 < T <= 100
    0.0 <= P <= 1.0
    0 < N <= 100
    0 < Mj <= 100
    0.0 <= Pj <= 1.0
    A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.
     
    Sample Input
    3
    0.04 3
    1 0.02
    2 0.03
    3 0.05
    0.06 3
    2 0.03
    2 0.03
    3 0.05
    0.10 3
    1 0.03
    2 0.02
    3 0.05
     
    Sample Output
    2
    4
    6

    问题涉及概率知识,当他去抢银行时被抓的概率是每个银行被抓的概率之积,因为只要一家抓到他他就落网了,是“或”的关系。然而,逃跑的概率是“与”的关系,必须全部逃走才算逃走,否则被抓。问题将所有银行的钱作为背包,以各家银行的钱作为花费,f[i]为偷到i的钱没被抓的概率,还有初值的问题,当i为0时,他没偷不被抓的概率为1所以有以下代码成立:

    #include <stdio.h>
    #include <string.h>
    double we[105],f[100005]={0};
    
    int main()
    {
        int n,t,i,j;
        double m,k;
        int cost[105];
        scanf("%d",&n);
        while(n--)
        {
            int sum=0;
            scanf("%lf%d",&m,&t);
            memset(f,0,sizeof(f));
            f[0]=1.0;
            for(i=0;i<t;i++)
            {
                scanf("%d%lf",&cost[i],&we[i]);
                sum+=cost[i];
            }
            for(i=0;i<t;i++)
            {
                for(j=sum;j>=cost[i];j--)
                {
                   f[j]=f[j]>(f[j-cost[i]]*(1-we[i]))?f[j]:(f[j-cost[i]]*(1-we[i]));
                }
            }
            for(i=sum;i>=0;i--)
            {
                if(f[i]>=(1-m))
                    {printf("%d
    ",i);break;}
                    //else printf("fbhweu");
            }
        }
        return 0;
    }
  • 相关阅读:
    css做导航
    css和div
    表格技巧
    HTML表单
    ASP.NET MVC 中的强类型控件
    Aspose.Cells导入与导出excel
    webpack 入门
    asp.net my sqlHelper
    visual studio下用mysql数据库建EF遇到的问题及解决方法
    asp.net mvc 无刷新高效分页
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3462495.html
Copyright © 2011-2022 走看看