HDOJ-2955 Robberies[01背包问题-DP]

抢劫问题的01背包算法
本文介绍了一种特殊的01背包问题应用案例——银行抢劫问题。通过将银行抢劫问题抽象为01背包问题,利用动态规划求解在限定被抓概率下能抢夺的最大金额。文章提供了详细的算法思路及实现代码。

转自:http://blog.youkuaiyun.com/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物品个数NRobberies
63
物品大小mj[i]物品价值(逃跑率rj[i])编号DP6543210
10.981 0000001
20.9720000000.981
30.95310000.98*0.971*0.970.981
   20.98*0.97*0.951*0.97*0.951*0.98*0.951*0.970.980.981

转载于:https://www.cnblogs.com/XBWer/archive/2012/07/18/2597661.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值