HDU(2955)Robberies (0-1背包)

/*

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

    题意:Roy想要抢劫银行,每家银行多有一定的金额和被抓到的概率,知道Roy被抓的最

       大概率P,求Roy在被抓的情况下,抢劫最多。

    分析:考虑其反面。被抓概率可以转换成安全概率,Roy的安全概率大于1-P时都是安全的。抢劫的金

       额为0时,肯定是安全的,所以d[0]=1;其他金额初始为最危险的所以概率全为0

    注意:不要误以为精度只有两位。

 */

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<string>
#include<algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int maxn = 105;
double dp[maxn*maxn];
int weight[maxn*maxn];
double p[maxn];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        double m;
        int n;
        scanf("%lf%d",&m,&n);
        m = 1-m;
        int sum_w = 0;
        for(int i = 0;i < n;i++){
            scanf("%d%lf",weight+i,p+i);
            p[i] = 1 - p[i];
            sum_w += weight[i];
        }
        memset(dp, 0, sizeof(dp));
        dp[0] = 1;
        for(int i = 0;i < n;i++)
            for(int j = sum_w;j >= weight[i];j--)
                dp[j] = max(dp[j],dp[j-weight[i]]*p[i]);
        for(int i = sum_w;i >= 0;i--)
            if(dp[i] - m > 1e-8){
                printf("%d\n",i);
                break;
            }
    }
    return 0;
}

 

 

posted @ 2013-06-02 14:28  Roly Yu  阅读(140)  评论(0编辑  收藏  举报