Passage

Problem Description
Bill is a millionaire. But unfortunately he was trapped in a castle. There are only n passages to go out. For any passage i (1<=i<=n), Pi (0<=Pi<=1) denotes the probability that Bill will escape from this castle safely if he chose this passage. Qi (0<=Qi<=1-Pi)
denotes the probability that there is a group of guards in this passage. And Bill should give them one million dollars and go back. Otherwise, he will be killed. The probability of this passage had a dead end is 1-Pi-Qi. In this case Bill has to go back. Whenever
he came back, he can choose another passage.

We already know that Bill has M million dollars. Help Bill to find out the probability that he can escape from this castle if he chose the optimal strategy.
 
Input
The first line contains an integer T (T<=100) indicating the number of test cases.

The first line of each test case contains two integers n (1<=n<=1000) and M (0<=M<=10).

Then n lines follows, each line contains two float number Pi and Qi.
 
Output
For each test case, print the case number and the answer in a single line.

The answer should be rounded to five digits after the decimal point.

Follow the format of the sample output.
 
Sample Input
3
1 10
0.5 0
2 0
0.3 0.4
0.4 0.5
3 0
0.333 0.234
0.353 0.453
0.342 0.532
 
Sample Output
Case 1: 0.50000
Case 2: 0.43000
Case 3: 0.51458
 
Source
 

题目大意:

T组測试数据,一个人困在了城堡中,有n个通道,m百万money ,每一个通道能直接逃出去的概率为 P[i] ,遇到士兵的概率为 q[i],遇到士兵得给1百万money,否则会被杀掉,还有 1-p[i]-q[i] 的概率走不通,要回头。问在能够选择的情况下,逃出去的概率是多少?

解题思路:

首先,n个通道要选择哪个先走哪个后走呢?假设暴力是2^n,显然不可取。仅仅须要贪心,选择逃生概率最大的通道,也就是 p[i]/q[i]最大的优先。

用 dp[i][j]记录 还剩j次机会,已经走到第i个通道能逃生的概率。

那么当前:

(1)遇到士兵,dp[i+1][j-1]+=dp[i][j]*q[i]

(2)走不通,dp[i+1][j]+=dp[i][j]*( 1-p[i]-q[i] )

(3)直接逃生,答案加上这个概率。

解题代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn=1100; struct route{
double p,q;
friend bool operator < (route a,route b){
return a.p/a.q>b.p/b.q;
}
}r[maxn]; int n,m;
double dp[maxn][20]; void input(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%lf%lf",&r[i].p,&r[i].q);
}
sort(r,r+n);
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
dp[i][j]=0;
}
}
} double solve(){
double ans=0;
dp[0][m]=1;
for(int i=0;i<n;i++){
for(int j=m;j>=0;j--){
ans+=dp[i][j]*r[i].p;
if(j-1>=0) dp[i+1][j-1]+=dp[i][j]*r[i].q;
dp[i+1][j]+=dp[i][j]*(1.0-r[i].p-r[i].q);
}
}
return ans;
} int main(){
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++){
input();
printf("Case %d: %.5lf\n",i,solve());
}
return 0;
}

HDU 3366 Passage (概率DP)的更多相关文章

  1. HDU 3366 Passage (概率DP)

    题意:T组测试数据,一个人困在了城堡中,有n个通道,m百万money ,每个通道能直接逃出去的概率为 P[i] ,遇到士兵的概率为 q[i], 遇到士兵得给1百万money,否则会被杀掉,还有 1-p ...

  2. 2014 Super Training #1 F Passage 概率DP

    原题: HDU 3366   http://acm.hdu.edu.cn/showproblem.php?pid=3366 本来用贪心去做,怎么都WA,后来看网上原来是一个DP题. 首先按P/Q来做排 ...

  3. HDU 3853LOOPS(简单概率DP)

    HDU 3853    LOOPS 题目大意是说人现在在1,1,需要走到N,N,每次有p1的可能在元位置不变,p2的可能走到右边一格,有p3的可能走到下面一格,问从起点走到终点的期望值 这是弱菜做的第 ...

  4. HDU - 1099 - Lottery - 概率dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1099 最最简单的概率dp,完全是等概率转移. 设dp[i]为已有i张票,还需要抽几次才能集齐的期望. 那么dp[ ...

  5. HDU 4405 【概率dp】

    题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...

  6. HDU 4576 Robot(概率dp)

    题目 /*********************复制来的大致题意********************** 有N个数字,M个操作, 区间L, R. 然后问经过M个操作后落在[L, R]的概率. * ...

  7. HDU 4599 Dice (概率DP+数学+快速幂)

    题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n ...

  8. [HDU 4089]Activation[概率DP]

    题意: 有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有以下情况: 1.激活失败,留在队列中等待下一次激活(概率为p1) 2.失去连接,出队列,然后排在队列的最后( ...

  9. hdu 3853 LOOPS 概率DP

    简单的概率DP入门题 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...

  10. HDU 3853 期望概率DP

    期望概率DP简单题 从[1,1]点走到[r,c]点,每走一步的代价为2 给出每一个点走相邻位置的概率,共3中方向,不动: [x,y]->[x][y]=p[x][y][0] ,  右移:[x][y ...

随机推荐

  1. Android定义自己的面板共享系统

    在Android分享知道有一个更方便的方法.调用的共享面板来分享我们的应用程序的系统.主要实现例如,下面的: public Intent getShareIntent(){ Intent intent ...

  2. android贴士Toast

    转载请注明出处:http://blog.csdn.net/droyon/article/details/42009015 我们可以用androd提供toast控制,但在使用过程中,给我们发了很多Toa ...

  3. UVA 674 (入门DP, 14.07.09)

     Coin Change  Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We ...

  4. Spring它不支持依赖注入static静态变量

    在springframework在,我们不能@Autowired静态变量,制作spring bean,例如,没有那么: @Autowired private static YourClass your ...

  5. struts2在&lt;s:select&gt;用动态标签

    后台传过来的必要性userlist成为一个下拉菜单.因此,认为使用<s:select>.但设置了很久设置的属性,在这个下跌. JSP代码: <s:select label=" ...

  6. ImportError with IronPython in C#

    I was using IronPython to execute python code inside my C# implementation lately, and I encountered ...

  7. ubuntu12.04硬盘安装

    ubuntu12.04发布了 , 安装又是一个话题.安装系统有很多方法,比如livecd,和u盘,但这些都需借用外部设备,所以硬盘安装是最好不过的方法了.u盘,cd安装都非常的简 单,对于那些讨厌用光 ...

  8. C#读取excel等表格常用方法

    0. 利用NPOI. 请查阅此插件的相关文档. 1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: 1 2 3 4 5 6 7 8 9 10 ...

  9. CentOS 6.3 安装 samba 共享(转)

    PHP环境在linux下,但是开发的时候用的是windows,于是我用了samba将linux的一个目录共享,然后在windows上做映射,这样就可以直接在windows下编辑linux上的文件了 首 ...

  10. Spring的文件上传

    Spring在发现包括multipart的请求后,会使用MultipartResolver的实现bean处理文件上传操作,现有採用Servlet3的 org.springframework.web.m ...