GS - Going to school
Your family has just moved to small town with simple transportation system: there are N junctions and N - 1 roads connecting the junctions. These roads guarantee that it’s possible to travel between any two junctions. Each road connects two junctions and has a preferred value.
You are new here and not familiar with the roads. So when you stay at a junction which is not your destination, you will choose one of incident roads to walk (even it makes you get farther from your destination). The probability of choosing one road equals to its preferred value divide to total preferred value of all incident roads. For example, if there are three incident roads at current junction with preferred value 1, 2 and 3, the probability of choosing each road is 1/6, 2/6 and 3/6, respectively.
Given the starting junction where your house is and the final junction where is your school, what is the expected number of roads you have to walk to reach the destination?
The input begins with T – number of test cases. For each test case, there will be:
- The first line consists of N, st, en - number of junctions, starting and final junction.
- In next N - 1 lines, each line consists of three positive integers u, v and c indicate that there is a road between junction u and v with preferred value c.
For each test case, print the expected number of roads you have to walk, round to exactly 5 precision digits.
T <= 20
1 <= N <= 15
All numbers in input <= 100
Input 1 3 2 3 1 2 1 2 3 1 Output 3.00000
There are 50% chance of going 2-3 directly; 25% chance of going 2-1-2-3, 12.5% of going 2-1-2-1-2-3 and so on. The result equals 1 × 50% + 3 × 25% + 5 × 12.5% + … = 3
If you are stuck and cannot think of anything, refer to this blog -