hdu 4089 Activation 概率dp

Activation

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


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
 

Recommend
lcy
 

題意:某人在一個長度爲n的隊列中初始在m位置,每個時刻有4種狀態。

1、不變 概率爲p1

2、隊首出隊,變成隊尾,概率爲p2.

3、隊首出隊,隊伍長度-1,概率爲p3.

4、跳出結束,概率爲p4.

求他至少在位置k(<=k)時跳出結束的概率。


思路:概率dp,dp[i][j]表示隊列中有i個人,他在位置j時跳出的概率,由4種情況可推出:

dp[i][j]=dp[i][j]*p1+dp[i][i]*p2+p4  (j=1)

dp[i][j]=dp[i][j]*p1+dp[i][j-1]*p2+dp[i-1][j-1]*p3+p4 (1<j<=k)

dp[i][j]=dp[i][j]*p1+dp[i][j-1]*p2+dp[i-1][j-1]*p3 (j>k)

將i從小到大遞推時 dp[i-1][j-1]爲已知,故可以表示爲常數項。

將常數項標記爲c[j],

化簡一下得出:

dp[i][j]=p*dp[i][i]+c[j]  (j=1)

dp[i][j]=p*dp[i][j-1]+c[j] (j>1)

其中p=p2/(1-p1).

可以看出這是一個循環,迭代一下,我用sp存係數項,sc存常數項,最後可以解出dp[i][i],就可以求出每項dp[i][j].

最後的dp[n][m]就是答案了。

其中特判一下p4<1e-5直接輸出0,不然因爲精度問題會wa。

代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;
const int N=2005;
const double eps=1e-5;
int n,m,k;
double p1,p2,p3,p4;
double dp[N][N],c[N];
void solve(){
    double p=p2/(1-p1);
    double p41=p4/(1-p1);
    double p31=p3/(1-p1);
    dp[1][1]=p41/(1-p);
    for(int i=2;i<=n;i++){
        double sp=1,sc=0;
        for(int j=1;j<=i;j++){
            if(j==1)c[j]=p41;
            else if(j<=k)c[j]=p31*dp[i-1][j-1]+p41;
            else c[j]=p31*dp[i-1][j-1];
            sp*=p;
            sc=sc*p+c[j];
        }
        dp[i][i]=sc/(1-sp);
        dp[i][1]=dp[i][i]*p+c[1];
        for(int j=2;j<i;j++){
            dp[i][j]=p*dp[i][j-1]+c[j];
        }
    }
    printf("%.5f\n",dp[n][m]);
}
int main()
{
    while(scanf("%d%d%d%lf%lf%lf%lf",&n,&m,&k,&p1,&p2,&p3,&p4)!=EOF){
        if(p4<eps)printf("0.00000\n");
        else solve();
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章