http://acm.hdu.edu.cn/showproblem.php?pid=5013
m个游客,n座城市(m, n <= 16), 每个人从1走到n, 每次有一定概率停在原地,然后以后就不前进了。一个人到过一个城会得到一定的愉悦度,对于相邻的两座城,会额外产生Cj / Cj - 1 * Hj的愉悦度,Cj是到过j城的人数,Hj是到过j城的人在这里获得的愉悦度之和。求期望的总愉悦度。
根据题解给出的解法
http://blog.csdn.net/oilover/article/details/39526899
需要跑3s左右
优化成纯dp能变成78ms,但是看不懂别人的代码...
根据期望的线性性,分别求每个人每天的值的期望
设f[i][j][x][y]表示第i天,前j个人,前一天x,当前天y的概率
设g[i][j][x][y]表示第i天,前j个人,前一天x,当前天y的期望
那么
f[i][j][x][y]=f[i][j-1][x][y]*(1-p[j]^(i-2))+f[i][j-1][x-1][y]*p[j]^(i-2)*(1-p[j])+f[i][j-1][x-1][y-1]*p[j]^(i-1)
g[i][j][x][y]=g[i][j-1][x][y]*(1-p[j]^(i-2))+g[i][j-1][x-1][y]*p[j]^(i-2)*(1-p[j])+(g[i][j-1][x-1][y-1]+f[i][j-1][x-1][y-1]*h[j][i])*p[j]^(i-1)
状态方程不懂,求各位大神解释..
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <queue> #include <stack> #include <iostream> #include <algorithm> using namespace std; #define RD(x) scanf("%d",&x) #define RD2(x,y) scanf("%d%d",&x,&y) #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define clr0(x) memset(x,0,sizeof(x)) typedef long long LL; const int maxn = 20; int n, m; double p[maxn][maxn],h[maxn][maxn],f[maxn][maxn][maxn],g[maxn][maxn][maxn]; int main() { while (~RD2(m,n)) { for (int i = 1; i <= m; i++) { scanf("%lf", &p[i][1]); p[i][0] = 1.0; for (int j = 2; j <= n; j++) { p[i][j] = p[i][j - 1] * p[i][1]; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { scanf("%lf", &h[i][j]); } } clr0(f),clr0(g); double ans = 0; for (int i = 2; i <= n; i++) { f[i][0][0] = 1; for (int j = 1; j <= m; j++) { for (int x = m; x >= 0; x--) { for (int y = x; y >= 0; y--) { f[i][x][y] *= (1.0 - p[j][i - 2]); if (x > 0) { f[i][x][y] += f[i][x - 1][y] * p[j][i - 2] * (1.0 - p[j][1]); if (y > 0) { f[i][x][y] += f[i][x - 1][y - 1] * p[j][i - 1]; } } g[i][x][y] *= (1.0 - p[j][i - 2]); if (x > 0) { g[i][x][y] += g[i][x - 1][y] * p[j][i - 2] * (1.0 - p[j][1]); if (y > 0) { g[i][x][y] += (g[i][x - 1][y - 1] + f[i][x - 1][y - 1] * h[j][i]) * p[j][i - 1]; } } } } } for (int x = 1; x <= m; x++) { for (int y = 1; y <= x; y++) { ans += g[i][x][y] * ((double)y / x + 1.0); } } } for (int i = 1; i <= m; i++) { ans += h[i][1]; } printf("%.10f\n", ans); } return 0; }
时间: 2024-10-13 01:27:26