HDU 3366 Passage (概率DP)
Passage
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.
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.
The answer should be rounded to five digits after the decimal point.
Follow the format of the sample output.
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
Case 1: 0.50000
Case 2: 0.43000
Case 3: 0.51458
题目大意:
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)的更多相关文章
- HDU 3366 Passage (概率DP)
题意:T组测试数据,一个人困在了城堡中,有n个通道,m百万money ,每个通道能直接逃出去的概率为 P[i] ,遇到士兵的概率为 q[i], 遇到士兵得给1百万money,否则会被杀掉,还有 1-p ...
- 2014 Super Training #1 F Passage 概率DP
原题: HDU 3366 http://acm.hdu.edu.cn/showproblem.php?pid=3366 本来用贪心去做,怎么都WA,后来看网上原来是一个DP题. 首先按P/Q来做排 ...
- HDU 3853LOOPS(简单概率DP)
HDU 3853 LOOPS 题目大意是说人现在在1,1,需要走到N,N,每次有p1的可能在元位置不变,p2的可能走到右边一格,有p3的可能走到下面一格,问从起点走到终点的期望值 这是弱菜做的第 ...
- HDU - 1099 - Lottery - 概率dp
http://acm.hdu.edu.cn/showproblem.php?pid=1099 最最简单的概率dp,完全是等概率转移. 设dp[i]为已有i张票,还需要抽几次才能集齐的期望. 那么dp[ ...
- HDU 4405 【概率dp】
题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...
- HDU 4576 Robot(概率dp)
题目 /*********************复制来的大致题意********************** 有N个数字,M个操作, 区间L, R. 然后问经过M个操作后落在[L, R]的概率. * ...
- HDU 4599 Dice (概率DP+数学+快速幂)
题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n ...
- [HDU 4089]Activation[概率DP]
题意: 有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有以下情况: 1.激活失败,留在队列中等待下一次激活(概率为p1) 2.失去连接,出队列,然后排在队列的最后( ...
- hdu 3853 LOOPS 概率DP
简单的概率DP入门题 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...
- HDU 3853 期望概率DP
期望概率DP简单题 从[1,1]点走到[r,c]点,每走一步的代价为2 给出每一个点走相邻位置的概率,共3中方向,不动: [x,y]->[x][y]=p[x][y][0] , 右移:[x][y ...
随机推荐
- 1104. Don’t Ask Woman about Her Age(数论)
a*b^n(mod(b-1) =a(mod(b-1) http://acm.timus.ru/problem.aspx?space=1&num=1104 #include <stdio. ...
- 如何判断一个Http Message的结束——python源码解读
HTTP/1.1 默认的连接方式是长连接,不能通过简单的TCP连接关闭判断HttpMessage的结束. 以下是几种判断HttpMessage结束的方式: 1. HTTP协议约定status ...
- 玩转Web值jquery(一)---对表单中的某一标签批量处理(以input为例)
jquery可以对form进行操作,以批量操作某一标签,这里以input标签为例总结. 示例一:对删除infoForm表单的input的所有readonly属性 $("#infoForm i ...
- Word文件交换的电脑打开字体、排版变化的原因和解决方法!
方案: 有时候.我们好不easy用Word写好文档,做好排版发给别人,别人会告诉你格式怎么是乱的啊,标题.正文.页眉页脚什么的格式都变了. 想尽各种办法都没能得到解决,那么出现这样的情况的原因究竟是什 ...
- 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)
题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- HDU 1885 Key Task 国家压缩+搜索
点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Android菜鸟的成长笔记(17)—— 再看Android中的Unbounded Service
原文:Android菜鸟的成长笔记(17)-- 再看Android中的Unbounded Service 前面已经写过关于startService(Unbounded Service)的一篇文章:&l ...
- Myeclipse它显示了一个目录的结构,而不是包
今天Myeclipse新project,编写代码,查找workspace空间展示project在包装和class所有平行结构,看的很不顺,有两个原因,第一,您可能无法切换到Package worksp ...
- hdu 2767 Proving Equivalences 强连通缩点
给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...