2878: [Noi2012]迷失游乐园 - BZOJ
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的更多相关文章
- BZOJ 2878: [Noi2012]迷失游乐园( 树形dp )
一棵树的话直接树形dp(求出往下走和往上走的期望长度). 假如是环套树, 环上的每棵树自己做一遍树形dp, 然后暴力枚举(环上的点<=20)环上每个点跑经过环上的路径就OK了. -------- ...
- 【BZOJ 2878】 2878: [Noi2012]迷失游乐园 (环套树、树形概率DP)
2878: [Noi2012]迷失游乐园 Description 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩.进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m ...
- bzoj 2878 [Noi2012]迷失游乐园——树上的期望dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2878 很好的树上概率题的思路,就是分成up和down. 代码中有众多小细节.让我弃疗好几天的 ...
- bzoj 2878: [Noi2012]迷失游乐园
#include<iostream> #include<cstring> #include<cstdio> #define M 100005 #define ld ...
- bzoj 2878: [Noi2012]迷失游乐园【树上期望dp+基环树】
参考:https://blog.csdn.net/shiyukun1998/article/details/44684947 先看对于树的情况 设d[u]为点u向儿子走的期望长度和,du[u]为u点的 ...
- [bzoj2878][Noi2012]迷失游乐园(基环树dp)
[bzoj2878][Noi2012]迷失游乐园(基环树dp) bzoj luogu 题意:一颗数或是基环树,随机从某个点开始一直走,不走已经到过的点,求无路可走时的路径长期望. 对于一棵树: 用两个 ...
- 【BZOJ 2878】 [Noi2012]迷失游乐园
Description 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩.进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m条道路的无向连通图,且该图中至多有一个环( ...
- NOI2012 : 迷失游乐园
终于补完NOI2012了好开心~ 题目大意:给定一棵树或者环套外向树,求出从中随机选一条简单路径的期望长度,环上点数不超过20. 设 d[x]表示x的度数,ch[x]表示x孩子个数 up[x]表示x向 ...
- 【bzoj2878】 Noi2012—迷失游乐园
http://www.lydsy.com/JudgeOnline/problem.php?id=2878 (题目链接) 题意 求基环树上以任意点为起点的简单路径期望长度. Solution 啊啊啊好丑 ...
随机推荐
- DWZ (JUI) 教程 DWZ中dialog层的刷新
在DWZ开发过程中经常会遇到的一种情况就是:在navTab页面中通过a标签打开一个dialog,在dialog层进行操作后,需要对该dialog层进行必要的刷新操作. 1.首先讲一下思路: 在非dia ...
- linux中ftp用户登录密码忘记了怎么修改
先来熟悉一下 vsftp 命令: 启动vsftp用命令: 1.service vsftpd start 重启vsftp用: www.111cn.net1.service vsftpd restart ...
- dataAdapter与dataSet和dataTable的填充
对于dataAdapter与dataSet和dataTable的填充,可以分为1对1,1对n,n对n,3种情况. 以SqlDataAdapter为例. //(1)1对1 SqlDataAdapter ...
- How Do I Declare A Block in Objective-C?
As a local variable: returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...}; As a p ...
- php魔术方法 http_build_query使用
最近在做一个项目使用到 http_build_query 这个魔术方法很好用,它可以将一个数组转换成这样的格式: 比如 $_arr = array('action'=>'show','page' ...
- Android开发代码规范
目录 1.命名基本原则 2.命名基本规范 2.1编程基本命名规范 2.2分类命名规范 3.分类命名规范 3.1基本数据类型命名规范 3.2控件命名规范 3.3变量命名规范 3.4整个项目的目录规范化 ...
- 简单解析依赖注入(控制反转)在Spring中的应用
IoC——Inversion of Control 控制反转DI——Dependency Injection 依赖注入 大家都知道,依赖注入是Spring中非常重要的一种设计模式.可能很多初学者 ...
- iOS开发笔记-两种单例模式的写法
iOS开发笔记-两种单例模式的写法 单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" ...
- 解决ubuntu 14.04 下eclipse 3.7.2 不能启动,报Could not detect registered XULRunner to use 或 org.eclipse.swt.SWTError: XPCOM 等问题的处理
对于eclipse 3.7.2在ubuntu 14.04下不能启动,需要在 eclipse/configuration 目录下的config.ini文件内增加一行org.eclipse.swt.bro ...
- [Apache] 2.2与2.4版本在设置虚拟域名时的小差别
Apache服务器配置虚拟步骤: 1.在httpd.conf中将附加配置文件httpd-vhosts.conf包含进来,接着在httpd-vhosts.conf中写入如下配置: #LoadModule ...