HDU 4035 maze probability DP + tree DP

Source: Internet
Author: User

Practice: in fact, there is a root tree in the question. At first, I thought there may be multiple connected components.

The data volume is so big, and I read the blog of the great god, and slowly hurt me...

If the current node is a leaf node,

Then DP [x] = 1-E [x]-K [x] + k [x] * DP [1] + sigma (P [I] * DP [fahter [I] );

If not

DP [x] = 1-E [x]-K [x] + k [x] * DP [1] + sigma (P [I] * father [I]) + sigma (P [I] * son [I]);

Because the leaf node does not have son, and in a certain node, his son's faher is himself (nonsense,), then, in the recursion process from the leaf to the root

So DP [x] = (a [x] * DP [Father] + B [x] * DP [1] + C [x]) /(1-sigma (son (A [x]);

 

When I was doing this, # define could make me write an error ,,,

#include<cstdio>#include<cstring>#include<cmath>#define no_no 1e50#define zero(x) fabs(x)<1e-9const int LMT=100003;struct line{    int u,v;    int next;}le[LMT<<1];int next[LMT],all,du[LMT];double x[LMT],y[LMT],z[LMT],e[LMT],k[LMT];void init(void){    memset(next,-1,sizeof(next));    memset(x,0,sizeof(x));    memset(y,0,sizeof(y));    memset(z,0,sizeof(z));    memset(du,0,sizeof(du));    all=0;}void insert(int u,int v){    le[all].u=u;    le[all].v=v;    le[all].next=next[u];    next[u]=all++;}bool dfs(int u,int pre){    int v,xx;    double temy=0;    z[u]+=1-e[u]-k[u];    x[u]+=k[u];    for(xx=next[u];xx!=-1;xx=le[xx].next)        if(le[xx].v!=pre)        {            v=le[xx].v;            if(!dfs(v,u)&&1-k[u]-e[u]>0)return 0;            x[u]+=x[v]*(1-k[u]-e[u])/du[u];            temy+=y[v]*(1-k[u]-e[u])/du[u];            z[u]+=z[v]*(1-k[u]-e[u])/du[u];        }        if(pre!=-1)y[u]+=(1-k[u]-e[u])/du[u];        if(zero((temy-1.0)))return 0;            x[u]/=(1-temy);            y[u]/=(1-temy);            z[u]/=(1-temy);        return 1;}int main(void){    int T,I,i,u,v,n;    scanf("%d",&T);    for(I=1;I<=T;I++)    {        init();        scanf("%d",&n);        for(i=1;i<n;i++)        {            scanf("%d%d",&u,&v);            insert(u,v);            insert(v,u);            du[u]++;du[v]++;        }        for(i=1;i<=n;i++)        {            scanf("%lf%lf",&k[i],&e[i]);            e[i]/=100;            k[i]/=100;        }        printf("Case %d: ",I);        if(!dfs(1,-1)||zero((1.0-x[1])))            printf("impossible");        else            printf("%.6lf",z[1]/(1-x[1]));        printf("\n");    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.