p4284 [SHOI2014]概率充电器
分析
https://www.luogu.org/blog/asuldb/solution-p4284#
代码
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define db long double
#define pb push_back
#define mp make_pair
const db eps = 1e-8;
db h[500100];
int n,m,a[500100];
vector<pair<int,db> >v[500100];
inline void dfs1(int x,int fa){
for(int i=0;i<v[x].size();i++)if(v[x][i].fi!=fa){
int y=v[x][i].fi;dfs1(y,x);db k=h[y]*v[x][i].se;h[x]=h[x]+k-h[x]*k;
}
}
inline void dfs2(int x,int fa){
for(int i=0;i<v[x].size();i++)if(v[x][i].fi!=fa){
int y=v[x][i].fi;db z=v[x][i].se;if(fabs(1.0-h[y]*z)<eps){dfs2(y,x);continue;}
db k=z*(h[x]-h[y]*z)/(1.0-h[y]*z);h[y]=h[y]+k-h[y]*k;dfs2(y,x);
}
}
int main(){
int i,j,k;scanf("%d",&n);
for(i=1;i<n;i++){int x,y,z;scanf("%d%d%d",&x,&y,&z);db t=(db)z/100.0;v[x].pb(mp(y,t)),v[y].pb(mp(x,t));}
for(i=1;i<=n;i++)scanf("%d",&a[i]),h[i]=(db)a[i]/100.0;
dfs1(1,0),dfs2(1,0);db ans=0.0;for(i=1;i<=n;i++)ans+=h[i];printf("%0.6Lf\n",ans);return 0;
}