2019ICPC南京网络赛 D Robots(期望dp)

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.

Input

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

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的出度。

dp1[i] = \frac{\sum{(dp1[j]+1)}+dp1[i]+1}{d+1}

化简一下这个式子,可以得到:

dp1[i] =\frac{\sum{(dp1[j]+1)}}{d} + \frac{1}{d}

 

同理可以得到dp2的式子

dp2[i] =\frac{\sum{(dp2[j]+dp1[j]+1)}}{d+1} + \frac{dp2[i]+dp1[i]+1}{d+1}

化简这个式子,可以得到:

dp2[i] =\frac{\sum{(dp2[j]+dp1[j]+1)}}{d} + \frac{dp1[i]+1}{d}

 

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=2e5+5;
int t,n,m,x,y;
vector<int> 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<e[x].size();i++)
    {
        int y=e[x][i];
        dfs(y);
        dp1[x]+=(dp1[y]+1)/out[x];
        dp2[x]+=(dp2[y]+dp1[y]+1)/out[x];
    }
    dp1[x]+=1.0/out[x];
    dp2[x]+=(dp1[x]+1)/out[x];
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);

        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        memset(out,0,sizeof(out));
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            out[x]++;
            e[x].push_back(y);
        }
        dfs(1);
        printf("%.2f\n",dp2[1]);
        for(int i=1;i<=n;i++) e[i].clear();
    }
    return 0;
}

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值