HDU4089 Activation

概率DP

状态转移方程:

dp[i][1] = p1*dp[i][1] + dp[i][i]*p2 + p4

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

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


一层一层的处理,然后对于每一层,依次带入转移方程或者解一个矩阵(或者是方程),把第一项求出来。第一项求出来之后后面的项根据状态转移方程依次求就行了。


#include <cstdio>
#include <cstring>

const double eps = 1e-6;
const int maxn = 2005;
int n, m, k;
double p1, p2, p3, p4;
double dp[maxn][maxn], p[maxn];

int main()
{
    while (scanf("%d%d%d%lf%lf%lf%lf", &n, &m, &k, &p1, &p2, &p3, &p4) == 7)
    {
        if (p4 < eps) {puts("0.00000"); continue;}
        p2 /= (1 - p1); p3 /= (1 - p1); p4 /= (1 - p1);
        p[0] = 1;
        for (int i=1;i<=n;i++) p[i] = p[i-1] * p2;
        memset(dp, 0, sizeof(dp));
        dp[1][1] = p4 / (p3 + p4);
        for (int i=2;i<=n;i++)
            for (int j=1;j<=n;j++)
            {
                if (j == 1)
                {
                    dp[i][1] = p4;
                    for (int x=1;x<i;x++)
                    {
                        dp[i][1] += p[x] * p3 * dp[i-1][i-x];
                        if (i+1-x <= k) dp[i][1] += p[x] * p4;
                    }
                    dp[i][1] /= (1 - p[i]);
                }
                else if (j <= k) dp[i][j] = dp[i][j-1] * p2 + dp[i-1][j-1] * p3 + p4;
                else dp[i][j] = dp[i][j-1] * p2 + dp[i-1][j-1] * p3;
            }
        printf("%.5lf\n", dp[n][m]);
    }
    return 0;
}


你可能感兴趣的:(HDU4089 Activation)