
ZOJ Problem Set  4002
\(N\) islands are floating on the sea, connected by \(N1\) wooden bridges. If we consider each island as a vertex and each bridge as an edge, these \(N\) islands and \(N1\) bridges form a tree. DreamGrid is taking a walk among the islands through the wooden bridges, and he will start his walk on a random island with equal probability. But beware, these bridges are out of repair for long years! Let's denote the durability of the \(i\)th bridge as \(d_i\). Every time DreamGrid passes a bridge (in either direction), its durability will be decreased by 1. If the durability is reduced to 0, the wooden bridge will break into pieces and DreamGrid can never walk across it again. But DreamGrid is busy brainstorming the problems for the next monthly and is not aware of the danger. During his walk, he will select a bridge connecting his current position with equal probability and walk pass it (thus moving to another island). This procedure will be repeated until DreamGrid finds himself stuck on an island. Poor DreamGrid. But we are not interested in rescuing DreamGrid (even if this leads to the postponement of the next monthly). What we are interested in is that, what's the expected number of times he passes a bridge before he is stuck. Please write a program to solve this problem. Recall that a tree is an undirected connected graph with \(N\) vertices and \(N1\) edges. InputThere are multiple test cases. The first line of the input contains an integer \(T\) (\(1 \le T \le 40\)), indicating the number of test cases. For each test case: The first line contains an integer \(N\) (\(1 \le N \le 10\)), indicating the number of islands. The following \(N1\) lines each contains three integers \(u_i\), \(v_i\) and \(d_i\) (\(1 \le u_i, v_i \le n\), \(1 \le d_i \le 15\)), indicating a bridge connecting island \(u_i\) and island \(v_i\) with a durability of \(d_i\). It's guaranteed that the given graph is a tree. OutputFor each test case output one line, indicating the expected number of times DreamGrid passes a bridge before he is stuck. Your answer will be considered correct if its absolute or relative error is not larger than \(10^{9}\). Sample Input1 4 1 2 1 1 3 1 1 4 2 Sample Output2.416666666666667 Hint
Author: LI, Lin Source: ZOJ Monthly, January 2018 