SHOI2014 概率充电器
题目链接:戳我
非常不好意思,因为想要排版,所以今天先只把代码贴出来,明天补题解。QAQ
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define eps 1e-7
#define MAXN 500010
int n,m,t;
int head[MAXN],fa[MAXN];
double h[MAXN];
struct Edge{int nxt,to,dis;}edge[MAXN<<1];
inline void add(int from,int to,int dis)
{
edge[++t].nxt=head[from],edge[t].to=to,edge[t].dis=dis;
head[from]=t;
}
inline void dfs1(int x,int pre)
{
fa[x]=pre;
for(int i=head[x];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(v==pre) continue;
dfs1(v,x);
double k=h[v]*(1.0*edge[i].dis/100);
h[x]+=k-k*h[x];
}
}
inline void dfs2(int x,int pre)
{
for(int i=head[x];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(v==fa[x]) continue;
double cur=h[v]*(1.0*edge[i].dis/100);
if(fabs(cur-1.0)>eps)
{
double k=(h[x]-cur)/(1-cur);
k*=1.0*edge[i].dis/100;
h[v]+=k-k*h[v];
}
dfs2(v,x);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&n);
for(int i=1;i<=n-1;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w),add(v,u,w);
}
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
h[i]=1.0*x/100;
}
dfs1(1,0);
dfs2(1,0);
double ans=0;
for(int i=1;i<=n;i++) ans+=h[i];
printf("%.6lf\n",ans);
return 0;
}