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 11) whose in-degree is equal to 00, and there is only one node (node nn) whose out-degree is equal to 00. And there are no multiple edges in the graph.
The first line contains one integer T (1 \le T \le 10)T(1≤T≤10)
For each case,the first line contains two integers n (2 \le n \le 10^5)n(2≤n≤105) and m (1 \le m \le 2 \times 10^5)m(1≤m≤2×105), the number of nodes and the number of edges, respectively.
Each of the next mm lines contains two integers uu and vv (1 \le u, v \le n)(1≤u,v≤n) denoting a directed edge from uu to vv.
It is guarenteed that \sum n \le 4\times 10^5∑n≤4×105, and \sum m \le 5 \times 10^5∑m≤5×105.
Output TT lines.Each line have a number denoting the expected durability consumption when the robot arrives at node nn.
Please keep two decimal places.
样例输入复制
1 5 6 1 2 2 5 1 5 1 3 3 4 4 5
样例输出复制
9.78
题意:
给你一个有向无权图,保证入度、出度为0的点分别为1号,n号节点。如果当前在节点u,已经走了i分钟,那么下一分钟可以等概率的留在原地,或者走向它指向的一个点(走向每个点的概率都是相等的,也就是说假设u点的出度为chu[u],那么它留在原地和走向它指向的一个点的概率均为1/(chu[u]+1)),花费为i+1。
求1号点走到n号点的花费的期望。
也就是假设从1号点走到n号点的期望时间为E分钟,则答案为E(E+1)/2。这个式子可以分成两部分,即 E*E/2 + E/2。
因此我们设dp[u]为u节点走到n节点的期望时间,dp2[u]为u节点走到n节点的平方时间的期望。
根据题意很容易得到
其中E为边集。
dp数组即E(x)。根据期望公式,dp2数组即E(x*x)
而E[(x+1)*(x+1)]=E(x*x+2*x+1)=E(x*x)+2*E(x)+1,因此有
其中E为边集。
将两个公式化简,可以得到
于是将图反建,按照拓扑排序的顺序进行状态转移即可。
PS:(赛场上dp2数组公式写错了,一直没过样例,菜cry...)
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include