【题目链接】
http://poj.org/problem?id=3071
【算法】
概率DP
f[i][j]表示第j支队伍进入第i轮的概率,转移比较显然
【代码】
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; int i,j,k,n,pos; double a[300][300],f[10][300]; int main() { while (scanf("%d",&n) != EOF && n != -1) { memset(f,0,sizeof(f)); for (i = 1; i <= (1 << n); i++) { for (j = 1; j <= (1 << n); j++) { scanf("%lf",&a[i][j]); } } for (i = 1; i <= (1 << n); i++) f[1][i] = 1.0; for (i = 2; i <= n + 1; i++) { for (j = 1; j <= (1 << n); j++) { for (k = 1; k <= (1 << n); k++) { if ((((j - 1) >> (i - 2)) ^ 1) == ((k - 1) >> (i - 2))) f[i][j] += f[i-1][k] * f[i-1][j] * a[j][k]; } } } pos = 0; for (i = 1; i <= (1 << n); i++) { if (f[n+1][i] > f[n+1][pos]) pos = i; } printf("%d\n",pos); } return 0; }
原文地址:https://www.cnblogs.com/evenbao/p/9295751.html
时间: 2024-11-13 02:46:18