3036: Time Limit: 2 sec memory limit: 128 MB
Submit: 108 solved: 73
[Submit] [Status] Description
With the release of the new version of Baidu space, the blog pet green frog has completed its mission to find its new home.
A directed acyclic graph is provided. The starting point is 1 and the ending point is N, and each edge has a length. The green frog starts from the starting point and ends.
When each vertex is reached, if K roads leave the vertex, the green frog can choose any road to leave the vertex, and the probability of going to each path is 1/K.
What is the total length of the path from the start point to the end point?
Input
The first line: two integers n m, representing n vertices and m edges in the graph.
Row 2 to row 1st + M: each row has three integers, a B c, representing a directed edge with a length of C from A to B.
Output
The expected value of the total length from the start point to the end point path is rounded to 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
Question: A simple expectation for DP is to push the code back from the end point:
1 {$M 1000000000,0,maxlongint} 2 const maxn=100000+1000; 3 type node=record 4 next,go,w:longint; 5 end; 6 var e:array[0..2*maxn] of node; 7 outp,head:array[0..maxn] of longint; 8 f:array[0..maxn] of double; 9 i,n,m,x,y,z,j,tot:longint;10 procedure insert(x,y,z:longint);11 begin12 inc(tot);13 e[tot].go:=y;e[tot].w:=z;e[tot].next:=head[x];head[x]:=tot;14 end;15 procedure init;16 begin17 readln(n,m);18 for i:=1 to m do19 begin20 readln(x,y,z);insert(x,y,z);inc(outp[x]);21 end;22 end;23 procedure dfs(x:longint);24 var i,y:longint;25 begin26 if f[x]<>-1.0 then exit;27 f[x]:=0.0;28 i:=head[x];29 while i<>0 do30 begin31 y:=e[i].go;32 dfs(y);33 f[x]:=f[x]+f[y]+e[i].w;34 i:=e[i].next;35 end;36 if outp[x]<>0 then f[x]:=f[x]/outp[x];37 end;38 39 procedure main;40 begin41 for i:=1 to n do f[i]:=-1.0;42 dfs(1);43 writeln(f[1]:0:2);44 end;45 begin46 assign(input,‘input.txt‘);assign(output,‘output.txt‘);47 reset(input);rewrite(output);48 init;49 main;50 close(input);close(output);51 end.
View code