HDU 2955 Robberies(01背包)

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
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
 
Wrong Answer
一开始按照一般的做法打出来发现编译报错,原来是忘了下标是double的了,那就把概率乘个100,变成int,然并卵。。精度并不是.2。。再废话一句,cin超时了。。。
 
Answer
参考其他人的,把银行的钱作为体积,概率作为价值,所有银行的钱作为背包容量,因为已经算了逃跑率,方程就是这样:
dp[j]=max(dp[j],dp[j-v[i].vo]*v[i].va);//v是vector的意思,不是体积。
输出的时候从dp[sum]//sum是总钱数)开始循环,遇到的第一个能逃跑的//逃跑率不大于1-p(给出的被抓率)),输出那个下标。
 
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
#define msd memset(dp,0,sizeof(dp))
using namespace std;
#define LOCAL
double dp[10010];
struct Node
{
    int vo;//
    double va;//[逃跑]概率
}v[10010];
int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("out.txt","w",stdout);
#endif // LOCAL
    //ios::sync_with_stdio(false);
    int N;
    cin>>N;
    while(N--)
    {
        double p;
        int n,sum=0;//sum是钱的总数,即背包容量
        msd,dp[0]=1;//什么都不抢,逃跑率100%
        scanf("%lf%d",&p,&n);
        for(int i=1;i<=n;i++)
        {scanf("%d%lf",&v[i].vo,&v[i].va),v[i].va=1-v[i].va;
        sum+=v[i].vo;}

        for(int i=1;i<=n;i++)
        for(int j=sum;j>=0;j--)
        dp[j]=max(dp[j],dp[j-v[i].vo]*v[i].va);

        for(int i=sum;i>=0;i--)
        {
            if(dp[i]>1-p)
            {
                printf("%d
",i);
                break;
            }
        }
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/gpsx/p/5203123.html