BZOJ 3566: [SHOI2014]概率充电器
2016-01-06 14:11
288 查看
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3566
首先这题正着想不好想,考虑补集转化。
先dfs一遍,令f[u]=(1-p[u])*∏(1-(1-f[v])*w) f[u]表示u这个点通过其子树并不能联通的概率。
然后考虑v从其父亲连过来的情况,设x=1-f[u]/(1-(1-f[v])*w)表示除掉v,u联通的概率,那么f[v]*=(1-x*w)
然后加起来就是答案了。
首先这题正着想不好想,考虑补集转化。
先dfs一遍,令f[u]=(1-p[u])*∏(1-(1-f[v])*w) f[u]表示u这个点通过其子树并不能联通的概率。
然后考虑v从其父亲连过来的情况,设x=1-f[u]/(1-(1-f[v])*w)表示除掉v,u联通的概率,那么f[v]*=(1-x*w)
然后加起来就是答案了。
#include<cstring> #include<iostream> #include<algorithm> #include<cstdio> #define maxn 500500 #define rep(i,l,r) for (int i=l;i<=r;i++) #define down(i,l,r) for (int i=l;i>=r;i--) #define clr(x,y) memset(x,y,sizeof(x)) #define eps 1e-8 using namespace std; struct data{int obj,pre; double c; }e[maxn*2]; int head[maxn],tot,n; double f[maxn],ans,p[maxn]; int read(){ int x=0,f=1; char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1; ch=getchar();} while (isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();} return x*f; } void insert(int x,int y,double z){ e[++tot].obj=y; e[tot].c=z; e[tot].pre=head[x]; head[x]=tot; } void dfs(int u,int fa){ f[u]=1-p[u]; for (int j=head[u];j;j=e[j].pre){ int v=e[j].obj; if (v!=fa) { dfs(v,u); f[u]=f[u]*(1-(1-f[v])*e[j].c); } } } void go(int u,int fa){ for (int j=head[u];j;j=e[j].pre){ int v=e[j].obj; if (v!=fa){ double x=1-f[u]/(1-(1-f[v])*e[j].c); if (x>eps&&f[v]>eps)f[v]=f[v]*(1-x*e[j].c); go(v,u); } } } int main(){ n=read(); rep(i,1,n-1){ int x=read(),y=read(),z=read(); insert(x,y,1.0*z/100); insert(y,x,1.0*z/100); } rep(i,1,n) {int x=read(); p[i]=1.0*x/100;} dfs(1,0); go(1,0); rep(i,1,n) ans+=1-f[i]; printf("%.6lf\n",ans); return 0; }
相关文章推荐
- [转]java.sql.SQLException: 无效的列索引
- Java中注解(Anotation)的使用
- android4.4+me909+电信4g卡
- Android开发基础教程 支持不同设备——支持不同(Android)平台版本
- codevs[1160] 蛇形矩阵
- 使用自定义控件
- Spring10种常见异常解决方法
- JS实现Tab切换
- java读取远程url图片,得到宽高
- Linux基础系列(四)系统用户和组管理
- IOS9 最新的分享(QQ、WeiXin、WeiBo)代码书写不超过10行
- Inotify+rsync实现自动化复制存储方案
- 深入理解BootStrap -- 进度条(progressbar)14
- 基于 Jenkins 快速搭建持续集成环境
- Java并发编程:Callable、Future和FutureTask
- 彻底搞定C语言指针详解
- Android5.0以上的项目都会有的按钮点击特效--水波纹
- Yii2 framework学习笔记(二) -- ui结构与美化
- 基于 Jenkins 快速搭建持续集成环境
- java生成压缩图