The Preliminary Contest for ICPC Asia Nanjing 2019 - D Robots(概率dp+拓扑排序)
这题概率dp + 拓扑排序可以写
改天补解释
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
vector<int>vec[maxn];
int indeg[maxn],seq[maxn];
double d[maxn],f[maxn];
int N,M,T,tot=0;
void topo()
{
queue<int>q;
q.push(1);
tot=0;
seq[tot++]=1;
while (!q.empty()) {
int u=q.front();
q.pop();
for (int i=0;i<vec[u].size();i++) {
if (--indeg[vec[u][i]]==0) {
q.push(vec[u][i]);
seq[tot++]=vec[u][i];
}
}
}
}
int main()
{
scanf("%d",&T);
while (T--) {
scanf("%d%d",&N,&M);
for (int i=1;i<=N;i++) {
vec[i].clear();
}
memset(indeg,0,sizeof(indeg));
for (int i=0;i<=N;i++) {
d[i]=0;
f[i]=0;
}
int x,y;
for(int i=0;i<M;i++) {
scanf("%d%d",&x,&y);
vec[x].push_back(y);
indeg[y]++;
}
topo();
for (int i=tot-2;i>=0;i--) {
int u=seq[i];
int cnt=vec[u].size()+1;
for (int j=0;j<cnt-1;j++) {
d[u]+=d[vec[u][j]];
}
d[u]=(d[u]/(double)cnt+1)*(double)cnt/(double)(cnt-1);
}
for (int i=tot-2;i>=0;i--) {
int u=seq[i];
int cnt=vec[u].size()+1;
for (int j=0;j<cnt-1;j++) {
f[u]+=f[vec[u][j]];
}
f[u]+=cnt*d[u];
f[u]=f[u]/(double)(cnt-1);
}
printf("%.2lf\n",f[1]);
}
return 0;
}
The Preliminary Contest for ICPC Asia Nanjing 2019 - D Robots(概率dp+拓扑排序)的更多相关文章
- The Preliminary Contest for ICPC Asia Nanjing 2019 D. Robots
题意:给出一个DAG,一只机器人从1点出发,等概率地选择一条出边走或者停留在原点,机器人的第k次行动消耗能量是k(无论是走还是停留都算一次行动).问1到n的期望. 解法:因为行动消耗的能量跟行动次数有 ...
- The Preliminary Contest for ICPC Asia Nanjing 2019/2019南京网络赛——题解
(施工中……已更新DF) 比赛传送门:https://www.jisuanke.com/contest/3004 D. Robots(期望dp) 题意 给一个DAG,保证入度为$0$的点只有$1$,出 ...
- [The Preliminary Contest for ICPC Asia Nanjing 2019] A-The beautiful values of the palace(二维偏序+思维)
>传送门< 前言 这题比赛的时候觉得能做,硬是怼了一个半小时,最后还是放弃了.开始想到用二维前缀和,结果$n\leq 10^{6}$时间和空间上都爆了,没有办法.赛后看题解用树状数组,一看 ...
- 计蒜客 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 ...
- The Preliminary Contest for ICPC Asia Nanjing 2019
传送门 A. The beautiful values of the palace 题意: 给出一个\(n*n\)的矩阵,并满足\(n\)为奇数,矩阵中的数从右上角开始往下,类似于蛇形填数那样来填充. ...
- The Preliminary Contest for ICPC Asia Nanjing 2019 H. Holy Grail
题目链接:https://nanti.jisuanke.com/t/41305 题目说的很明白...只需要反向跑spfa然后输入-dis,然后添-dis的一条边就好了... #include < ...
- 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 ...
- 树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019
题意: 给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少. 思路: 以后记住:二维前缀和sort+树状数组就行了!!!. #define IOS ios_base::sync_wit ...
- 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 ...
随机推荐
- koa2第一天
router.get("/hello",async(ctx )=>{ const a=await new Promise(reslove=>reslove(123)) ...
- bzoj1690:[Usaco2007 Dec]奶牛的旅行 (分数规划 && 二分 && spfa)
用dfs优化的spfa判环很快啦 分数规划的题目啦 二分寻找最优值,用spfa判断能不能使 Σ(mid * t - p) > 0 最优的情况只能有一个环 因为如果有两个环,两个环都可以作为奶牛的 ...
- 题解 【Codeforces381A】 Sereja and Dima
本题是很好的双指针练习题. 关于双指针,详见洛谷日报#73. 我们可以用两个指针l和r表示题中两人接下来要比较的数字,用fl标记下一个将要取的人,并分别用两个计数器统计双方的答案. 因此,我们有了如下 ...
- my codestyle
代码风格 缩进 缩进采用4个空格或tab. 原则是:如果地位相等,则不需要缩进:如果属于某一个代码的内部代码就需要缩进. 变量命名 变量命名遵守遵从驼峰命名法,统一使用lowerCamelCase风格 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
- Gym-TORCS安装
系统为Ubuntu16.04来安装Gym-TORCS 安装pip: sudo apt-get install python-pip sudo pip install --upgrade pip 安装p ...
- jsoup学习待续
1.Jsoup简介 Jsoup是一个java html解析器.它是一个用于解析HTML文档的java库.Jsoup提供api来从URL或HTML文件中提取和操作数据.它使用DOM,CSS和类似 Jqu ...
- Hello 2020D(多重集)
如果有一对时间对在某一场馆有时间重合而这一对时间对在另一场馆没有时间重合,则输出NO,否则输出YES. #define HAVE_STRUCT_TIMESPEC #include<bits/st ...
- bash_profile文件
bash_profile文件的作用 如何填写 如何生效
- ALSA driver --PCM 实例创建过程
前面已经写过PCM 实例的创建框架,我们现在来看看PCM 实例是如何创建的. 在调用snd_pcm_new时就会创建一个snd_pcm类型的PCM 实例. struct snd_pcm { struc ...