loj2192 「SHOI2014」概率充电器

OI 动态规划
文章目录

就是先算一下每个点在他的子树影响下不连通的概率,然后再考虑父亲的影响。

ref

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n, uu, vv, ww, hea[500005], cnt;
double q[500005], dp[500005];
const double eps=1e-7;
struct Edge{
int too, nxt;
double val;
}edge[1000005];
void add_edge(int fro, int too, double val){
edge[++cnt].nxt = hea[fro];
edge[cnt].too = too;
edge[cnt].val = val;
hea[fro] = cnt;
}
void dfs1(int x, int f){
dp[x] = 1 - q[x];
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t!=f){
dfs1(t, x);
dp[x] *= 1 - (1 - dp[t]) * edge[i].val;
}
}
}
void dfs2(int x, int f, double e){
dp[x] *= e;
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t!=f){
double tmp=1-(1-dp[t])*edge[i].val;
if(fabs(tmp)<eps) tmp = 0;
else tmp=dp[x]/tmp;
dfs2(t, x, 1-(1-tmp)*edge[i].val);
}
}
}
int main(){
cin>>n;
for(int i=1; i<n; i++){
scanf("%d %d %d", &uu, &vv, &ww);
add_edge(uu, vv, ww/100.0);
add_edge(vv, uu, ww/100.0);
}
for(int i=1; i<=n; i++){
scanf("%lf", &q[i]);
q[i] /= 100.0;
}
dfs1(1, 0);
dfs2(1, 0, 1);
double ans=0.0;
for(int i=1; i<=n; i++)
ans += 1 - dp[i];
printf("%.6f\n", ans);
return 0;
}