HDU4800(概率DP)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 511111
#define INF 1000000009

double dp[11111][150], f[150][150];
int n, m, cnt;
int a[maxn];

int main () {
    //freopen ("in", "r", stdin);
    while (scanf ("%d", &m) == 1) {
        cnt = (m-1)*m*(m-2) / 6;
        for (int i = 0; i < cnt; i++) {
            for (int j = 0; j < cnt; j++) {
                scanf ("%lf", &f[i][j]);
            }
        }
        scanf ("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf ("%d", &a[i]);
        }
        memset (dp, 0, sizeof dp);
        for (int i = 0; i < cnt; i++)
            dp[0][i] = 1.0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < cnt; j++) {
                dp[i+1][j] = max (dp[i][j]*f[j][a[i+1]], dp[i+1][j]);
                dp[i+1][a[i+1]] = max (dp[i+1][a[i+1]], dp[i][j]*f[j][a[i+1]]);
            }
        }
        double ans = 0;
        for (int i = 0; i < cnt; i++) {
            ans = max (ans, dp[n][i]);
        }
        printf ("%.6f\n", ans);
    }
    return 0;
}

你可能感兴趣的:(HDU4800(概率DP))