SHOI2014 概率充电器(树形DP)(概率DP)(容斥)

13 篇文章 0 订阅
13 篇文章 0 订阅

传送门

好题啊!很明显要从子树算一遍,然后从父亲算一遍

f ( u ) f(u) f(u) 表示直接充上电或从子树充上电的概率
概率合并需要容斥一下 P ( A ∣ B ) = P ( A ) + P ( B ) − P ( A ) ∗ P ( B ) P(A|B)=P(A)+P(B)-P(A)*P(B) P(AB)=P(A)+P(B)P(A)P(B)

然后将 f ( u ) f(u) f(u) 扩展到可以从父亲走过来
显然如果是从父亲走过来不能是自己走上去在走下来

也就是说概率是只考虑父亲以及父亲其他儿子的概率
P ( A ) = P ( A ∣ B ) − P ( B ) 1 − P ( B ) P(A)=\frac{P(A|B)-P(B)}{1-P(B)} P(A)=1P(B)P(AB)P(B) 就是父亲从它的父亲以及其他儿子充上电的概率

然后从父亲走下来合并依然容斥合并

结合考察了树形 d p dp dp 的套路 及 概率 d p dp dp 容斥转移

#include<bits/stdc++.h>
#define cs const
using namespace std;
cs int N = 5e5 + 5;
int read(){
	int cnt = 0, f = 1; char ch = 0;
	while(!isdigit(ch)){ ch = getchar(); if(ch == '-') f = -1; }
	while(isdigit(ch)) cnt = cnt*10 + (ch-'0'), ch = getchar();
	return cnt * f;
}
int first[N], nxt[N << 1], to[N << 1], tot; double w[N << 1];
int n; double a[N], f[N];
void add(int x, int y, double z){nxt[++tot] = first[x], first[x] = tot, to[tot] = y, w[tot] = z; }
void dfs1(int u, int fa){
	f[u] = a[u];
	for(int i = first[u]; i; i = nxt[i]){
		int t = to[i]; if(t == fa) continue;
		dfs1(t, u); double p = w[i] * f[t];
		f[u] = f[u] + p - f[u] * p;
	}
}
void dfs2(int u, int fa){
	for(int i = first[u]; i; i = nxt[i]){
		int t = to[i]; if(t == fa) continue;
		double pa = f[t] * w[i];
		if(pa < 1){
			double pc = (f[u] - pa) / (1 - pa);
			double p = pc * w[i];
			f[t] = f[t] + p - p * f[t];
		} dfs2(t, u);
	}
}
int main(){
	n = read();
	for(int i = 1; i < n; i++){
		int x = read(), y = read(); double z = read(); z = z/100.0;
		add(x, y, z); add(y, x, z);
	} 
	for(int i = 1; i <= n; i++) a[i] = 1.0 * read() / 100.0;
	dfs1(1, 0); dfs2(1, 0); double ans = 0;
	for(int i = 1; i <= n; i++) ans += f[i];
	printf("%.6lf", ans); return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FSYo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值