Hdu 4089 Activation(概率DP)

题目链接

Activation

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1574    Accepted Submission(s): 597


Problem Description
After 4 years' waiting, the game "Chinese Paladin 5" finally comes out. Tomato is a crazy fan, and luckily he got the first release. Now he is at home, ready to begin his journey.
But before starting the game, he must first activate the product on the official site. There are too many passionate fans that the activation server cannot deal with all the requests at the same time, so all the players must wait in queue. Each time, the server deals with the request of the first player in the queue, and the result may be one of the following, each has a probability:
1. Activation failed: This happens with the probability of p1. The queue remains unchanged and the server will try to deal with the same request the next time.
2. Connection failed: This happens with the probability of p2. Something just happened and the first player in queue lost his connection with the server. The server will then remove his request from the queue. After that, the player will immediately connect to the server again and starts queuing at the tail of the queue.
3. Activation succeeded: This happens with the probability of p3. Congratulations, the player will leave the queue and enjoy the game himself.
4. Service unavailable: This happens with the probability of p4. Something just happened and the server is down. The website must shutdown the server at once. All the requests that are still in the queue will never be dealt.
Tomato thinks it sucks if the server is down while he is still waiting in the queue and there are no more than K-1 guys before him. And he wants to know the probability that this ugly thing happens.
To make it clear, we say three things may happen to Tomato: he succeeded activating the game; the server is down while he is in the queue and there are no more than K-1 guys before him; the server is down while he is in the queue and there are at least K guys before him.
Now you are to calculate the probability of the second thing.
 

Input
There are no more than 40 test cases. Each case in one line, contains three integers and four real numbers: N, M (1 <= M <= N <= 2000), K (K >= 1), p1, p2, p3, p4 (0 <= p1, p2, p3, p4 <= 1, p1 + p2 + p3 + p4 = 1), indicating there are N guys in the queue (the positions are numbered from 1 to N), and at the beginning Tomato is at the Mth position, with the probability p1, p2, p3, p4 mentioned above.
 

Output
A real number in one line for each case, the probability that the ugly thing happens.
The answer should be rounded to 5 digits after the decimal point.
 

Sample Input
  
  
2 2 1 0.1 0.2 0.3 0.4 3 2 1 0.4 0.3 0.2 0.1 4 2 3 0.16 0.16 0.16 0.52
 

Sample Output
  
  
0.30427 0.23280 0.90343
 

Source

题意:排队注册游戏。初始的时候队列有n个人,主角在队列的第m个位置。会出现下面四种情况:

1,p1的概率,队列保持不变和服务器将设法处理下次同样的请求。

2,p2的概率,服务器出问题,没有处理队首那个人的信息,队首那个人出队后马上加到队尾。

3,p3的概率,注册成功,队首那个人出队。

4,p4的概率,服务器崩溃,所有请求不处理。

问服务器崩溃的时候,主角前面最多有K-1个人的概率。


题解:设目标状态为:服务器崩溃,此时队列中在主角前面的人数不超过K个。

用dp[ i ][ j ] 表示当前队列中有 i 个人,主角在队列中第 j 个位置,要从当前状态变到目标状态的概率为多少?

那么转移就是:

j==1:dp[ i ][ j ]=p1*dp[ i ][ j ]+p2*dp[ i ][ i ]+p3*dp[ i-1 ][ j-1 ]+p4 ;

2<=j<=k:dp[ i ][ j ]=p1*dp[ i ][ j ]+p2*dp[ i ][ j-1 ]+p3*dp[ i-1 ][ j-1 ]+p4 ;

k<j<=i::dp[ i ][ j ]=p1*dp[ i ][ j ]+p2*dp[ i ][ j-1 ]+p3*dp[ i-1 ][ j-1 ] ;

我们枚举 i—>n 依次求解,所以求dp[ i ] 的时候,dp[ i-1 ]可以看成常量。

可以将转移写成如下形式:

j==1: dp[ i ][ j ]=pp2*dp[ i ][ i ]+pp3*dp[ i-1 ][ j-1 ]+pp4;

2<=j<=k: dp[ i ][ j ]=pp2*dp[ i ][ j-1 ]+pp3*dp[ i-1 ][ j-1 ]+pp4;

k<j<=i: dp[ i ][ j ]=pp2*dp[ i ][ j-1 ]+pp3*dp[ i-1 ][ j-1 ];

pp2=p2/(1-p1) , pp3=p3/(1-p1),pp4=p4/(1-p1);

我们可以将dp[ i ][ j ] 方程式中已知的量设成 C[ j ]。

这个方程式不能递归求解。对于dp[ i ] 来说,有 i 个未知量,有i个方程式,我们是可以求解出答案的。

最常见的方法是,用一个未知数表示出所有未知数,求出这个未知数就求出了所有未知数。

这里我是用dp[ i ][ i ] 来表示所有的未知数,我们可以得出方程:

dp[ i ][ i ]=pp2^i*dp[ i ][ i ]+(sum(pp2^k+c[ i-k ]),0<=k<n)。

这样我们就能解出dp[ i ][ i ],那么其余的未知数也可以求出。

注意:对于解方程式过程中出现的所有分子,我们要保证它们不等于0,但是题目的数据中可能出现分子为0的情况,所以我们要特殊处理分子为0的情况。显然当分子为0的时候,答案一定为0。

详情见代码:

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<string.h>
#include<string>
#include<math.h>
#define nn 2100
#define eps 1e-8
#define inff 0x3fffffff
typedef long long LL;
using namespace std;
int n,m,k;
double p1,p2,p3,p4;
double dp[2][nn];
double po[nn];
double pp2,pp3,pp4;
int main()
{
    int i,j;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        scanf("%lf%lf%lf%lf",&p1,&p2,&p3,&p4);
        if(fabs(1.0-p1)<=eps)
        {
            pp2=pp3=pp4=0;
        }
        else
        {
            pp2=p2/(1.0-p1);
            pp3=p3/(1.0-p1);
            pp4=p4/(1.0-p1);
        }
        double ix,fc;
        po[0]=1.0;
        for(i=1;i<=n;i++)
        {
            po[i]=po[i-1]*pp2;
        }
        memset(dp,0,sizeof(dp));
        int t=0;
        for(i=1;i<=n;i++)
        {
            ix=0;
            for(j=1;j<=i;j++)
            {
                if(j<=k)
                    ix+=po[i-j]*(pp3*dp[1-t][j-1]+pp4);
                else
                    ix+=po[i-j]*pp3*dp[1-t][j-1];
            }
            if(fabs(1.0-po[i])<=eps)
                fc=0;
            else
                fc=ix/(1.0-po[i]);
            dp[t][i]=fc;
            for(j=1;j<i;j++)
            {
                dp[t][j]=po[1]*fc+pp3*dp[1-t][j-1];
                if(j<=k)
                    dp[t][j]+=pp4;
                fc=dp[t][j];
            }
            t=1-t;
        }
        printf("%.5lf\n",dp[1-t][m]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值