• HDOJ2955 Robberies[01背包问题DP]


    转自:http://blog.csdn.net/kay_sprint/article/details/7237521

    Robberies

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4211    Accepted Submission(s): 1566


    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
     
    Source
     
    Recommend
    gaojie
     
     
     
    code:
     1 /*
     2 将小偷计划要偷的钱的总数作为背包的容量,然后每个银行的存款就作为各个物品的重量, 
     3 每个银行小偷的逃跑率就作为每个物品的价值,这样就转化为01背包问题了。 
     4 至于为什么不可以用题目给的被抓获的概率作为价值,是因为小偷被抓与否的计算方法, 
     5 不是将每个银行小偷被抓的概率相乘,概率论的基本知识,所以要以逃跑率作为价值。 
     6 定义数组 F[j]为偷到j万元的时候,逃跑的概率,那么状态方程如下: 
     7 F[j]=max{F[j],F[j-m[i]]*g[i]},其中m[i]是第i个银行的存款,g[i]是在该银行偷窃后逃跑的概率 
     8 这个状态方法和O1背包的状态方程是一个思想的。 
     9 */
    10 #include<iostream>
    11 using namespace std;
    12 
    13 int main()
    14 {
    15     int T;
    16     double p;             //逃跑率 
    17     int n;
    18     int mj[102];          //银行的钱 
    19     double rj[102];       //不被抓的概率 
    20     int i,j;
    21     double temp; 
    22     double dp[10002];      //问题的最大可能规模为:100*100 
    23     int sum;               //所有银行钱的总数 
    24     while(~scanf("%d",&T))
    25     {
    26         while(T--)
    27         {
    28             sum=0;
    29             memset(dp,0,sizeof(dp));
    30             dp[0]=1;          //没偷钱,100%不被抓 
    31             scanf("%lf%d",&p,&n);
    32             for(i=0;i<n;i++)
    33             {
    34                 scanf("%d%lf",&mj[i],&temp);
    35                 rj[i]=1-temp;
    36                 sum+=mj[i];
    37             }    
    38             //核心代码,DP所在   
    39               for(i=0;i<n;i++)
    40                   for(j=sum;j>=mj[i];j--)
    41                   {
    42                       if(dp[j-mj[i]]*rj[i]>dp[j])
    43                           dp[j]=dp[j-mj[i]]*rj[i];
    44                 }
    45               //从后面找起,最大的一个逃跑率大于给定的逃跑率的就是答案  
    46               p=1-p; 
    47             for(i=sum;i>=0;i--)
    48               if(dp[i]>=p) 
    49                   break;
    50             printf("%d\n",i);
    51         }
    52     }
    53     return 0;
    54 }

    自己调试:

    最大容量sum 物品个数N Robberies
    6 3
    物品大小mj[i] 物品价值(逃跑率rj[i]) 编号 DP 6 5 4 3 2 1 0
    1 0.98 1   0 0 0 0 0 0 1
    2 0.97 2 0 0 0 0 0 0 0.98 1
    3 0.95 3 1 0 0 0 0.98*0.97 1*0.97 0.98 1
          2 0.98*0.97*0.95 1*0.97*0.95 1*0.98*0.95 1*0.97 0.98 0.98 1






                If you have any questions about this article, welcome to leave a message on the message board.



    Brad(Bowen) Xu
    E-Mail : maxxbw1992@gmail.com


  • 相关阅读:
    layui flow loading占位图实现方法
    layui弹出层layer的area过大被遮挡
    layui 复选框checkbox 实现全选全选
    axios 设置headers token
    elementUI vue this.$confirm 和el-dialog 弹出框 移动
    vue + axios + formdata 上传文件带参数的爬坑之路
    Java四舍五入时保留指定小数位数
    List containsKey 和Map contains 判断集合中是否包含某个值
    BigDecimal 基本使用 比较大小和加减乘除
    springMVC返回json数据乱码问
  • 原文地址:https://www.cnblogs.com/XBWer/p/2597661.html
Copyright © 2020-2023  润新知