Maze HDU

When wake up, lxhgww find himself in a huge maze. 

The maze consisted by N rooms and tunnels connecting these rooms. Each pair of rooms is connected by one and only one path. Initially, lxhgww is in room 1. Each room has a dangerous trap. When lxhgww step into a room, he has a possibility to be killed and restart from room 1. Every room also has a hidden exit. Each time lxhgww comes to a room, he has chance to find the exit and escape from this maze. 

Unfortunately, lxhgww has no idea about the structure of the whole maze. Therefore, he just chooses a tunnel randomly each time. When he is in a room, he has the same possibility to choose any tunnel connecting that room (including the tunnel he used to come to that room). 
What is the expect number of tunnels he go through before he find the exit? 
InputFirst line is an integer T (T ≤ 30), the number of test cases. 

At the beginning of each case is an integer N (2 ≤ N ≤ 10000), indicates the number of rooms in this case. 

Then N-1 pairs of integers X, Y (1 ≤ X, Y ≤ N, X ≠ Y) are given, indicate there is a tunnel between room X and room Y. 

Finally, N pairs of integers Ki and Ei (0 ≤ Ki, Ei ≤ 100, Ki + Ei ≤ 100, K1 = E1 = 0) are given, indicate the percent of the possibility of been killed and exit in the ith room. 
OutputFor each test case, output one line “Case k: ”. k is the case id, then the expect number of tunnels lxhgww go through before he exit. The answer with relative error less than 0.0001 will get accepted. If it is not possible to escape from the maze, output “impossible”. 
Sample Input
3
3
1 2
1 3
0 0
100 0
0 100
3
1 2
2 3
0 0
100 0
0 100
6
1 2
2 3
1 4
4 5
4 6
0 0
20 30
40 30
50 50
70 10
20 60
Sample Output
Case 1: 2.000000
Case 2: impossible
Case 3: 2.895522

类似的一题:hdu3853.
这题中给出的边是无向的,所以状态可以转移到1, fa[i], son[i], 三个地方。
令 dp[i] 表示从 i 位置走出迷宫的期望。
那么对于叶子结点:
dp[i] = k[i] * dp[1] + (1 - k[i] - e[i]) * (dp[fa[i]] + 1)
对于非叶子结点: len 表示 和结点 i 有关的边数, j 表示 i 的儿子节点
dp[i] = k[i] * dp[1] + (1 - k[i] - e[i]) / len * (dp[fa[i]] + 1 + Σ(dp[j] + 1))

dp[i] = A[i] * dp[1] + B[i] * dp[fa[i]] + C[i]
Σdp[j] = Σ(A[j] * dp[1] + B[j] * dp[i] + C[j])
代入非叶子结点的 dp[i] 中
dp[i] = k[i] * dp[1] + (1 - k[i] - e[i]) / len * (dp[fa[i]] + Σ(A[j] * dp[1] + B[j] * dp[i] + C[j])) + (1 - k[i] - e[i])
   = (k[i] + (1 - k[i] - e[i]) / len * ΣA[j] * dp[1]
   + (1 - k[i] - e[i]) / len * dp[fa[i]]
   + (1 - k[i] - e[i]) / len * ΣB[j] * dp[i]
   + (1 - k[i] - e[i]) / len * ΣC[j] + (1 - k[i] - e[i])
移项,合并同类项得
(1 - (1 - k[i] - e[i]) / len * ΣB[j])dp[i] = (k[i] + (1 - k[i] - e[i]) / len * ΣA[j] * dp[1]
                      + (1 - k[i] - e[i]) / len * dp[fa[i]]
                      + (1 - k[i] - e[i]) *(ΣC[j] / len + 1)
然后通过这个式子推出A[1], B[1], C[1]
要求的是 dp[1], 代入一开始设的式子
dp[1] = A[1] * dp[1] + C[1]
dp[1] = C[1] / (1 - A[1])
当A[1] 和 1 很接近时,表示无解。
  1 /*
  2           .
  3          ';;;;;.
  4         '!;;;;;;!;`
  5        '!;|&#@|;;;;!:
  6       `;;!&####@|;;;;!:
  7      .;;;!&@$$%|!;;;;;;!'.`:::::'.
  8      '!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
  9      :!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%`    '!%&#########@$!:.
 10      ;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
 11      ;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%`
 12     `!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&;
 13     :@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@|
 14     ;|::;;;;;;;;;;;;|&&$|;;!$@&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%.
 15    `!!;;;;;;;!!!!;;;;;$@@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@|.
 16 %&&$!;;;;;!;;;;;;;;;;;|$&&&&&&&&&@@%!%%;!||!;;;;;;;;;;;;;$##!
 17 !%;;;;;;!%!:;;;;;;;;;;!$&&&&&&&&&&@##&%|||;;;!!||!;;;;;;;$&:
 18 ':|@###%;:;;;;;;;;;;;;!%$&&&&&&@@$!;;;;;;;!!!;;;;;%&!;;|&%.
 19  !@|;;;;;;;;;;;;;;;;;;|%|$&&$%&&|;;;;;;;;;;;;!;;;;;!&@@&'
 20   .:%#&!;;;;;;;;;;;;;;!%|$$%%&@%;;;;;;;;;;;;;;;;;;;!&@:
 21   .%$;;;;;;;;;;;;;;;;;;|$$$$@&|;;;;;;;;;;;;;;;;;;;;%@%.
 22     !&!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|@#;
 23      `%$!;;;;;;;;;;;$@|;;;;;;;;;;;;;;;;;;;;;;;;!%$@#@|.
 24        .|@%!;;;;;;;;;!$&%||;;;;;;;;;;;;;;;;;!%$$$$$@#|.
 25            ;&$!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;%#####|.
 26            |##$|!;;;;;;::'':;;;;;;;;;;;;;!%$$$@#@;
 27           ;@&|;;;;;;;::'''''':;;;;;;;|$&@###@|`
 28         .%##@|;;;;:::''''''''''::;!%&##$'
 29       `$##@$$@@&|!!;;;:'''''::::;;;;;|&#%.
 30     ;&@##&$%!;;;;;;::''''''''::;!|%$@#@&@@:
 31      .%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%.
 32     :@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;`
 33      `%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&'
 34      |##&%!;;;;;::''''''''''''::;;;;;;;!$@&:`!'
 35     :;!@$|;;;;;;;::''''''''''':;;;;;;;;!%&@$:                 !@#$'
 36       |##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%:              '%%!%&;
 37       |&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||`            '%$|!%&;
 38       |@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&:           .|%;:!&%`
 39       !@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$;          `%&%!!$&:
 40       '$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##;         !$!;:!$&:
 41        |#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%'      `%$|%%;|&$'
 42         |&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&;  .:%&$!;;;:!$@!
 43         `%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
 44         !&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
 45        '%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
 46        .!%%&%'`|$;       :|$#%|@#&;%#%.
 47 */
 48 #include <map>
 49 #include <set>
 50 #include <list>
 51 #include <ctime>
 52 #include <cmath>
 53 #include <stack>
 54 #include <queue>
 55 #include <string>
 56 #include <vector>
 57 #include <cstdio>
 58 #include <bitset>
 59 #include <cstdlib>
 60 #include <cstring>
 61 #include <iostream>
 62 #include <algorithm>
 63 #define  lowbit(x)  x & (-x)
 64 #define  mes(a, b)  memset(a, b, sizeof a)
 65 #define  fi         first
 66 #define  se         second
 67 #define  pii        pair<int, int>
 68 #define  INOPEN     freopen("in.txt", "r", stdin)
 69 #define  OUTOPEN    freopen("out.txt", "w", stdout)
 70 
 71 typedef unsigned long long int ull;
 72 typedef long long int ll;
 73 const int    maxn = 1e4 + 10;
 74 const int    maxm = 1e5 + 10;
 75 const int    mod  = 1e9 + 7;
 76 const ll     INF  = 1e18 + 100;
 77 const int    inf  = 0x3f3f3f3f;
 78 const double pi   = acos(-1.0);
 79 const double eps  = 1e-10;
 80 using namespace std;
 81 
 82 int n, m;
 83 int cas, tol, T;
 84 
 85 std::vector<int> vec[maxn];
 86 double A[maxn];
 87 double B[maxn];
 88 double C[maxn];
 89 double k[maxn];
 90 double e[maxn];
 91 
 92 void init() {
 93     for(int i=0; i<=n; i++)
 94         vec[i].clear();
 95     mes(A, 0);
 96     mes(B, 0);
 97     mes(C, 0);
 98     mes(k, 0);
 99     mes(e, 0);
100 }
101 
102 void dfs(int u, int f) {
103     int len = vec[u].size();
104     if(len == 1 && u != 1) {
105         A[u] = k[u];
106         B[u] = C[u] = 1 - k[u] - e[u];
107         return ;
108     }
109     if(A[u] != 0.0)
110         return ;
111     for(int i=0; i<len; i++) {
112         int v = vec[u][i];
113         if(v == f)    continue;
114         dfs(v, u);
115     }
116     double tmpa = 0.0, tmpb = 0.0, tmpc = 0.0;
117     for(int i=0; i<len; i++) {
118         int v = vec[u][i];
119         if(v == f)    continue;
120         tmpa += A[v];
121         tmpb += B[v];
122         tmpc += C[v];
123     }
124     double tmp = (1.0 - (1.0 - k[u] - e[u]) / len * tmpb);
125     A[u] = (k[u] + (1.0 - k[u] - e[u]) / len * tmpa) / tmp;
126     B[u] = (1.0 - k[u] - e[u]) / len / tmp;
127     C[u] = (1.0 - k[u] - e[u]) * (tmpc / len + 1) / tmp;
128 }
129 
130 int main() {
131     int cas = 1;
132     scanf("%d", &T);
133     while(T--) {
134         scanf("%d", &n);
135         init();
136         for(int i=1; i<n; i++) {
137             int u, v;
138             scanf("%d%d", &u, &v);
139             vec[u].push_back(v);
140             vec[v].push_back(u);
141         }
142         for(int i=1; i<=n; i++) {
143             scanf("%lf%lf", &k[i], &e[i]);
144             k[i] /= 100.0;
145             e[i] /= 100.0;
146         }
147         dfs(1, -1);
148         printf("Case %d: ", cas++);
149         if(fabs(1 - A[1]) <= eps) {
150             printf("impossible
");
151         } else {
152             double ans = C[1] / (1 - A[1]);
153             printf("%.6f
", ans);
154         }
155     }
156     return 0;
157 }
View Code
 
原文地址:https://www.cnblogs.com/Jiaaaaaaaqi/p/10090013.html