HDU 4035 maze
Realizing the importance of state transfer and simplifying Equations
Problem resolved from http://blog.csdn.net/morgan_xww/article/details/6776947
/**
DP to find the expected question.
Question:
There are n rooms connected by n-1 tunnels, which actually forms a tree,
Starting from node 1, there are three possibilities for each node I:
1. Killed, return to node 1 (probability is Ki)
2. Find the exit and exit the maze (probability is EI)
3. There are m edges connected to the vertex and a Random Edge is taken.
The expected value of the number of edges to walk out of the maze.
Set E [I] to represent the expected number of edges to walk out of the maze at node I. E [1] is the request.
Leaf node:
E [I] = Ki * E [1] + EI * 0 + (1-ei EI) * (E [Father [I] + 1 );
= Ki * E [1] + (1-0000ei) * E [Father [I] + (1-0000ei );
Non-leaf node: (m is the number of edges connected to the node)
E [I] = Ki * E [1] + EI * 0 + (1-ei EI) /M * (E [Father [I] + 1 + Σ (E [child [I] + 1 ));
= Ki * E [1] + (1-0000ei)/m * E [Father [I] + (1-0000ei)/m * Σ (E [child [I]) + (1-0000ei );
Set each node: E [I] = ai * E [1] + bi * E [Father [I] + ci;
For non-leaf node I, if J is set to the child node of I, then
Σ (E [child [I]) = Σ E [J]
= Σ (AJ * E [1] + Bj * E [Father [J] + cj)
= Σ (AJ * E [1] + Bj * E [I] + cj)
The preceding formula is used.
(1-(1-0000ei)/m * Σ BJ) * E [I] = (KI + (1-0000ei)/m * Σ AJ) * E [1] + (1-0000ei) /M * E [Father [I] + (1-0000ei) + (1-0000ei)/m * Σ CJ;
Therefore
Ai = (KI + (1-0000ei)/m * Sigma AJ)/(1-(1-0000ei)/m * Σ BJ );
Bi = (1-0000ei)/M/(1-(1-0000ei)/m * Σ BJ );
Ci = (1-digit EI) + (1-digit EI)/m * Sigma CJ)/(1-(1-digit EI)/m * Sigma BJ );
For leaf nodes
Ai = Ki;
Bi = 1-ki-EI;
Ci = 1-ki-EI;
Starting from the leaf node until A1, B1, and c1 are calculated;
E [1] = A1 * E [1] + B1 * 0 + C1;
So
E [1] = C1/(1-A1 );
If A1 approaches 1, there is no solution...
**/
1 //#pragma comment(linker,"/STACK:102400000,102400000") 2 #include <map> 3 #include <set> 4 #include <stack> 5 #include <queue> 6 #include <cmath> 7 #include <ctime> 8 #include <vector> 9 #include <cstdio> 10 #include <cctype> 11 #include <cstring> 12 #include <cstdlib> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 #define INF 1e8 17 #define inf (-((LL)1<<40)) 18 #define lson k<<1, L, mid 19 #define rson k<<1|1, mid+1, R 20 #define mem0(a) memset(a,0,sizeof(a)) 21 #define mem1(a) memset(a,-1,sizeof(a)) 22 #define mem(a, b) memset(a, b, sizeof(a)) 23 #define FOPENIN(IN) freopen(IN, "r", stdin) 24 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 25 template<class T> T CMP_MIN(T a, T b) { return a < b; } 26 template<class T> T CMP_MAX(T a, T b) { return a > b; } 27 template<class T> T MAX(T a, T b) { return a > b ? a : b; } 28 template<class T> T MIN(T a, T b) { return a < b ? a : b; } 29 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; } 30 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } 31 32 //typedef __int64 LL; 33 //typedef long long LL; 34 const int MAXN = 10005; 35 const int MAXM = 100005; 36 const double eps = 1e-10; 37 //const LL MOD = 1000000007; 38 39 int T, N; 40 vector<int>v[MAXN]; 41 double k[MAXN], e[MAXN]; 42 double A[MAXN], B[MAXN], C[MAXN]; 43 44 45 void init() 46 { 47 int U, V; 48 scanf("%d", &N); 49 for(int i=0;i<=N;i++) v[i].clear(); 50 for(int i=0;i<N-1;i++) 51 { 52 scanf("%d %d", &U, &V); 53 v[U].push_back(V); 54 v[V].push_back(U); 55 } 56 for(int i=1;i<=N;i++) 57 { 58 scanf("%d %d", &U, &V); 59 k[i] = (double)U / 100.0; 60 e[i] = (double)V / 100.0; 61 } 62 } 63 64 bool DFS(int x, int fa) 65 { 66 A[x] = k[x]; 67 B[x] = (1 - k[x] - e[x]) / v[x].size(); 68 C[x] = 1 - k[x] - e[x]; 69 if(v[x].size() == 1 && x != fa) 70 return true; 71 double temp = 0; 72 for(int i = 0; i < v[x].size() ; i ++ ) 73 { 74 int y = v[x][i]; 75 if(y == fa) continue; 76 if(!DFS(y, x)) return false; 77 A[x] += A[y] * B[x]; 78 C[x] += C[y] * B[x]; 79 temp += B[y] * B[x]; 80 } 81 if(fabs(temp - 1.0) < eps) return false; 82 A[x] = A[x] / (1 - temp); 83 B[x] = B[x] / (1 - temp); 84 C[x] = C[x] / (1 - temp); 85 return true; 86 } 87 88 int main() 89 { 90 //FOPENIN("in.txt"); 91 scanf("%d", &T); 92 for(int t = 1; t <= T; t ++ ) 93 { 94 init(); 95 if( DFS(1, 1) && fabs(A[1] - 1.0) > eps ) 96 { 97 printf("Case %d: %lf\n", t, C[1] / (1 - A[1])); 98 } 99 else100 {101 printf("Case %d: impossible\n", t);102 }103 }104 }