思路:
首先拓扑一遍,然后树上跑两次,一次求向下传,一次求向上传
c o d e code code
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int n, deg[500100], fa[500100];
int head[500100], tot;
struct node
{
int to, next;
double w;
}b[5001000];
double f[500100], g[500100];
void add(int x, int y, double w)
{
b[++tot]=(node){y, head[x], w};
head[x]=tot;
}
double merge_(double &x, double y)
{
return x+y-x*y;
}
void bfs()
{
queue<int> q;
q.push(1);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=head[x]; i; i=b[i].next)
{
int y=b[i].to;
if(y!=fa[x])
{
fa[y]=x;
deg[y]--;
q.push(y);
}
}
}
}
void bfs1()
{
queue<int> q;
for(int i=1; i<=n; i++)
if(!deg[i])
q.push(i);
while(!q.empty())
{
int x=q.front();
q.pop();
deg[fa[x]]--;
if(deg[fa[x]]==0)
q.push(fa[x]);
for(int i=head[x]; i; i=b[i].next)
{
int y=b[i].to;
if(fa[x]!=y)
{
g[y]=f[y]*b[i].w;
f[x]=merge_(f[x], g[y]);
}
}
}
}
void bfs2()
{
queue<int> q;
q.push(1);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=head[x]; i; i=b[i].next)
{
int y=b[i].to;
if(y!=fa[x])
{
if(g[y]<1.0)
f[y]=merge_(f[y], (f[x]-g[y])/(1-g[y])*b[i].w);
q.push(y);
}
}
}
}
int main()
{
freopen("charger.in", "r", stdin);
freopen("charger.out", "w", stdout);
scanf("%d", &n);
for(int i=1; i<n; i++)
{
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
add(x, y, w*1.0/100.0);
add(y, x, w*1.0/100.0);
deg[x]++;
deg[y]++;
}
for(int i=1; i<=n; i++)
{
scanf("%lf", &f[i]);
f[i]/=100.0;
}
bfs();
bfs1();
bfs2();
double ans=0;
for(int i=1; i<=n; i++)
ans+=f[i];
printf("%.6lf", ans);
return 0;
}