The most water probability expectation, recommended algorithm collection of "analysis of a class of mathematical expectation problem solving method"
1#include <iostream>2#include <cstdio>3 using namespacestd;4 #defineN 1000105 #defineM 2000206 structE7 {8 intnext,to,v;9 }e[m];Ten DoubleF[n]; One intHead[n],vis[n],oute[n]; A intn,m,cnt; -InlineintRead () - { the intans=0, f=1; - CharC; - while(!isdigit (C=getchar ())) {if(c=='-') f=-1;} -ans=c-'0'; + while(IsDigit (C=getchar ())) ans=ans*Ten+c-'0'; - returnans*F; + } AInlinevoidInsertintXintYintW) at{cnt++; e[cnt].next=head[x]; head[x]=cnt; e[cnt].to=y; e[cnt].v=W;} - voidDfsintx) - { - if(!vis[x]) vis[x]=1; - Else return; - for(intI=head[x];i;i=e[i].next) in { - DFS (e[i].to); tof[x]+=e[i].v+f[e[i].to]; + } - if(Oute[x]) f[x]/=Oute[x]; the } * intMain () $ {Panax NotoginsengN=read (); m=read (); - for(intI=1; i<=m;i++) the { + intx,y,w; AX=read (); Y=read (); w=read (); the //printf ("%d%d%d", x,y,w); + Insert (x,y,w); -oute[x]++; $ } $Dfs1); -printf"%.2lf\n", f[1]); - return 0; the}
View Code Description
With the new version of Baidu space offline, blog pet mung Bean Frog completed its mission, to find its new destination.
A connected graph with a direction-free loop is given, where the starting point is 1 and the end is N, and each edge has a length. The mung bean frog starts from the starting point and goes to the end.
At each vertex, if there is a road where K leaves the point, the mung bean Frog can choose any route to leave the point, and the probability of going to each road is 1/k.
Now mung bean frog want to know, from the starting point to the end of the path through the total length of the expectation is what?
Input
First line: Two integers n m, representing n points, M edges in the graph
The second line to line 1+m: 3 integers a b c each line, representing a to B with a length of C with a forward edge
Output
The expected value of the total length from the starting point to the end path, rounding retains two decimal places.
Sample Input4 4
1 2 1
1 3 2
2 3 3
3 4 4
Sample Output7.00HINT
For 100% of data n<=100000,m<=2*n
Source
Poetize3
The fate probability and expectation of "BZOJ3036" mung bean Frog