Hdu 4089 Activation 概率DP

Activation

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


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个人前面至少有k-1个人排队,且服务器崩溃的概率。


这题需要倒过来求,用求期望的方式求概率。dp[i][j]表示队列中一共有 i 个人,排在第 j 个位置的概率。则:

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

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

由于队首的状态会转移到队尾,最后dp方程形成了一个环。

与上一题 hdu 4652一样,把式子一个个迭代,最后可以推出dp[i][i].解出一个元素之后,剩下的元素都可以由此解出。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=2005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
db dp[maxn][maxn];

int main() {
	int n,m,k,i,j;
	db p1,p2,p3,p4;
	while (scanf("%d%d%d",&n,&m,&k)!=EOF) {
		scanf("%lf%lf%lf%lf",&p1,&p2,&p3,&p4);
		if (p4<1e-6) {
			printf("0.00000\n");continue;
		}
		p2/=1.0-p1;p3/=1.0-p1;p4/=1.0-p1;
		dp[1][1]=p4/(1.0-p2);
		for (i=2;i<=n;i++) {
			dp[i][i]=pow(p2,i-1)*p4;
			for (j=2;j<=min(k,i);j++) 
			    dp[i][i]+=pow(p2,i-j)*(p3*dp[i-1][j-1]+p4);
			for (j=min(k,i)+1;j<=i;j++) 
			    dp[i][i]+=pow(p2,i-j)*p3*dp[i-1][j-1];
			dp[i][i]/=1-pow(p2,i);
			dp[i][1]=p2*dp[i][i]+p4;
			for (j=2;j<=min(k,i);j++) 
			    dp[i][j]=p2*dp[i][j-1]+p3*dp[i-1][j-1]+p4;
			for (j=min(k,i)+1;j<=i;j++) 
			    dp[i][j]=p2*dp[i][j-1]+p3*dp[i-1][j-1];
		}
		printf("%.5lf\n",dp[n][m]);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值