您的位置:首页 > 其它

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

2012-07-18 16:30 387 查看

转自: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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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 .

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

2 4 6

[align=left]Source[/align]
IDI Open 2009

[align=left]Recommend[/align]
gaojie

code:

/*
将小偷计划要偷的钱的总数作为背包的容量,然后每个银行的存款就作为各个物品的重量,
每个银行小偷的逃跑率就作为每个物品的价值,这样就转化为01背包问题了。
至于为什么不可以用题目给的被抓获的概率作为价值,是因为小偷被抓与否的计算方法,
不是将每个银行小偷被抓的概率相乘,概率论的基本知识,所以要以逃跑率作为价值。
定义数组 F[j]为偷到j万元的时候,逃跑的概率,那么状态方程如下:
F[j]=max{F[j],F[j-m[i]]*g[i]},其中m[i]是第i个银行的存款,g[i]是在该银行偷窃后逃跑的概率
这个状态方法和O1背包的状态方程是一个思想的。
*/
#include<iostream>
using namespace std;

int main()
{
int T;
double p;             //逃跑率
int n;
int mj[102];          //银行的钱
double rj[102];       //不被抓的概率
int i,j;
double temp;
double dp[10002];      //问题的最大可能规模为:100*100
int sum;               //所有银行钱的总数
while(~scanf("%d",&T))
{
while(T--)
{
sum=0;
memset(dp,0,sizeof(dp));
dp[0]=1;          //没偷钱,100%不被抓
scanf("%lf%d",&p,&n);
for(i=0;i<n;i++)
{
scanf("%d%lf",&mj[i],&temp);
rj[i]=1-temp;
sum+=mj[i];
}
//核心代码,DP所在
for(i=0;i<n;i++)
for(j=sum;j>=mj[i];j--)
{
if(dp[j-mj[i]]*rj[i]>dp[j])
dp[j]=dp[j-mj[i]]*rj[i];
}
//从后面找起,最大的一个逃跑率大于给定的逃跑率的就是答案
p=1-p;
for(i=sum;i>=0;i--)
if(dp[i]>=p)
break;
printf("%d\n",i);
}
}
return 0;
}


自己调试:

最大容量sum物品个数NRobberies
63
物品大小mj[i]物品价值(逃跑率rj[i])编号DP6543210
10.9810000001
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: