2019年 ACM/ICPC 亚洲区域赛(南京)网络赛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

题意:

给你一个有向无权图,保证入度、出度为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节点的平方时间的期望。

根据题意很容易得到

dp[u]=\frac{1}{chu[u]+1}*(dp[u]+1+\sum(dp[v]+1)),\left ( u,v \right )\epsilon E 

其中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,因此有

dp_{2}[u]=\frac{1}{chu[u]+1}*(dp_{2}[u]+2*dp[u]+1+\sum(dp_{2}[v]+2*dp[v]+1)), \left ( u,v \right )\epsilon E

其中E为边集。

将两个公式化简,可以得到

dp[u]=\frac{1}{chu[u]}*(1+\sum(dp[v]+1)),\left ( u,v \right )\epsilon E

dp_{2}[u]=\frac{1}{chu[u]}*(2*dp[u]+1+\sum(dp_{2}[v]+2*dp[v]+1)), \left ( u,v \right )\epsilon E

于是将图反建,按照拓扑排序的顺序进行状态转移即可。

PS:(赛场上dp2数组公式写错了,一直没过样例,菜cry...)

代码:

#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>
#include<iomanip>
#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 = 100000+5;
using namespace std;

int S,T;
int in[N],out[N];
double dp[N],dp2[N];
double p[N],pp[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]--;

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

            if(in[y]==0)
            {
                dp2[y]+=p[y]*(2.0*dp[y]+1);
                St.push(y);
            }
        }
    }
}
int main() {
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);

        memset(dp,0,sizeof(dp));
        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();
        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];
            dp[i]+=p[i];
        }

        for(int i=1;i<=n;i++){
            if(out[i]==0)
                T=i;
            if(in[i]==0)
                S=i;
        }

        Topsort(n);
        //for(int i=1;i<=n;i++)
        //cout<<dp[i]<<" "<<dp2[i]<<endl;
        double res=dp[T]/2.0+dp2[T]/2.0;
        cout<<fixed<<setprecision(2)<<res<<endl;
    }
    return 0;
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值