Welcome to ZOJ
 Problem Sets Information Select Problem Runs Ranklist
ZOJ Problem Set - 4002
Wooden Bridge

Time Limit: 7 Seconds      Memory Limit: 131072 KB      Special Judge

$N$ islands are floating on the sea, connected by $N-1$ wooden bridges. If we consider each island as a vertex and each bridge as an edge, these $N$ islands and $N-1$ 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 $N-1$ edges.

#### Input

There 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 $N-1$ 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.

#### Output

For 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 Input

1
4
1 2 1
1 3 1
1 4 2

#### Sample Output

2.416666666666667

#### Hint

RouteProbability# of Times to
Cross a Bridge
1 - 21/121
1 - 31/121
1 - 4 - 1 - 21/243
1 - 4 - 1 - 21/243
2 - 1 - 31/82
2 - 1 - 4 - 1 - 31/84
3 - 1 - 21/82
3 - 1 - 4 - 1 - 21/84
4 - 1 - 21/122
4 - 1 - 31/122
4 - 1 - 41/122

Author: LI, Lin
Source: ZOJ Monthly, January 2018
Submit    Status