D题 Robots 【期望】
Robots
Given a directed graph with no loops which starts at node 11 and ends at node nn.
There is a robot who starts at 11, and will go to one of adjacent nodes or stand still with equal probability every day.
Every day the robot will have durability consumption which equals to the number of passed days.
Please calculate the expected durability consumption when the robot arrives at node nn.
It is guaranteed that there is only one node (node 1) whose in-degree is equal to 0,
and there is only one node (node n) whose out-degree is equal to 0. And there are no multiple edges in the graph.
Input
The first line contains one integer T (1≤T≤10)
For each case,the first line contains two integers n (2≤n≤10^5) and m (1≤m≤2×10^5), the number of nodes and the number of edges, respectively.
Each of the next mm lines contains two integers u and v (1≤u,v≤n) denoting a directed edge from u to v.
It is guarenteed that ∑n≤4×10^5, and ∑m≤5×10^5.
Output
Output T lines.Each line have a number denoting the expected durability consumption when the robot arrives at node n.
Please keep two decimal places.
样例输入
1
5 6
1 2
2 5
1 5
1 3
3 4
4 5
样例输出
9.78
翻译:
给定一个没有循环的有向图,它从节点11开始,在节点nn结束。
有一个机器人从11岁开始,每天都以相同的概率走到相邻的一个节点,或者站着不动。
机器人每天的耐用性消耗相当于过去的天数。
请计算机器人到达节点nn时的预期耐久性消耗。
保证只有一个节点(节点1)的入度为0,
并且只有一个节点(节点n)的出度等于0。图中没有多条边
输入
第一行包含一个整数T(1≤T≤10)
对于每种情况,第一行包含两个整数n(2≤n≤10^5)和m(1≤m≤2×10^5),分别为节点数和边数。
接下来的mm线每条都包含两个整数u和v(1≤u,v≤n),表示从u到v的有向边。
∑n≤4×10^5,∑m≤5×10^5。
输出
输出线。每一行都有一个数字,表示机器人到达节点n时的预期耐久性消耗。
请保留两位小数。
官方题解:

dp[i] :1到N的天数期望;
f[i]:花费代价期望;
out[i]表示点的出度;
天数期望方程
dp[i] = 1/( out[i] + 1 )*dp[i] + 1/( out[i] + 1 )*∑dp[j] + 1;
移式化简后:
dp[i] = 1/out[i]*∑dp[j] + 1 + 1/out[i];
花费期望:
f[i]=1/( out[i] + 1 )*f[i] + 1/( out[i] + 1)*∑f[j] + dp[i];
移式化间后:
f[i] = 1/out[i]*∑f[j] + (out[i] + 1) / out[i] * dp [ i ];
ac代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 110000;
int t,n,m;
int out[2*N];
double dp[2*N],f[2*N];
vector<int> v[2*N]; 
void init(){
    for(int i=0;i<=n;i++){
        v[i].clear();
        dp[i] = 0;
        f[i] = 0;
        out[i] = 0;
    }
} 
double dfsdp(int x){
    if(dp[x]!=0) return dp[x];
    if(out[x] == 0)  return dp[x];
    int len = v[x].size();
    double s1=0;
    for(int i=0;i<len;i++){
        int e=v[x][i];
        dfsdp(e);
        s1+=dp[e];
    } 
    dp[x]=s1/out[x]*1.0+1+1.0/out[x];
    return dp[x];
}
double dfsf(int x){
    if(f[x]!=0) return f[x];
    if(out[x] == 0)  return f[x];
    int len = v[x].size();
    double s1=0;
    for(int i=0;i<len;i++){
        int e=v[x][i];
        dfsf(e);
        s1+=f[e];
    }
    f[x]=s1/out[x]*1.0+(1+1.0/out[x]*1.0)*dp[x];
    return f[x];
}
int main(){
 int a,b;
    int t;
    cin>>t;
    while(t--){
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d",&a,&b);
            out[a]++;
            v[a].push_back(b);
        }
        dfsdp(1);
        dfsf(1);
        printf("%.2f\n",f[1]);
    }
    return 0;
}
D题 Robots 【期望】的更多相关文章
- CTF--web 攻防世界web题 robots  backup
		
攻防世界web题 robots https://adworld.xctf.org.cn/task/answer?type=web&number=3&grade=0&id=506 ...
 - LightOJ 1248 Dice (III) (水题,期望DP)
		
题意:给出一个n面的色子,问看到每个面的投掷次数期望是多少. 析:这个题很水啊,就是他解释样例解释的太...我鄙视他,,,,, dp[i] 表示 已经看到 i 面的期望是多少,然后两种选择一种是看到新 ...
 - HDU 5245 Joyful(概率题求期望)
		
D - Joyful Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
 - bzoj 2510: 弱题 概率期望dp+循环矩阵
		
题目: Description 有M个球,一开始每个球均有一个初始标号,标号范围为1-N且为整数,标号为i的球有ai个,并保证Σai = M. 每次操作等概率取出一个球(即取出每个球的概率均为1/M) ...
 - 2019南京网络赛 D Robots   期望dp
		
题目传送门 题意:给出一幅有向无环图,保证只有1入度为0,n出度为0,求问一个机器人从1出发,每天等概率的走到相邻点或者留在原地,问到达n点的代价.每天的代价都不一样,就是天数(第x天走一步的代价就是 ...
 - light oj 1248   第六周E题(期望)
		
E - 期望(经典问题) Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Descri ...
 - [2019南京网络赛D题]Robots
		
题目链接 2019.9.2更新 第二天睡醒想了想发现好像搜一遍就可以过,赛时写的花里胡哨的还错了,太菜了QAQ #include<bits/stdc++.h> using namespac ...
 - 期望dp+高斯消元优化——uvalive4297好题
		
非常好的题!期望+建矩阵是简单的,但是直接套高斯消元会T 所以消元时要按照矩阵的形态 进行优化 #include<bits/stdc++.h> using namespace std; ; ...
 - bzoj 2134 单选错位(期望)
		
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2134 [题意] ai与ai+1相等得1分,求期望. [思路] 每个题的期望都是独立的. ...
 
随机推荐
- jpql简单l查询
			
JPQL全称Java Persistence Query Language package com.ytkj.entity; import javax.persistence.*; import ja ...
 - Visual Studio 2013创建并运行Cocos2d-x工程
			
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.准备: 我们得先把Visual Studio 2013安装好:可以去MSDN官网下载,在安装好 2.安装好vs之后,在cmd(终端)创 ...
 - 比较全面的CSS hack方式一览
			
转载请注明来自CSDN freshlover的博客专栏<史上最全CSS Hack方式一览> 做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某 ...
 - Python37不能启动pyspider
			
报错内容: Traceback (most recent call last): File "/usr/local/var/pyenv/versions/3.7.3/bin/pyspider ...
 - Linux替换文件行首的空白字符
			
使用命令sed.cp.tail.cat 1.拷贝一个任意文件(生产环境切勿操作) cp /etc/profile /tmp 查看文件部分格式 cat /tmp/profile # /etc/profi ...
 - poi解析word文档转换成html(包括图片解析)
			
需求:将本地上传的word文档解析并放入数据库中 代码: import java.io.ByteArrayOutputStream;import java.io.File;import java.io ...
 - C/C++ 吐槽第一期:你最讨厌的C/C++里面的数据类型是什么
			
C/C++ 这里面讨论的范围包括从以往开始,到现有的所有官方标准,VC扩展,GCC扩展, C语言部分包括C89.C90.C99.C11这些知名的大版本,中间或者之前的比如K&R这种不出名的小版 ...
 - 喜讯!联诚发创始人龙平芳荣获2019LED行业优秀女企业家称号!联诚发横揽三项大奖!
			
2019年12月20日,在深圳大梅沙京基喜来登度假酒店隆重举行“蝶变跨越”慧聪LED显示屏行业品牌盛会颁奖典礼!在来自全国各地的LED显示屏行业协会领导,企业领袖,精英代表以及来自全国各 ...
 - string::size_type 页73 size_t 页90
			
异同点: size_t size_type sizeof(XXX)所得到的结果的类型就是 string类类型和vector类类型定义的类型,string的size操作返回来的是string::size ...
 - taro-安装及使用-npm
			
taro-安装及使用 https://nervjs.github.io/taro/docs/GETTING-STARTED.html 安装 Taro 项目基于 node,请确保已具备较新的 node ...