[TOJ 3978] Probability II

3978.   Probability II

Before read this problem, you should read the problem "Probability I" to get some background information.

In fact, Bob is far more intelligent than we have imagined. He will vindicate to Alice at any point which means he may say to Alice that he love her directly. Bob knows that if he do this at the point i, he can success with the probabilitypi4. But if he fails, he must be back to the point 1. And at the point n he don’t need Vindicate to Alice. Bob is not brave enough as ZHENGWEI, so he decides that he will vindicate at most m times. If he all failed at the m times, he won’t vindicate again which means he will then try his best to show step by step until he reaches the point n. Now we want to know how many expect times Bob have to show using his best strategy.(When Bob directly say to Alice, it is also once show).

Input

There will be several test cases, and each test case input as follows:
The first line input n(1≤n≤1000), m(1≤m≤1000). Then there will be n-1 lines following. Each of the following lines contains four real number pi1pi2pi3pi4. You can trust that pi1+pi2+pi3=1(pi1pi2 and pi3 have been described in the problem "Probability I". 0 ≤ pi1pi2pi3pi4≤ 1, pi3=0 at point n-1).

Output

For each test case, please output the expect times that Bob has to show to Alice. Answers must be rounded to one digits after the decimal point. If he can’t reach at the point n, please output "impossible".

Sample Input

2 1

0.3 0.7 0.0 1.0

3 1

0.2 0.3 0.5 0.2

0.3 0.7 0.0 1.0

Sample Output

1.0

1.6


dp再加上一些概率上的问题 先推公式,然后每次可以有两种选择, 从后向前递推求解。


#include<iostream>
#include<cstring>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
int n,m;
const double inf=101010101010.0;
double dp[1200][1200];
double p[1200][4];
int main()
{
    while(cin>>n>>m)
    {
        memset(dp,0,sizeof(dp));
        bool flag=1;
        for(int i=1;i<=n-1;i++)
            cin>>p[i][0]>>p[i][1]>>p[i][2]>>p[i][3];
        for(int j=m;j>=0;j--)
        for(int i=n-1;i>=1;i--)
                if(j==m)
                    dp[i][j]=(dp[i+1][j]*p[i][1]+dp[i+2][j]*p[i][2]+1)/(1-p[i][0]);
                else
                    dp[i][j]=min((dp[i+1][j]*p[i][1]+dp[i+2][j]*p[i][2]+1)/(1-p[i][0]),dp[1][j+1]*(1-p[i][3])+1);
        double ans=inf;
        for(int i=0;i<=m;i++)
            ans=min(ans,dp[1][i]);
        if(ans>101010100)
            puts("impossible");
        else
            printf("%.1f\n",ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值