题意:给出一个DAG,一只机器人从1点出发,等概率地选择一条出边走或者停留在原点,机器人的第k次行动消耗能量是k(无论是走还是停留都算一次行动)。问1到n的期望。

解法:因为行动消耗的能量跟行动次数有关(有点像求E(x^2)的味道),考虑做两次概率DP。

dp1[x]表示点x到终点n的期望天数,那么dp1[x]=( dp1[x]+sigma(dp1[y]) ) / (du+1) + 1 。(du是x点度数,y是x儿子)。

dp2[x]表示点x到终点n的期望代价,那么dp2[x]=( dp2[x]+dp1[x]+1 + sigma(dp2[y]+dp1[y]+1) ) / (du+1)。(du和y同上)

先求dp1之后求dp2,答案就是dp2[1]。

代码写得很丑:

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+;
int n,m;
vector<int> G[N]; double dp1[N],dp2[N];
void dfs1(int x) {
if (x==n) return;
if (dp1[x]!=) return;
double tmp=;
for (int i=;i<G[x].size();i++) {
int y=G[x][i];
dfs1(y);
tmp+=dp1[y];
}
tmp+=G[x].size()+;
tmp/=(double)G[x].size();
dp1[x]=tmp;
} void dfs2(int x) {
if (x==n) return;
if (dp2[x]!=) return;
double tmp=;
for (int i=;i<G[x].size();i++) {
int y=G[x][i];
dfs2(y);
tmp+=dp2[y]+dp1[y]+;
}
tmp+=dp1[x]+;
tmp/=(double)G[x].size();
dp2[x]=tmp;
} int main()
{
int T; cin>>T;
while (T--) {
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) G[i].clear();
for (int i=;i<=m;i++) {
int x,y; scanf("%d%d",&x,&y);
G[x].push_back(y);
}
for (int i=;i<=n;i++) dp1[i]=dp2[i]=;
dfs1();
dfs2();
printf("%.2lf\n",dp2[]);
}
return ;
}

The Preliminary Contest for ICPC Asia Nanjing 2019 D. Robots的更多相关文章

  1. The Preliminary Contest for ICPC Asia Nanjing 2019 - D Robots(概率dp+拓扑排序)

    这题概率dp + 拓扑排序可以写 改天补解释 #include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; ve ...

  2. The Preliminary Contest for ICPC Asia Nanjing 2019/2019南京网络赛——题解

    (施工中……已更新DF) 比赛传送门:https://www.jisuanke.com/contest/3004 D. Robots(期望dp) 题意 给一个DAG,保证入度为$0$的点只有$1$,出 ...

  3. [The Preliminary Contest for ICPC Asia Nanjing 2019] A-The beautiful values of the palace(二维偏序+思维)

    >传送门< 前言 这题比赛的时候觉得能做,硬是怼了一个半小时,最后还是放弃了.开始想到用二维前缀和,结果$n\leq 10^{6}$时间和空间上都爆了,没有办法.赛后看题解用树状数组,一看 ...

  4. 计蒜客 The Preliminary Contest for ICPC Asia Nanjing 2019

    F    Greedy Sequence You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each  ...

  5. The Preliminary Contest for ICPC Asia Nanjing 2019

    传送门 A. The beautiful values of the palace 题意: 给出一个\(n*n\)的矩阵,并满足\(n\)为奇数,矩阵中的数从右上角开始往下,类似于蛇形填数那样来填充. ...

  6. The Preliminary Contest for ICPC Asia Nanjing 2019 H. Holy Grail

    题目链接:https://nanti.jisuanke.com/t/41305 题目说的很明白...只需要反向跑spfa然后输入-dis,然后添-dis的一条边就好了... #include < ...

  7. The Preliminary Contest for ICPC Asia Nanjing 2019 B. super_log (广义欧拉降幂)

    In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For examp ...

  8. 树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019

    题意: 给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少. 思路: 以后记住:二维前缀和sort+树状数组就行了!!!. #define IOS ios_base::sync_wit ...

  9. B.super_log(The Preliminary Contest for ICPC Asia Nanjing 2019)

    同:https://www.cnblogs.com/--HPY-7m/p/11444923.html #define IOS ios_base::sync_with_stdio(0); cin.tie ...

随机推荐

  1. 【LeetCode】并查集 union-find(共16题)

    链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence  (2018年11月22日,开始解决hard题) 给 ...

  2. python 中 len()和range()

    https://blog.csdn.net/qq_36357820/article/details/77850841

  3. spring boot与ElasticSearch的集成

    本文主要介绍Spring boot与ElasticSearch的集成,因为Spring boot的教程以及ElasticSearch的学习其他博客可能更优秀,所以建议再看这篇文章前先学习学习一下Spr ...

  4. STM32F103系列命名规则

    对于STM32F103xxyy系列:第一个x代表引脚数:T-36pin,C-48pin,R-64pin,V-100pin,Z-144pin:第二个x代表Flash容量:6-32K,8-64K,B-12 ...

  5. python多个装饰器

    '''在装饰器中加上参数:1.实现在源代码中加上时间统计:函数timmer2.实现用户名认证功能:函数auth23.实现一次认证,刷新后自动登录功能,index函数已经认证并登录,在执行home函数时 ...

  6. ECS运维:操作系统有异常?诊断日志来帮忙!

    ​云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新.阿里云使用严格的IDC标准.服务器准入标准 ...

  7. ldd3 第12章 PCI驱动程序

    PCI接口 PCI寻址 引导阶段 配置寄存器和初始化 MODULE_DEVICE_TABLE 注册PCI驱动程序 佬式PCI探测 激活PCI设备 访问配置空间 访问I/O和内存空间 PCI中断 硬件抽 ...

  8. java 通过反射调用属性,方法,构造器

    package reflection2; import static org.junit.Assert.assertArrayEquals; import java.lang.reflect.Cons ...

  9. vue 中使用scss

    1.下载 npm install --save-dev sass-loader npm install --save-dev node-sass npm install sass-loader --s ...

  10. AGC002 F Leftmost Ball——DP

    题目:https://atcoder.jp/contests/agc002/tasks/agc002_f 充要条件是前缀0的个数 >= 颜色种数. 设计 DP ,放一个颜色的时候就把所有该颜色的 ...