hdu4035 Maze

Posted sssy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu4035 Maze相关的知识,希望对你有一定的参考价值。

题目链接

hdu4035 Maze

题解

f[u]表示在节点u通关的所需的边数期望
转移方程分叶子节点和非叶子点讨论
发现都可以化成f[x]=af[1]+bf[dad]+c的形式
然后推一下系数
还是看这个吧https://www.cnblogs.com/Paul-Guderian/p/7624039.html

代码

#include<cmath> 
#include<cstdio> 
#include<ctime> 
#include<cstring> 
#include<algorithm> 
inline int read() { 
    int x = 0,f = 1; 
    char c = getchar(); 
    while(c < '0' || c > '9')c = getchar(); 
    while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar(); 
    return x * f; 
}
#define eps 1e-9
const int maxn = 100007; 
struct node {
    int v,next; 
} edge[maxn << 1]; 
int head[maxn],num = 0; 
inline void add_edge(int u,int v) { 
    edge[++ num].v = v; edge[num].next = head[u]; head[u] = num; 
} 
int n; 
int a[maxn]; 
double A[maxn],B[maxn],C[maxn]; 
double k[maxn],e[maxn]; 
bool dfs(int x,int fa) { 
    if(!edge[head[x]].next && x != 1) { 
        A[x] = k[x]; 
        B[x] = 1 - k[x] - e[x]; 
        C[x] = 1 - k[x] - e[x]; 
        return true; 
    } 
    double A_ = 0,B_ = 0,C_ = 0; int cnt = 0; 
    for(int i = head[x];i;i = edge[i].next) { 
        int v = edge[i].v; 
        if(++ cnt && v != fa) { 
            if(!dfs(v,x)) return false; 
            A_ += A[v];B_ += B[v],C_ += C[v]; 
        } 
    } 
    if(fabs(1 - (1 - k[x] - e[x]) / cnt * B_) < eps) return false ; 
    A[x] = (k[x] + (1 - k[x] - e[x]) / cnt * A_ ) / (1 - (1 - k[x] - e[x]) / cnt * B_); 
    B[x] = ((1 - k[x] - e[x]) / cnt) / (1 - (1 - k[x] - e[x]) / cnt * B_); 
    C[x] = (1 - k[x] - e[x] + (1 - k[x] - e[x]) / cnt * C_) / (1 - (1 - k[x] - e[x]) / cnt * B_); 
    return true; 
}  
int main() { 
    int Q = read(); 
    for(int t = 1;t <= Q;t += 1) { 
        memset(head,0,sizeof head); 
        num = 0; 
        n = read(); 
        for(int i = 1;i < n;++ i) { 
            int u = read(),v = read(); 
            add_edge(u,v);add_edge(v,u); 
        } 
        for(int i = 1;i <= n;++ i) { 
            scanf("%lf%lf",&k[i],&e[i]); 
            k[i] /= 100,e[i] /= 100; 
        } 
        printf("Case %d : ",t); 
        if(!dfs(1,1) || fabs(1 - A[1]) < eps) { 
            puts("impossible"); continue; 
        } else printf("%.6lf
",C[1] / (1 - A[1])); 
    } 
}

以上是关于hdu4035 Maze的主要内容,如果未能解决你的问题,请参考以下文章

Hdu 4035 Maze(概率DP)

HDU4035 Maze 树形DP期望DP

Maze HDU - 4035(期望dp)

HDU4035Maze

[hdu4035] Maze概率dp 数学期望

HDU 4035:Maze 概率DP求期望(有环)