Robots(2019 ACM-ICPC 南京赛区网络赛 D)

Problem Description

Given a directed graph with no loops which starts at node 1 and ends at node n.

There is a robot who starts at 1, 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≤105) and m(1≤m≤2×105), 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×105, and ∑m≤5×105.

Output

Output T lines.Each line have a number denoting the expected durability consumption when the robot arrives at node n.

​​​​​​​Sample Input

1
5 6
1 2
2 5
1 5
1 3
3 4
4 5

​​​​​​​Sample Output

9.78

题意:t 组样例,每组给出一 n 个点 m 条边的 DAG 图,现在要从 1 号点走到 n 号点,对于每个点的,其走向下一个点与在当前点停留不动的概率均等,每 i 天的花费是 i,问走到第 n 个点的期望是多少

思路:期望 dp

由于走向下一个点与在当前点停留不动的概率均等,那么假设 x 点的出度为 out[x],其停留不动与走向其一个邻接点的概率为 p[x]=\frac{1}{out[x]+1},花费为 i+1

假设从 1 号点走到 n 号点的期望时间为 E 分钟,那么答案为 \frac{E(E+1)}{2}=\frac{E^2}{2}+\frac{E}{2},即答案可分为 \frac{E^2}{2} 与 \frac{E}{2} 两部分

设 dp1[x] 为点 x 到点 n 的期望时间,dp2[x] 为点 x 到点 n 的平方时间的期望

那么根据题意,有:dp1[x]=p[x]*((dp1[x]+1)+\sum_{(x,y)\in E}(dp1[y]+1))

根据数学期望的性质:E[(x+1)^2]=E(x^2+2x+1)=E(x^2)+2E(x)+1

于是,有:dp2[x]=p[x]*((dp2[x]+2dp1[x]+1)+\sum_{(x,y)\in E}(dp2[y]+2dp1[y]+1))

将两个公式进行化简,有:\left\{\begin{matrix} dp1[x]=\frac{1}{out[x]}(1+\sum(dp1[y]+1)) \:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\: \\ dp2[x]=\frac{1}{out[x]}(dp2[x]+2dp1[x]+1+\sum(dp2[y]+2dp1[y]+1)) \end{matrix}\right.

因此反向建图,按照拓扑排序的顺序进行转移即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

int S,T;
int in[N],out[N];
double dp1[N],dp2[N];
double p[N];
vector<int> G[N];
void Topsort(int n) {
    stack<int> St;
    St.push(S);

    while(!St.empty()) {
        int x=St.top();
        St.pop();

        for(int i=0; i<G[x].size(); i++) {
            int y=G[x][i];
            in[y]--;

            dp1[y]+=p[y]*(dp1[x]+1.0);
            dp2[y]+=p[y]*(dp2[x]+2*dp1[x]+1.0);

            if(in[y]==0) {
                dp2[y]+=p[y]*(2.0*dp1[y]+1);
                St.push(y);
            }
        }
    }
}
int main() {
    int t;
    scanf("%d",&t);
    while(t--){
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        memset(out,0,sizeof(out));
        memset(in,0,sizeof(in));
        for(int i=0;i<N;i++)
            G[i].clear();

        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            G[y].push_back(x);
            in[x]++;
            out[y]++;
        }

        for(int i=1;i<=n;i++){
            if(in[i]!=0){
                p[i]=1.0/in[i];
                dp1[i]+=p[i];
            }
            if(in[i]==0)//记录起点
                S=i;
            if(out[i]==0)//记录终点
                T=i;
        }

        Topsort(n);

        double res=dp1[T]/2.0+dp2[T]/2.0;
        printf("%.2lf\n",res);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值