SHOI2014 충전기 확률합니다 (DP 트리)합니다 (DP의 확률 ()을 포함 및 제외)

좋은 질문 아! 그것은 다시 하위 트리 카운트에서 분명, 그의 아버지로부터 다시 계산

만들기 에프 ( ) 부) 확률의 서브 트리로부터의 전기 또는 전기 전하에 직접적인 전하를 나타내는
합병 확률 개재물 제외 필요 무엇 ( | ) = ( ) + ( ) - ( ) * ( ) P (A | B) = P (A) + P (B) -P (A) * P (B)

그때 에프 ( ) 부) 확장에 아버지로부터 전달되는
이 내려가 자신에 착수하지 아버지로부터 온 경우 분명히

그 확률은 유일한 아버지를 고려 확률과 아버지의 다른 아들
( ) = ( | ) - ( ) 1 - ( ) P (A) = \ FRAC {P (A | B) -P (B)} {1-P (B)} 그것은 가능성에 아들의 아버지와 다른 전하의 아버지

그런 아버지의 합병 스틸 포함 및 제외 합병 아래로 이동

트리의 결합 효과 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;
}
게시 된 610 개 원래 기사 · 원 찬양 94 ·은 50000 +를 볼

추천

출처blog.csdn.net/sslz_fsy/article/details/103497630