Robots 2019icpc南京网络赛 DAG概率DP

题目

Description

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

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 n.

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 ) T ( 1 ≤ T ≤ 10 ) T (1 \le T \le 10)T(1≤T≤10) T(1T10)T(1T10)

For each case,the first line contains two integers n ( 2 ≤ n ≤ 1 0 5 ) n ( 2 ≤ n ≤ 105 ) a n d m ( 1 ≤ m ≤ 2 × 1 0 5 ) m ( 1 ≤ m ≤ 2 × 105 n (2 \le n \le 10^5)n(2≤n≤10 5 ) and m (1 \le m \le 2 \times 10^5)m(1≤m≤2×10 5 n(2n105)n(2n105)andm(1m2×105)m(1m2×105
), the number of nodes and the number of edges, respectively.

Each of the next mm lines contains two integers uu and v v ( 1 ≤ u , v ≤ n ) ( 1 ≤ u , v ≤ n ) vv (1 \le u, v \le n)(1≤u,v≤n) vv(1u,vn)(1u,vn) denoting a directed edge from u to v.

It is guarenteed that ∑ n ≤ 4 × 1 0 5 \sum n ≤4×10^5 n4×105
, and ∑ m ≤ 5 × 1 0 5 \sum m ≤5×10^5 m5×105
.

Output

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

Sample Input

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

Sample Output

9.78

分析

DAG上的概率DP, 首先要进行拓扑排序,确定DP顺序。
题目里说每天的消耗等于经过的天数,这个其实不好实现,但是换一种说法,求以最后经过天数为末项,1为首项,公差为1的等差数列之和是不是就好多了?
事实上这就是解法,设x为最后的总天数, E ( x 2 + x 2 ) E(\frac{x^2+x}{2}) E(2x2+x)就是这题的答案。
需要注意的是期望的平方不等于平方的期望 ( E ( x 2 ) − E 2 ( x ) = D ( x ) , D ( x ) 为 方 差 ) (E(x^2)-E^2(x)=D(x),D(x)为方差) (E(x2)E2(x)=D(x)D(x))
需要利用下面这个式子 E ( ( x + 1 ) 2 ) = E ( x 2 + 2 x + 1 ) = E ( x 2 ) + 2 E ( x ) + 1 E((x+1)^2)=E(x^2+2x+1)=E(x^2)+2E(x)+1 E((x+1)2)=E(x2+2x+1)=E(x2)+2E(x)+1利用这个式子走一遍求出 E ( x ) 和 E ( x 2 ) E(x)和E(x^2) E(x)E(x2)即可。
AC代码(比赛的时候敲得有点丑QAQ):

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int MAXM=2e5+10;
const int MAXN = 1e5 + 10;
struct Edge{
	int to,next;
}edge[MAXM];
int head[MAXN],tot, cnt[MAXN];
double dp[MAXN], p[MAXN], tp[MAXN];
void init(){
	tot=0;
	memset(head,-1,sizeof(head));
}
void addedge(int u,int v){
	edge[tot]=(Edge){v,head[u]};
	head[u]=tot++;
}
vector<int> topo;
int deg[MAXN];
void bfs(int n){
    topo.clear();
    queue<int> Q;
    for (int u=1;u<=n;++u){
        if (!deg[u]){
            Q.push(u);
        }
    }
    while (!Q.empty()){
        int u=Q.front();
        Q.pop();
        topo.push_back(u);
        for (int i=head[u];~i;i=edge[i].next){
            if (!(--deg[edge[i].to]))
                Q.push(edge[i].to);
        }
    }
}
int T,n,m,u,v;
int main()
{
	for (scanf("%d",&T);T--;){
		scanf("%d%d",&n,&m);
		init();
		memset(deg,0,sizeof(deg));
		for (int i=0;i<m;++i){
			scanf("%d%d",&u,&v);
			addedge(v,u);
			++deg[u];
		}
		for(int i = 1; i <= n; i++) p[i] = 1.0/(double)(deg[i]+1);
		for(int i = 1; i < n; i++) p[i] = p[i]/(1.0-p[i]);
		bfs(n);
		memset(dp, 0, sizeof(dp));
		memset(tp, 0, sizeof(tp));
		for(int i = 1; i < n; i++) dp[i] = p[i];
		for(int i = 0; i < n-1; i++){
			int k = topo[i];
			for(int j = head[k]; j != -1; j = edge[j].next){
				int v = edge[j].to;
				dp[v] += p[v]*(dp[k] + 1.0);
			}
		}
		for(int i = 1; i < n; i++) tp[i] = p[i]*(2.0*dp[i]+1.0);
		for(int i = 0; i < n-1; i++){
			int k = topo[i];
			for(int j = head[k]; j != -1; j = edge[j].next){
				int v = edge[j].to;
				tp[v] += p[v]*(tp[k]+2.0*dp[k]+1.0); 
			}
		}
		printf("%.2f\n", (dp[1]+tp[1])/2.0);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值