首页 > 代码库 > POJ 3071 概率DP

POJ 3071 概率DP

链接:

http://poj.org/problem?id=3071

代码:

31 double a[130][130];
32 double dp[130][10];
33 
34 int main() {
35     int n;
36     while (cin >> n && n != -1) {
37         int m = 1 << n;
38         rep(i, 0, m) {
39             rep(j, 0, m) scanf("%lf", &a[i][j]);
40             dp[i][0] = 1;
41         }
42         int ans;
43         rep(i, 0, n) {
44             ans = 0;
45             rep(j, 0, m) {
46                 double sum = 0;
47                 rep(k, (1 << i), 1 << (i + 1))
48                     sum += dp[k^j][i] * a[j][k^j];
49                 dp[j][i + 1] = dp[j][i] * sum;
50                 if (dp[j][i + 1] > dp[ans][i + 1]) ans = j;
51             }
52         }
53         cout << ans + 1 << endl;
54     }
55     return 0;
56 }

 

POJ 3071 概率DP