Maze HDU - 4035(期望dp)

时间:2023-04-23 22:00:26
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 很接近时,表示无解。
 /*
.
';;;;;.
'!;;;;;;!;`
'!;|&#@|;;;;!:
`;;!&####@|;;;;!:
.;;;!&@$$%|!;;;;;;!'.`:::::'.
'!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
:!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%` '!%&#########@$!:.
;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%`
`!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&;
:@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@|
;|::;;;;;;;;;;;;|&&$|;;!$@&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%.
`!!;;;;;;;!!!!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':;%$!;;;;!%%%&#&%$&: .|%;:!&%`
!@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&:
'$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&:
|#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$'
|&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:!$@!
`%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
!&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
'%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
.!%%&%'`|$; :|$#%|@#&;%#%.
*/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e4 + ;
const int maxm = 1e5 + ;
const int mod = 1e9 + ;
const ll INF = 1e18 + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
using namespace std; int n, m;
int cas, tol, T; std::vector<int> vec[maxn];
double A[maxn];
double B[maxn];
double C[maxn];
double k[maxn];
double e[maxn]; void init() {
for(int i=; i<=n; i++)
vec[i].clear();
mes(A, );
mes(B, );
mes(C, );
mes(k, );
mes(e, );
} void dfs(int u, int f) {
int len = vec[u].size();
if(len == && u != ) {
A[u] = k[u];
B[u] = C[u] = - k[u] - e[u];
return ;
}
if(A[u] != 0.0)
return ;
for(int i=; i<len; i++) {
int v = vec[u][i];
if(v == f) continue;
dfs(v, u);
}
double tmpa = 0.0, tmpb = 0.0, tmpc = 0.0;
for(int i=; i<len; i++) {
int v = vec[u][i];
if(v == f) continue;
tmpa += A[v];
tmpb += B[v];
tmpc += C[v];
}
double tmp = (1.0 - (1.0 - k[u] - e[u]) / len * tmpb);
A[u] = (k[u] + (1.0 - k[u] - e[u]) / len * tmpa) / tmp;
B[u] = (1.0 - k[u] - e[u]) / len / tmp;
C[u] = (1.0 - k[u] - e[u]) * (tmpc / len + ) / tmp;
} int main() {
int cas = ;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
init();
for(int i=; i<n; i++) {
int u, v;
scanf("%d%d", &u, &v);
vec[u].push_back(v);
vec[v].push_back(u);
}
for(int i=; i<=n; i++) {
scanf("%lf%lf", &k[i], &e[i]);
k[i] /= 100.0;
e[i] /= 100.0;
}
dfs(, -);
printf("Case %d: ", cas++);
if(fabs( - A[]) <= eps) {
printf("impossible\n");
} else {
double ans = C[] / ( - A[]);
printf("%.6f\n", ans);
}
}
return ;
}