Description

放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩。进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点、m条道路的无向连通图,且该图中至多有一个环(即m只可能等于n或者n-1)。小Z现在所在的大门也正好是一个景点。小Z不知道什么好玩,于是他决定,从当前位置出发,每次随机去一个和当前景点有道路相连的景点,并且同一个景点不去两次(包括起始景点)。贪玩的小Z会一直游玩,直到当前景点的相邻景点都已经访问过为止。小Z所有经过的景点按顺序构成一条非重复路径,他想知道这条路径的期望长度是多少?小Z把游乐园的抽象地图画下来带回了家,可是忘了标哪个点是大门,他只好假设每个景点都可能是大门(即每个景点作为起始点的概率是一样的)。同时,他每次在选择下一个景点时会等概率地随机选择一个还没去过的相邻景点。
Input

第一行是两个整数n和m,分别表示景点数和道路数。 接下来行,每行三个整数Xi, Yi, Wi,分别表示第i条路径的两个景点为Xi, Yi,路径长Wi。所有景点的编号从1至n,两个景点之间至多只有一条道路。
Output

共一行,包含一个实数,即路径的期望长度。
Sample Input
4 3
1 2 3
2 3 1
3 4 4
Sample Output
6.00000000

【样例解释】样例数据中共有6条不同的路径: 路径 长度 概率
1-->4 8 1/4
2-->1 3 1/8
2-->4 5 1/8
3-->1 4 1/8
3-->4 4 1/8
4-->1 8 1/4
因此期望长度 = 8/4 + 3/8 + 5/8 + 4/8 + 4/8 + 8/4 = 6.00
【评分方法】本题没有部分分,你程序的输出只有和标准答案的差距不超过0.01时,才能获得该测试点的满分,否则不得分。
【数据规模和约定】对于100%的数据,1 <= Wi <= 100。 测试点编号 n m 备注
1 n=10 m = n-1 保证图是链状
2 n=100 只有节点1的度数大于2
3 n=1000 /
4 n=100000 /
5 n=100000 /
6 n=10 m = n /
7 n=100 环中节点个数<=5
8 n=1000 环中节点个数<=10
9 n=100000 环中节点个数<=15
10 n=100000 环中节点个数<=20

一直WA,最后才发现环没仔细考虑,因为最后一个是不可能走到i的所以度数减1

就是求一个down和一个up,分别表示向下走的期望长度和向上走的期望长度

对于环,我们以每个环上的节点为根求一边down

然后枚举环上的点更新up值,环上点的up值就表示第一条边走环边的期望长度

然后用环上节点的up值更新其他点的up值

 const
maxn=;
var
first,d,fa:array[..maxn]of longint;
up,down:array[..maxn]of double;
vis,cir:array[..maxn]of boolean;
last,next,w:array[..maxn*]of longint;
huan,len:array[..maxn]of longint;
n,m,tot,cnt:longint;
ans:double;
flag:boolean; procedure insert(x,y,z:longint);
begin
inc(tot);
last[tot]:=y;
next[tot]:=first[x];
first[x]:=tot;
w[tot]:=z;
inc(d[y]);
end; procedure dfs(x:longint);
var
i,j:longint;
begin
vis[x]:=true;
i:=first[x];
while i<> do
begin
if flag then exit;
if (vis[last[i]])and(fa[x]<>last[i]) then
begin
len[last[i]]:=w[i];
j:=x;
while j<>fa[last[i]] do
begin
inc(cnt);cir[j]:=true;
huan[cnt]:=j;
j:=fa[j];
end;
flag:=true;exit;
end;
if not vis[last[i]] then
begin
fa[last[i]]:=x;len[last[i]]:=w[i];
dfs(last[i]);
end;
i:=next[i];
end;
end; procedure dfs1(x:longint);
var
i:longint;
begin
vis[x]:=true;
i:=first[x];
while i<> do
begin
if not vis[last[i]] then
begin
dfs1(last[i]);
down[x]:=down[x]+w[i]+down[last[i]]/(d[last[i]]-+longint(d[last[i]]=));
end;
i:=next[i];
end;
end; procedure dfs2(x:longint);
var
i:longint;
begin
if (not cir[x]) and (fa[x]<>) then up[x]:=len[x]+(up[fa[x]]+down[fa[x]]-len[x]-down[x]/(d[x]-+longint(d[x]=)))/(d[fa[x]]-+longint(d[fa[x]]=));
vis[x]:=true;
i:=first[x];
while i<> do
begin
if not vis[last[i]] then
begin
fa[last[i]]:=x;len[last[i]]:=w[i];
dfs2(last[i]);
end;
i:=next[i];
end;
end; procedure main;
var
i,x,y,z:longint;
s:double;
begin
read(n,m);cnt:=-;
for i:= to m do
begin
read(x,y,z);
insert(x,y,z);
insert(y,x,z);
end;
if m=n- then
begin
dfs1();
for i:= to n do vis[i]:=false;
dfs2();
for i:= to n do ans:=ans+(up[i]+down[i])/d[i];
end
else
begin
dfs();
for i:= to n do vis[i]:=false;
for i:= to cnt do vis[huan[i]]:=true;
for i:= to cnt do dfs1(huan[i]);
for i:= to cnt do
begin
x:=(i+)mod(cnt+);s:=;
while x<>i do
begin
if (x+)mod(cnt+)=i then up[huan[i]]:=up[huan[i]]+s*len[huan[(x+cnt)mod(cnt+)]]+s*down[huan[x]]/(d[huan[x]]-+longint(d[huan[x]]=))
else up[huan[i]]:=up[huan[i]]+s*len[huan[(x+cnt)mod(cnt+)]]+s*down[huan[x]]/(d[huan[x]]-);
s:=s/(d[huan[x]]-);
x:=(x+)mod(cnt+);
end;
x:=(i+cnt)mod(cnt+);s:=;
while x<>i do
begin
if (x+cnt)mod(cnt+)=i then up[huan[i]]:=up[huan[i]]+s*len[huan[x]]+s*down[huan[x]]/(d[huan[x]]-+longint(d[huan[x]]=))
else up[huan[i]]:=up[huan[i]]+s*len[huan[x]]+s*down[huan[x]]/(d[huan[x]]-);
s:=s/(d[huan[x]]-);
x:=(x+cnt)mod(cnt+);
end;
end;
for i:= to n do vis[i]:=false;
for i:= to cnt do vis[huan[i]]:=true;
for i:= to cnt do dfs2(huan[i]);
for i:= to n do ans:=ans+(up[i]+down[i])/d[i];
end;
writeln(ans/n::);
end; begin
main;
end.

2878: [Noi2012]迷失游乐园 - BZOJ的更多相关文章

  1. BZOJ 2878: [Noi2012]迷失游乐园( 树形dp )

    一棵树的话直接树形dp(求出往下走和往上走的期望长度). 假如是环套树, 环上的每棵树自己做一遍树形dp, 然后暴力枚举(环上的点<=20)环上每个点跑经过环上的路径就OK了. -------- ...

  2. 【BZOJ 2878】 2878: [Noi2012]迷失游乐园 (环套树、树形概率DP)

    2878: [Noi2012]迷失游乐园 Description 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩.进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m ...

  3. bzoj 2878 [Noi2012]迷失游乐园——树上的期望dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2878 很好的树上概率题的思路,就是分成up和down. 代码中有众多小细节.让我弃疗好几天的 ...

  4. bzoj 2878: [Noi2012]迷失游乐园

    #include<iostream> #include<cstring> #include<cstdio> #define M 100005 #define ld ...

  5. bzoj 2878: [Noi2012]迷失游乐园【树上期望dp+基环树】

    参考:https://blog.csdn.net/shiyukun1998/article/details/44684947 先看对于树的情况 设d[u]为点u向儿子走的期望长度和,du[u]为u点的 ...

  6. [bzoj2878][Noi2012]迷失游乐园(基环树dp)

    [bzoj2878][Noi2012]迷失游乐园(基环树dp) bzoj luogu 题意:一颗数或是基环树,随机从某个点开始一直走,不走已经到过的点,求无路可走时的路径长期望. 对于一棵树: 用两个 ...

  7. 【BZOJ 2878】 [Noi2012]迷失游乐园

    Description 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩.进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m条道路的无向连通图,且该图中至多有一个环( ...

  8. NOI2012 : 迷失游乐园

    终于补完NOI2012了好开心~ 题目大意:给定一棵树或者环套外向树,求出从中随机选一条简单路径的期望长度,环上点数不超过20. 设 d[x]表示x的度数,ch[x]表示x孩子个数 up[x]表示x向 ...

  9. 【bzoj2878】 Noi2012—迷失游乐园

    http://www.lydsy.com/JudgeOnline/problem.php?id=2878 (题目链接) 题意 求基环树上以任意点为起点的简单路径期望长度. Solution 啊啊啊好丑 ...

随机推荐

  1. js 引用类型比较

    看到别人提的一个问题,说为什么,他们是fasle 然后自己也测试了一番觉得应该是这样的, var a=[1,2,3,4]; var b=a; var c=[1,2,3,4]; if(a==b){ al ...

  2. 8款强大的CSS3/HTML5动画及应用源码

    1.CSS3 jQuery UI控制滑杆插件 今天我们要来分享一款基于CSS3和jQuery UI的控制滑杆插件,这款控制滑杆是在HTML5滑杆元素的基础上进行自定义CSS3渲染,所以外观更加漂亮.另 ...

  3. Redhat/Centos6.x-Samba配置

    安装: yum -y install samba samba-common samba-client 设置samba帐号 useradd smb passwd smb smbpasswd -a smb ...

  4. 了解GDAL的图像处理/Python

    GDAL是一个操作各种栅格地理数据格式的库.包括读取.写入.转换.处理各种栅格数据格式(有些特定的格式对一些操作如写入等不支持).它使用了一个单一的抽象数据模型就支持了大多数的栅格数据(GIS对栅格, ...

  5. BZOJ 1631==USACO 2007== POJ 3268 Cow Party奶牛派对

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19226   Accepted: 8775 Description One ...

  6. 超越luabind的luaBridge

    此编是引用他人的文章,这里记录下,主要为以后自己查找方便,原文地址:http://www.cppblog.com/sunicdavy/archive/2013/12/07/204648.html 最近 ...

  7. Android下各个按键对应的key code

    KEYCODE_UNKNOWN=0; KEYCODE_SOFT_LEFT=1; KEYCODE_SOFT_RIGHT=2; KEYCODE_HOME=3; KEYCODE_BACK=4; KEYCOD ...

  8. Google面试题:计算从1到n的正数中1出现的次数

    题目: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次. 找工作,准备看写题目,题目说是Goo ...

  9. NSS_08 extjs表单验证

    Extjs做了非常好的表单验证功能, 使用起来非常方便. 系统内置了4种验证功能,分别是alpha, alphanumeric,url, email, 在程序中可以直接使用,(可以结合allowBla ...

  10. 启语外语培训网SEO历程

    网站开发完成已经好长时间了,但收录一直上不去,排名也上不去.我这篇文章主要是分析一下网站的SEO历程 网址:http://waiyupx.cheer-edu.net/School/School-5.h ...