HDU 4035Maze(概率DP)

时间:2022-05-10 01:19:57

HDU 4035   Maze

体会到了状态转移,化简方程的重要性

题解转自http://blog.csdn.net/morgan_xww/article/details/6776947

/**
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则无解...
**/

 //#pragma comment(linker,"/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e8
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; int T, N;
vector<int>v[MAXN];
double k[MAXN], e[MAXN];
double A[MAXN], B[MAXN], C[MAXN]; void init()
{
int U, V;
scanf("%d", &N);
for(int i=;i<=N;i++) v[i].clear();
for(int i=;i<N-;i++)
{
scanf("%d %d", &U, &V);
v[U].push_back(V);
v[V].push_back(U);
}
for(int i=;i<=N;i++)
{
scanf("%d %d", &U, &V);
k[i] = (double)U / 100.0;
e[i] = (double)V / 100.0;
}
} bool DFS(int x, int fa)
{
A[x] = k[x];
B[x] = ( - k[x] - e[x]) / v[x].size();
C[x] = - k[x] - e[x];
if(v[x].size() == && x != fa)
return true;
double temp = ;
for(int i = ; i < v[x].size() ; i ++ )
{
int y = v[x][i];
if(y == fa) continue;
if(!DFS(y, x)) return false;
A[x] += A[y] * B[x];
C[x] += C[y] * B[x];
temp += B[y] * B[x];
}
if(fabs(temp - 1.0) < eps) return false;
A[x] = A[x] / ( - temp);
B[x] = B[x] / ( - temp);
C[x] = C[x] / ( - temp);
return true;
} int main()
{
//FOPENIN("in.txt");
scanf("%d", &T);
for(int t = ; t <= T; t ++ )
{
init();
if( DFS(, ) && fabs(A[] - 1.0) > eps )
{
printf("Case %d: %lf\n", t, C[] / ( - A[]));
}
else
{
printf("Case %d: impossible\n", t);
}
}
}