HDU-2955-Robberies(01背包)

来源:互联网 发布:淘宝最优类目查询 编辑:程序博客网 时间:2024/06/03 17:29

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955

Robberies

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
30.04 31 0.022 0.033 0.050.06 32 0.032 0.033 0.050.10 31 0.032 0.023 0.05
 
Sample Output
246
 

题目分析:给出被抓概率P和M家银行,M家银行各自的钱数m[i]和各自被抓的概率p[i],求不被抓的情况下抢的最大钱数。被抓概率为pi,逃跑概率ri=1-pi。盗取多家(i家)银行概率逃跑概率为r1*r2*r3...ri。

01背包变形:银行里的钱数为背包总容量(银行数一定,钱数有限),每一家银行的钱为cost[i],不被抓到即逃跑概率为value[i],状态转移方程为dp[j]=max(dp[j],dp[j-m[i]]*p[i])。

 代码如下:

#include <iostream>#include<algorithm>#include <cstring>using namespace std;//数组要开大一点 double p[11111],dp[11111];int m[11111];int main(){double P;int T,M,i,j;cin>>T;while(T--){int sum=0;cin>>P>>M;P=1-P;    //令它转化为逃跑的概率for(i=0;i<M;i++){cin>>m[i]>>p[i];p[i]=1-p[i];//逃跑的概率sum+=m[i];//可以抢到的最大钱数}memset(dp,0,sizeof(dp));dp[0]=1.0;//表示抢钱为0的时候,逃跑的概率为1for(i=0;i<M; i++){for(j=sum;j>=m[i];j--){dp[j]=max(dp[j],dp[j-m[i]]*p[i]);}}for(i=sum;i>=0;i--)  //从最大的钱数开始,依次验证逃跑概率是否比给定的大 {if(dp[i]>P){cout<<i<<endl;//输出最大钱数 break;}} }return 0;}    



原创粉丝点击