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 pi1, pi2, pi3, pi4. You can trust that pi1+pi2+pi3=1(pi1, pi2 and pi3 have been described in the problem "Probability I". 0 ≤ pi1, pi2, pi3, pi4≤ 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);
}
}