题目:http://poj.org/problem?id=2096

题目好长...意思就是每次出现 x 和 y,问期望几次 x 集齐 n 种,y 集齐 s 种;

所以设 f[i][j] 表示已经有几种,转移一下即可。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef double db;
int const xn=1005;
int n,s; db f[xn][xn];
int main()
{scanf("%d%d",&n,&s);for(int i=n;i>=0;i--)for(int j=s;j>=0;j--){if(i==n&&j==s)continue;db p0=1.0*i/n*j/s,p1=1.0*(n-i)/n*j/s,p2=1.0*i/n*(s-j)/s,p3=1.0*(n-i)/n*(s-j)/s;f[i][j]=p1*f[i+1][j]+p2*f[i][j+1]+p3*f[i+1][j+1]+1;f[i][j]=f[i][j]/(1-p0);}printf("%.6f\n",f[0][0]);return 0;
}

View Code

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754

带环的期望DP,本来用高斯消元可以做,但 n^3 * T 过不了;

发现每个状态都有到 f[0] 的转移,所以设 f[i] = A[i] * f[0] + B[i] (套路!)

然后把递推式子代入一番,得到 A[i] 和 B[i] 的转移是无环的,求出 A[0], B[0] 即可。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef double db;
int rd()
{int ret=0,f=1; char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();return f?ret:-ret;
}
int const xn=550;
int n,f[xn]; db p[20],A[xn],B[xn];
int main()
{int T=rd();while(T--){n=rd(); int k1=rd(),k2=rd(),k3=rd(),a=rd(),b=rd(),c=rd(),sum=k1+k2+k3;for(int i=0;i<=sum;i++)p[i]=0;p[0]=1.0/(k1*k2*k3);for(int i=1;i<=k1;i++)for(int j=1;j<=k2;j++)for(int k=1;k<=k3;k++)if(i!=a||j!=b||k!=c)p[i+j+k]+=p[0];for(int i=0;i<=n+sum;i++)A[i]=0,B[i]=0;for(int i=n;i>=0;i--){for(int k=3;k<=sum;k++)A[i]+=p[k]*A[i+k],B[i]+=p[k]*B[i+k];A[i]+=p[0]; B[i]+=1;}printf("%.10f\n",B[0]/(1-A[0]));}return 0;
}

View Code

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4035

树上带环!同样可以高斯消元,但复杂度不行;

设 \( f[x] \) 表示在 x 这个点距离结束的期望,\( P[x] = 1 - K[x] - E[x] \),\( d[x] \) 为度数,得到朴素方程:

\( f[x] = K[x] * f[1] + \frac{P[x]}{d[x]}(f[fa]+1) + \frac{P[x]}{d[x]}\sum\limits_{v \in son}(f[v]+1) \)

由于转移的顺序实际上应该是从 \( fa \) 到 \( x \),又因为每个点都和 \( 1 \) 组成环(或者因为最后要求 \( f[1] \) ?),所以设 \( f[x] = A[x]f[1] + B[x]f[fa] + C[x] \)

于是可以树形DP得到 \( A[x], B[x], C[x] \)

\( f[1] = \frac{C[1]}{1-A[1]} \),当 \( A[1] \) 趋近于 1 时无解;

eps 设成 1e-8 会 WA,1e-10 才可以。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define eps 1e-10
using namespace std;
typedef double db;
int rd()
{int ret=0,f=1; char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();return f?ret:-ret;
}
int const xn=10005;
int n,hd[xn],ct,to[xn<<1],nxt[xn<<1],d[xn];
db K[xn],E[xn],P[xn],A[xn],B[xn],C[xn];
void add(int x,int y){to[++ct]=y; nxt[ct]=hd[x]; hd[x]=ct;}
void dfs(int x,int fa)
{db as=0,bs=0,cs=0;for(int i=hd[x],u;i;i=nxt[i]){if((u=to[i])==fa)continue;dfs(u,x);as+=A[u]; bs+=B[u]; cs+=C[u];}A[x]=(d[x]*K[x]+P[x]*as)/(d[x]-P[x]*bs);B[x]=P[x]/(d[x]-P[x]*bs);C[x]=(P[x]*cs+d[x]*P[x])/(d[x]-P[x]*bs);
}
int main()
{int T=rd(),cnt=0;while(T--){cnt++; n=rd(); ct=0; memset(hd,0,sizeof hd); memset(d,0,sizeof d);for(int i=1,x,y;i<n;i++)x=rd(),y=rd(),add(x,y),add(y,x),d[x]++,d[y]++;for(int i=1;i<=n;i++)scanf("%lf%lf",&K[i],&E[i]),K[i]/=100,E[i]/=100,P[i]=1-K[i]-E[i];for(int i=1;i<=n;i++)A[i]=B[i]=C[i]=0;dfs(1,0);printf("Case %d: ",cnt);if(fabs(A[1]-1)<eps)puts("impossible");else printf("%.8f\n",C[1]/(1-A[1]));}return 0;
}

View Code

转载于:https://www.cnblogs.com/Zinn/p/10278561.html

poj 2096 , zoj 3329 , hdu 4035 —— 期望DP相关推荐

  1. 【POJ - 2096】Collecting Bugs(概率dp)

    题干: Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material ...

  2. hdu 4035 可能性DP 成都网络游戏

    http://acm.hdu.edu.cn/showproblem.php?pid=4035 获得: 1.首先推断是不是树.事实上,所有的感觉身影,既看边数==算-1是不成立 2.有时候,我告诉孩子来 ...

  3. POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新

    直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...

  4. POJ 2096 Collecting Bugs:期望dp

    题目链接:http://poj.org/problem?id=2096 题意: 有一个程序猿,他每天都会发现一个bug. bug共有n个种类.属于某一个种类的概率为1/n. 有s个子系统,每个bug属 ...

  5. HDU 6656 Kejin Player (期望DP 逆元)

    2019 杭电多校 7 1011 题目链接:HDU 6656 比赛链接:2019 Multi-University Training Contest 7 Problem Description Cub ...

  6. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  7. Everything Is Generated In Equal Probability HDU 6595(期望dp)

    大致题意 好像是给一个数字n,然后随机选择一个1到n的数字,对其排列进行这样一种运算.首先res加上当前排列的逆序对数,然后再随机选择当前数列的子序列,继续进行该运算直到子序列长度为0.求res的期望 ...

  8. 插头DP 概率DP / 期望DP

    插头DP && 概率DP / 期望DP 写在前面: 插头DP P5056 [模板]插头dp 手写哈希表的方法: 拉链法的代码如下: 开放寻址法的代码如下: 接下来是这道题的代码实现: ...

  9. luogu P4745 [CERC2017]Gambling Guide(期望DP + 最短路实现)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 P4745 [CERC2017]Gambling Guide(期望DP + 最短路实现) Weblin ...

最新文章

  1. 前端编码风格规范(3)—— JavaScript 规范
  2. /dev/zero和/dev/null的区别
  3. C#6.0中$的用法
  4. 编程c语言顺口溜,C语言运算符优先级顺口溜[转]
  5. 《JavaScript高效图形编程(修订版)》——6.7 画布绘制基础
  6. php聊天室禁止提交,phpcms v9禁止提交信息到官网方法详解
  7. 靠模仿就能建立第二个“Google”?
  8. 在线学编程python_我跟爸爸学编程:从Python到C++
  9. git jenkins 子目录_在Jenkins中,如何将项目签出到特定目录(使用GIT)
  10. POST 请求的三种常见数据提交格式
  11. 使用企业微信的jsdk调用企业微信api--前端
  12. JavaScript就这么回事(好收藏,哪天忘了可以查一查)
  13. 投资,什么是真正的风险?
  14. 电脑休眠后无法唤醒的解决办法!
  15. C语言贪吃蛇游戏主要功能,C语言游戏-贪吃蛇
  16. 'CALayer position contains NaN: [nan nan]'异常
  17. 空间管理员(一)AFS文件目录系统索引节点vfs_inode结构
  18. word自带公式等号对齐(可任意符号处对齐)
  19. 公安情报指挥一体化合成作战平台建设,指挥调度系统开发
  20. 牛客网--14609--Forever97与寄信

热门文章

  1. 机器学习原理与算法(六) 支持向量机
  2. linux下kill某个应用
  3. Linux tar命令高级用法——备份数据
  4. 动态规划(制表法)模板及应用
  5. 内核中的内存申请:kmalloc、vmalloc、kzalloc、get_free_pages 之间的区别
  6. spring心得6--自动装配知识点讲解及案例分析
  7. 数据结构之顺序表(一)
  8. c# 写文件注意问题及用例展示
  9. AngualrJS之服务器端通信
  10. 《Windows PowerShell实战指南(第2版)》——1.4 搭建自己的实验环境