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. Qt学习一门:直接使用QT具

    今天,通过直接使用QT一些工具来编写命令行程序.你可以看到一种Qt更一般的用法. 内容很easy,输出电流日期. 首先,用一个QDate分类,可以使用QDate类的静态方法currentDate为了得 ...

  2. freemarker 里 ?? 和 ? 都是什么意思

    ??是推断对象是否为空,比如:<#if object??>object对象不为空(即object存在)</#if> ?后面要加keyword,比如:<#if object ...

  3. 【原创】poj ----- 1611 The Suspects 解题报告

    题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS   Memory Limit: 20000K To ...

  4. #Windows Phone:在HTML5专案中,如何从Javascript传送字串到C#的APP端

    原文:#Windows Phone:在HTML5专案中,如何从Javascript传送字串到C#的APP端 #Windows Phone:在HTML5专案中,如何从Javascript传送字串到C#的 ...

  5. 【原创】shadowebdict开发日记:基于linux的简明英汉字典(一)

    全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...

  6. 【Android先进】查看手机记忆库状态和应用方法

    一世 我们知道.android程序存储器通常被限制16M.当然,24M的,和android程序存储器分为2部分:native和dalvik.dalvik 就是我们寻常说的java堆.我们创建的对象是在 ...

  7. Codeforces Round #258 (Div. 2) B. Sort the Array

    题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...

  8. 最近做RTSP流媒体的实时广播节目

    //h264视频流打包代码 // NALDecoder.cpp : Defines the entry point for the console application. #include < ...

  9. Greenplum+Hadoop学习笔记-14-定义数据库对象之创建与管理模式

    6.3.创建与管理模式 概述:DB内组织对象的一种逻辑结构.一个DB内能够有多个模式.在未指定模式时默认放置在public中.能够通过"\dn"方式查看数据库中现有模式: test ...

  10. OWIN 为WebAPI

    OWIN 为WebAPI 宿主 跨平台 OWIN是什么? OWIN的英文全称是Open Web Interface for .NET. 如果仅从名称上解析,可以得出这样的信息:OWIN是针对.NET平 ...