Hdu 4035 Maze (dp求期望) - 2011 ACM/ICPC 成都赛区网络预选赛 1005

      比赛时看题了,但是没有思路。比赛结束后这题总共通过20+,赛后看这个解题报告,由于博主说得太简洁,而我又是从来没有见过这种dp求数学期望的题,所以研究了好久都木有明白。只有搜索一下【dp求期望】的题目,从简单的开始入手,费了老大功夫,终于搞懂了,于是写下详细解题报告。

如果感觉这个题看不懂,也可以按照我的步骤来看:Poj 2096 --> Zoj 3329 --> Hdu 4035

/**
    dp求期望的题。
    题意:
    有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树,
    从结点1出发,开始走,在每个结点i都有3种可能:
        1.被杀死,回到结点1处(概率为ki)
        2.找到出口,走出迷宫 (概率为ei)
        3.和该点相连有m条边,随机走一条
    求:走出迷宫所要走的边数的期望值。
    
    设 E[i]表示在结点i处,要走出迷宫所要走的边数的期望。E[1]即为所求。
    
    叶子结点:
    E[i] = ki*E[1] + ei*0 + (1-ki-ei)*(E[father[i]] + 1);
         = ki*E[1] + (1-ki-ei)*E[father[i]] + (1-ki-ei);
    
    非叶子结点:(m为与结点相连的边数)
    E[i] = ki*E[1] + ei*0 + (1-ki-ei)/m*( E[father[i]]+1 + ∑( E[child[i]]+1 ) );
         = ki*E[1] + (1-ki-ei)/m*E[father[i]] + (1-ki-ei)/m*∑(E[child[i]]) + (1-ki-ei);
    
    设对每个结点:E[i] = Ai*E[1] + Bi*E[father[i]] + Ci;
    
    对于非叶子结点i,设j为i的孩子结点,则
    ∑(E[child[i]]) = ∑E[j]
                   = ∑(Aj*E[1] + Bj*E[father[j]] + Cj)
                   = ∑(Aj*E[1] + Bj*E[i] + Cj)
    带入上面的式子得
    (1 - (1-ki-ei)/m*∑Bj)*E[i] = (ki+(1-ki-ei)/m*∑Aj)*E[1] + (1-ki-ei)/m*E[father[i]] + (1-ki-ei) + (1-ki-ei)/m*∑Cj;
    由此可得
    Ai =        (ki+(1-ki-ei)/m*∑Aj)   / (1 - (1-ki-ei)/m*∑Bj);
    Bi =        (1-ki-ei)/m            / (1 - (1-ki-ei)/m*∑Bj);
    Ci = ( (1-ki-ei)+(1-ki-ei)/m*∑Cj ) / (1 - (1-ki-ei)/m*∑Bj);
    
    对于叶子结点
    Ai = ki;
    Bi = 1 - ki - ei;
    Ci = 1 - ki - ei;
    
    从叶子结点开始,直到算出 A1,B1,C1;
    
    E[1] = A1*E[1] + B1*0 + C1;
    所以
    E[1] = C1 / (1 - A1);
    若 A1趋近于1则无解...
**/

#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

const int MAXN = 10000 + 5;

double e[MAXN], k[MAXN];
double A[MAXN], B[MAXN], C[MAXN];

vector<int> v[MAXN];

bool search(int i, int fa)
{
    if ( v[i].size() == 1 && fa != -1 )
    {
        A[i] = k[i];
        B[i] = 1 - k[i] - e[i];
        C[i] = 1 - k[i] - e[i];
        return true;
    }

    A[i] = k[i];
    B[i] = (1 - k[i] - e[i]) / v[i].size();
    C[i] = 1 - k[i] - e[i];
    double tmp = 0;
    
    for (int j = 0; j < (int)v[i].size(); j++)
    {
        if ( v[i][j] == fa ) continue;
        if ( !search(v[i][j], i) ) return false;
        A[i] += A[v[i][j]] * B[i];
        C[i] += C[v[i][j]] * B[i];
        tmp  += B[v[i][j]] * B[i];
    }
    if ( fabs(tmp - 1) < 1e-10 ) return false;
    A[i] /= 1 - tmp;
    B[i] /= 1 - tmp;
    C[i] /= 1 - tmp;
    return true;
}

int main()
{
    int nc, n, s, t;

    cin >> nc;
    for (int ca = 1; ca <= nc; ca++)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            v[i].clear();

        for (int i = 1; i < n; i++)
        {
            cin >> s >> t;
            v[s].push_back(t);
            v[t].push_back(s);
        }
        for (int i = 1; i <= n; i++)
        {
            cin >> k[i] >> e[i];
            k[i] /= 100.0;
            e[i] /= 100.0;
        }
        
        cout << "Case " << ca << ": ";
        if ( search(1, -1) && fabs(1 - A[1]) > 1e-10 )
            cout << C[1]/(1 - A[1]) << endl;
        else
            cout << "impossible" << endl;
    }
    return 0;
}


你可能感兴趣的:(c,网络,BI,search)