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.
思路:概率dp,既然問最優可能性,肯定是最有可能跑出去的情況最好了,就算跑不出去也要少碰士兵,所以先按p/q從大到小排序,然後dp[i][j]代表在第i個通道,還有j萬元的時候能出去的概率。這個時候,能直接出去的話,就把最終結果加上dp[i][j]*p;如果碰上士兵就dp[i+1][j-1]+=dp[i][j]*q;如果是死路,就dp[i+1][j]+=dp[i][j]*(1-p-q);
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
double dp[1024][12];
struct node
{
double p,q;
bool operator <(const node &a)const
{
return p/q > a.p/a.q;
}
}num[1024];
int main()
{
int t,kase = 1;
scanf("%d",&t);
while(t--)
{
int m,n;
scanf("%d%d",&n,&m);
for(int i = 0;i < n;i++)
{
scanf("%lf%lf",&num[i].p,&num[i].q);
}
sort(num,num+n);
double ans = 0.0;
memset(dp,0,sizeof(dp));
dp[0][m] = 1.0;
for(int i = 0;i < n;i++)
{
for(int j = m;j >= 0;j--)
{
ans+=num[i].p*dp[i][j];
if(j>=1)
dp[i+1][j-1] += dp[i][j]*num[i].q;
dp[i+1][j] += dp[i][j]*(1-num[i].q-num[i].p);
}
}
printf("Case %d: %.5lf\n",kase++,ans);
}
return 0;
}