ACM 01背包问题

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

解题思路:

题目的大意是,Roy想要抢劫银行,每家银行多有一定的金额和被抓到的概率,知道Roy被抓的最大概率P,求Roy在没有被抓的情况下,抢劫最多 的钱数。这是一个背包问题,我们只要做一点转化。把每个银行的储钱量之和当成背包容量,然后概率当成价值来求。这里是被抓的概率,我们把他转化成不被抓的概率,然后这里的和就可以转化成乘积了。然后利用01背包的模版就可以做出来了。

程序代码:

#include<cstdio>
#include <iostream>
#include<cstring>
using namespace std;
int money[101] , nkind  , sum ;
float pro[101] , npro , fine[11100];
int main ()
{
    int cas,i, j ;
    scanf ( "%d" , &cas ) ;//案例数
    while ( cas -- )
    {
          sum = 0 ;
          scanf ( "%f%d" , &npro , &nkind ) ;//最高的概率数,和银行数
          for ( i = 0 ; i < nkind ; i ++ )
          {
              scanf ( "%d%f"  , money+i, pro+i ) ;//银行钱数和被抓的概率数
              sum += money[i] ;
          }
          memset( fine , 0 , sizeof (fine) ) ;
          fine[0] = 1 ; //背包中的钱为0时,是最安全的,所以安全概率为1
          float  p = 1 - npro ;//最低安全概率
          for ( i = 0 ; i < nkind ; i ++ )//银行数
          {
              for ( j = sum ; j >= money[i] ; j -- )//
                  if ( fine[j] < fine[j-money[i]]*(1-pro[i]) )//抢到j元钱的安全概率为fine[j]
                  {
                       fine[j] = fine[j-money[i]]*(1-pro[i]) ;
                  }
          }
          for ( i = sum ; i >= 0 ; i -- )
              if ( fine[i] >=  p )//安全的概率大于等于被抓的最低概率时
              {
                   printf ( "%d\n" , i ) ;
                   break ;
              }
    }
    return 0 ;
}

  

时间: 2024-10-21 00:25:49

ACM 01背包问题的相关文章

ACM 01背包问题1

Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … The bone collector had a big bag with a volume of V ,and

从01背包问题理解动态规划---初体验

01背包问题具体例子:假设现有容量10kg的背包,另外有3个物品,分别为a1,a2,a3.物品a1重量为3kg,价值为4:物品a2重量为4kg,价值为5:物品a3重量为5kg,价值为6.将哪些物品放入背包可使得背包中的总价值最大? 这个问题有两种解法,动态规划和贪婪算法.本文仅涉及动态规划. 先不套用动态规划的具体定义,试着想,碰见这种题目,怎么解决? 首先想到的,一般是穷举法,一个一个地试,对于数目小的例子适用,如果容量增大,物品增多,这种方法就无用武之地了. 其次,可以先把价值最大的物体放入

hdu5188 加限制的01背包问题

http://acm.hdu.edu.cn/showproblem.php? pid=5188 Problem Description As one of the most powerful brushes in the world, zhx usually takes part in all kinds of contests. One day, zhx takes part in an contest. He found the contest very easy for him. Ther

HDU 2602 Bone Collector (01背包问题)

原题代号:HDU 2602 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 原题描述: Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone Collector". This man like to collect varies of bones , such as dog's , cow's ,

0-1背包问题与子集合加总问题的近似算法

最近没有怎么更新博客,因为一直比较忙.最近发现所里在做的一个项目中,可以抽出一部分内容和0-1背包问题.子集合加总问题非常相似(虽然表面上不容易看出相似点),所以看了一些这方面的资料和论文,这里主要对问题特点和算法思想做一些整理.这类问题其实很有意思,做数学和做计算机的人都会研究,而且我这里将要提到的论文都是做计算机的人所写的. 问题简述0-1 Knapsack Problem (0-1背包问题,下面简称KP)和Subset Sum Problem (子集合加总问题,下面简称SSP)是经典的NP

动态规划(5)——01背包问题(NYOJ325zb的生日)

zb的生日 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么庆祝生日,经过调查,zb发现C小加和never都很喜欢吃西瓜,而且一吃就是一堆的那种,zb立刻下定决心买了一堆西瓜.当他准备把西瓜送给C小加和never的时候,遇到了一个难题,never和C小加不在一块住,只能把西瓜分成两堆给他们,为了对每个人都公平,他想让两堆的重量之差最小.每个西瓜的重量已知,你能帮

动态规划之01背包问题(最易理解的讲解)

01背包问题,是用来介绍动态规划算法最经典的例子,网上关于01背包问题的讲解也很多,我写这篇文章力争做到用最简单的方式,最少的公式把01背包问题讲解透彻. 01背包的状态转换方程 f[i,j] = Max{ f[i-1,j-Wi]+Pi( j >= Wi ),  f[i-1,j] } f[i,j]表示在前i件物品中选择若干件放在承重为 j 的背包中,可以取得的最大价值. Pi表示第i件物品的价值. 决策:为了背包中物品总价值最大化,第 i件物品应该放入背包中吗 ? 题目描述: 有编号分别为a,b

01背包问题:POJ3624

背包问题是动态规划中的经典问题,而01背包问题是最基本的背包问题,也是最需要深刻理解的,否则何谈复杂的背包问题. POJ3624是一道纯粹的01背包问题,在此,加入新的要求:输出放入物品的方案. 我们的数组基于这样一种假设: totalN表示物品的种类,totalW表示背包的容量 w[i]表示第i件物品的重量,d[i]表示第i件物品的价值. F(i,j)表示前i件物品放入容量为j的背包中,背包内物品的最大价值. F(i,j) = max{ F(i-1,j) , F(i-1,j-w[i])+d[i

动态规划 - 0-1背包问题

0-1背包问题描述如下: 有一个容量为V的背包,和一些物品.这些物品分别有两个属性,体积w和价值v,每种物品只有一个.要求用这个背包装下价值尽可能多的物品,求该最大价值,背包可以不被装满.因为最优解中,每个物品都有两种可能的情况,即在背包中或者不存在(背 包中有0个该物品或者 1个),所以我们把这个问题称为0-1背包问题. 用dp[i][j]表示前i个物品在总体积不超过j的情况下,放到背包里的最大价值.由此可以推出状态转移方程: dp[0][j] = 0; dp[i][j] = max{dp[i