题目
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(1≤T≤10)T(1≤T≤10)
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(2≤n≤105)n(2≤n≤105)andm(1≤m≤2×105)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 v v ( 1 ≤ u , v ≤ n ) ( 1 ≤ u , v ≤ n ) vv (1 \le u, v \le n)(1≤u,v≤n) vv(1≤u,v≤n)(1≤u,v≤n) denoting a directed edge from u to v.
It is guarenteed that
∑
n
≤
4
×
1
0
5
\sum n ≤4×10^5
∑n≤4×105
, and
∑
m
≤
5
×
1
0
5
\sum m ≤5×10^5
∑m≤5×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);
}
}