题意:

  • 给出一张N个点M条边的有向无环图,1号点为全图唯一的入度为0的点,N号点为全图唯一的出度为0的点
  • 现在你从1号点出发,每单位时间,你有相同的概率 走向相邻节点或原地不动。
  • 第 i 单位时间内你的消耗为 i ,
  • 问你走到 N 点的期望消耗。

>> 我是很慢的题面链接 <<

Strategy:基于拓扑的概率dp

状态: dp1[i],dp2[i]dp1[i], dp2[i]dp1[i],dp2[i]从i点到n点的期望路径时间与消耗

目标:dp2[1]dp2[1]dp2[1] 从第一个点到第n个点的期望消耗

边界: dp1[n] = dp[2] = 0

合法判断: 本题无

转移方程:

dp1[cur]=∑dp1[to]out[cur]+1+dp1[cur]out[cur]+1+1dp1[cur] = \sum \frac{dp1[to]}{out[cur] + 1} + \frac{dp1[cur]}{out[cur] + 1} + 1 dp1[cur]=∑out[cur]+1dp1[to]​+out[cur]+1dp1[cur]​+1

选择儿子的期望选择儿子的概率 + 选择自己的期望选择自己的概率 + 贡献

dp2[cur]=∑dp2[to]out[cur]+1+dp2[cur]out[cur]+1+dp1[cur]dp2[cur] = \sum \frac{dp2[to]}{out[cur] + 1} + \frac{dp2[cur]}{out[cur] + 1} + dp1[cur] dp2[cur]=∑out[cur]+1dp2[to]​+out[cur]+1dp2[cur]​+dp1[cur]

同上

attention: 注意化简

双倍经验: 拓扑排序+记忆化搜索, 其中记忆化搜索比较无脑, 这题出的非常好,巧妙的考察了关于概率dp知识点, 比赛时想半天没想出来, 看完题解后啧啧称奇, 拍手叫好!

记忆化

#include <vector>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>
#include<iomanip>
#include<map>
#include <algorithm>
using namespace std;
#define _rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define _rev(i, a, b) for (ll i = (a); i >= (b); --i)
#define _for(i, a, b) for (ll i = (a); i < (b); ++i)
#define _rof(i, a, b) for (ll i = (a); i > (b); --i)
#define maxm 109
#define oo 0x3f3f3f3f
#define ll long long
#define db double
#define eps 1e-8
#define what_is(x) cerr << #x << " is " << x << "s" << endl;
#define met(a, b) memset(a, b, sizeof(a))
#define pi acos(-1.0)
const ll maxn = 1e5 + 10;
int head[maxn], cnt, out[maxn], m, n, in[maxn], a[maxn];
db dp1[maxn], dp2[maxn];
struct node
{int nxt, to;
} way[maxn * 2];
void addedge(int from, int to)
{way[++cnt].to = to;way[cnt].nxt = head[from];head[from] = cnt;out[from]++, in[to]++;
}
inline db dfs1(int cur)
{if (dp1[cur] || cur == n)return dp1[cur];db sum = 0;for (int i = head[cur]; i; i = way[i].nxt){int to = way[i].to;sum += dfs1(to);}return dp1[cur] = (sum + 1) / out[cur] + 1;
}
inline db dfs2(int cur)
{if (dp2[cur] || cur == n)return dp2[cur];db sum = 0;for (int i = head[cur]; i; i = way[i].nxt){int to = way[i].to;sum += dfs2(to);}return dp2[cur] = (sum + dp1[cur] * (out[cur] + 1)) / out[cur];
}
int main()
{ios::sync_with_stdio(0);int t;cin >> t;while (t--){met(dp1, 0), met(dp2, 0), met(way, 0), met(out, 0), met(in, 0), met(head, 0), cnt = 0;cin >> n >> m;_rep(i, 1, m){int u, v, t;cin >> u >> v;addedge(u, v);}dfs1(1);cout << fixed << setprecision(2) << dfs2(1) << endl;}
}

拓扑递推

#include <bits/stdc++.h>
using namespace std;
#define _rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define _rev(i, a, b) for (ll i = (a); i >= (b); --i)
#define _for(i, a, b) for (ll i = (a); i < (b); ++i)
#define _rof(i, a, b) for (ll i = (a); i > (b); --i)
#define maxm 109
#define oo 0x3f3f3f3f
#define ll long long
#define db double
#define eps 1e-8
#define what_is(x) cerr << #x << " is " << x << "s" << endl;
#define met(a, b) memset(a, b, sizeof(a))
#define pi acos(-1.0)
const ll maxn = 1e5 + 10;
int head[maxn], cnt, out[maxn], m, n, in[maxn], a[maxn];
db dp1[maxn], dp2[maxn];
struct node
{int nxt, to;
} way[maxn * 2];
void addedge(int from, int to)
{way[++cnt].to = to;way[cnt].nxt = head[from];head[from] = cnt;out[from]++, in[to]++;
}
void top()
{int count = 0;queue<int> q;_rep(i, 1, n){if (!in[i])q.push(i), a[++count] = i;}while (!q.empty()){int cur = q.front();q.pop();for (int i = head[cur]; i; i = way[i].nxt){int to = way[i].to;in[to]--;if (!in[to])q.push(to), a[++count] = to;}}
}
int main()
{ios::sync_with_stdio(0);int t;cin >> t;while (t--){met(dp1, 0), met(dp2, 0), met(way, 0), met(out, 0), met(in, 0), met(head, 0), cnt = 0;cin >> n >> m;_rep(i, 1, m){int u, v, t;cin >> u >> v;addedge(u, v);}top();_rev(i, n, 1){if (a[i] == n)continue;db sum = 0;for (int j = head[a[i]]; j; j = way[j].nxt){int to = way[j].to;sum += dp1[to];}dp1[a[i]] = (sum + 1) / out[a[i]] + 1;}_rev(i, n, 1){if (a[i] == n)continue;db sum = 0;for (int j = head[a[i]]; j; j = way[j].nxt){int to = way[j].to;sum += dp2[to];}dp2[a[i]] = (sum + dp1[a[i]]*(out[a[i]] + 1)) / out[a[i]];}cout << fixed << setprecision(2) << dp2[1] << endl;}
}

2019南京icpc网选D-robot相关推荐

  1. Greedy Sequence(2019南京icpc网络预选赛)主席树求区间小于k的最大值

    题意:给出n个整数,构造s1,s2,s3-sn s1,s2,s3-sns1,s2,s3-sn,si sisi满足五个条件 1.s1[i]=i s1[i]=is1[i]=i 2.对于1<j< ...

  2. 南师大考研632c语言,2019南京师范大学外国语言学及应用语言学考研623外国语言文学基础知识与汉语写作与830英语语言学基础知识与翻译考试真题试卷...

    2019南京师范大学外国语言学及应用语言学考研623外国语言文学基础知识与汉语写作与830英语语言学基础知识与翻译考试真题试卷 本复习全析是由仙林南师大考研网依托多年丰富的教学与辅导经验,组织仙林教学 ...

  3. 2019 ACM - ICPC 上海网络赛 E. Counting Sequences II (指数型生成函数)

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

  4. 2019 ACM - ICPC 西安邀请赛 B. Product (杜教筛) 简单数论(bushi)

    G.(2019 ACM/ICPC 全国邀请赛(西安)B) Product Weblink https://nanti.jisuanke.com/t/39269 Problem && S ...

  5. 计算机学院特色迎新标语,2019大学各学院开学迎新创意标语 2019各大学网红创意迎新宣传标语...

    每年开学,各大高校都会出现各种让人哭笑不得的开学迎新标语,这些标语创意搞笑,能拉近新生和学校的距离,让人觉得更加亲切,下面八宝网小编带来介绍. 2019大学各学院开学迎新创意标语 经济学院--博于问学 ...

  6. 2019北邮网安院机试真题(回忆版)@lantin

    2019北邮网安院机试真题(回忆版) 细不谈,前两题真的都是签到题,会简单排序和if-else都可以写的出来的题目.网安院的机试基本上是两道签到题,C题是数据结构题,D题是算法.做到保2争3,保3争4 ...

  7. 2019年“旅行者之选”全球、亚洲、中国最佳目的地 | 周末

    全球旅游规划和预订平台猫途鹰 (TripAdvisor) 公布了2019年"旅行者之选"全球最佳目的地榜单,旨在通过猫途鹰 (TripAdvisor) 全球用户的分享揭示最受欢迎的 ...

  8. 2014 网选 5007 Post Robot(暴力或者AC_自动机(有点小题大作了))

    //暴力,从每一行的开始处开始寻找要查询的字符 #include<iostream> #include<cstdio> #include<cstring> #inc ...

  9. 2019 ACM/ICPC 南京站 E.Observation,区间筛

    题目大意 求 (∑d=LR(fdxor K))(modP)\Big(\sum\limits_{d=L}^{R} (f_d\text{ xor } K)\Big)\pmod{P}(d=L∑R​(fd​  ...

最新文章

  1. IntelliJ IDEA快捷键汇总_java
  2. BZOJ1975 [Sdoi2010]魔法猪学院 k短路
  3. thrust 学习笔记
  4. servlet的使用
  5. 2018 Kaggle 报告:在技术领域,女性从业者持续减少,00后开始展露头脚
  6. 多重线性回归 多元线性回归_了解多元线性回归
  7. 带你玩转七牛云存储——高级篇
  8. python3+requests+unittest_python3+requests+unittest:接口自动化测试(一)
  9. C语言标准库 <float.h>
  10. MyBatis-Plus updateById方法更新不了空字符串/null解决方法
  11. css的语法结构由3部分组成,CSS语法
  12. python深度学习pdf_Python深度学习
  13. 局域网打印机怎么连接_苹果手机怎么连打印机?苹果系统怎么连接网络打印机?一看就会...
  14. 腾讯视频网页版无法连接服务器失败怎么办,腾讯视频突然不能投屏怎么解决 腾讯视频突然不能投屏解决方法...
  15. CentOS7中文输入法,拼音输入法
  16. 竞赛获奖系统解读:远场说话人确认中基于两阶段迁移学习解决域不匹配问题
  17. 2021年大数据工程师面试内容包括哪些?
  18. 《一篇文章全吃透》—YYModel的使用技巧
  19. Python之urlparse模块
  20. 如何从零开始创建React项目

热门文章

  1. larvael 8使用中间件+接口频次限制
  2. 物联网平台 TCP/IP HTTP MQTT 通讯协议 支持海康摄像头 数据实时采集和远程控制
  3. 每个黑ke必须学习的9大技能
  4. 工作流:小明和小强都是张老师的学生
  5. Train Problem I(火车进站问题)hdu1022
  6. Revit二次开发——读取cad中的文字信息
  7. kaggle验证步骤_经过验证的19个最佳步骤,如何找到工作
  8. python加空格_Python在字符串中插入空格
  9. redis哨兵代码测试异常之:Can connect to sentinel, but myMaster seems to be not monitored...
  10. 《笨办法学 python3》系列练习计划——34.访问列表的元素