BZOJ3036: 绿豆蛙的归宿&Wikioi2488:绿豆蛙的归宿

3036: 绿豆蛙的归宿

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit: 108  Solved: 73
[Submit][Status]

Description

随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿。

给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度。绿豆蛙从起点出发,走向终点。
到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K 。
现在绿豆蛙想知道,从起点走到终点的所经过的路径总长度期望是多少?

Input

第一行: 两个整数 N M,代表图中有N个点、M条边
第二行到第 1+M 行: 每行3个整数 a b c,代表从a到b有一条长度为c的有向边

Output

从起点到终点路径总长度的期望值,四舍五入保留两位小数。

Sample Input

4 4
1 2 1
1 3 2
2 3 3
3 4 4

Sample Output

7.00

HINT

对于100%的数据  N<=100000,M<=2*N

Source

题解:
简单的期望DP,要从终点往回推
代码:
 {$M 1000000000,0,maxlongint}
const maxn=+;
type node=record
next,go,w:longint;
end;
var e:array[..*maxn] of node;
outp,head:array[..maxn] of longint;
f:array[..maxn] of double;
i,n,m,x,y,z,j,tot:longint;
procedure insert(x,y,z:longint);
begin
inc(tot);
e[tot].go:=y;e[tot].w:=z;e[tot].next:=head[x];head[x]:=tot;
end;
procedure init;
begin
readln(n,m);
for i:= to m do
begin
readln(x,y,z);insert(x,y,z);inc(outp[x]);
end;
end;
procedure dfs(x:longint);
var i,y:longint;
begin
if f[x]<>-1.0 then exit;
f[x]:=0.0;
i:=head[x];
while i<> do
begin
y:=e[i].go;
dfs(y);
f[x]:=f[x]+f[y]+e[i].w;
i:=e[i].next;
end;
if outp[x]<> then f[x]:=f[x]/outp[x];
end; procedure main;
begin
for i:= to n do f[i]:=-1.0;
dfs();
writeln(f[]::);
end;
begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
init;
main;
close(input);close(output);
end.
上一篇:【题解】 bzoj3036: 绿豆蛙的归宿 (期望dp)


下一篇:Java框架搭建-Maven、Mybatis、Spring MVC整合搭建