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
给了一个N个点,M条边的有向图,有一个机器人从节点1出发要到节点N,第i天的消耗是i,问到达节点N的期望消耗是多少?
第一道图上求期望的题,记录一下。
因为消耗的量是和走的天数相关,所以要记录一下期望的天数。
dp1[i]表示的是节点i到节点N的期望到达的天数。
dp2[i]表示的是节点i到节点N的期望的消耗量。
节点j是与节点i相连的。
d表示的是节点i的出度。
化简一下这个式子,可以得到:
同理可以得到dp2的式子
化简这个式子,可以得到:
#include
using namespace std;
const int maxn=2e5+5;
int t,n,m,x,y;
vector e[maxn];
int out[maxn];
bool vis[maxn];
double dp1[maxn],dp2[maxn];
void dfs(int x)
{
if(x==n) return;
if(vis[x]) return;
vis[x]=true;
for(int i=0;i