GS - Going to school

Your family has just moved to small town with simple transportation system: there are N conjunctions and N - 1 roads connecting the conjunctions. These roads guarantee that it’s possible to travel between any two conjunctions. Each road connects two conjunctions and has a preferred value.

You are new here and not familiar with the roads. So when you stay at a conjunction 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 conjunction 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 conjunction where your house is and the final conjunction 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 conjunctions, starting and final
• In next N - 1 lines, each line consists of three positive integers u, v and c indicate that
there is a road between conjunction 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

Sample input
3 2 3
1 2 1
2 3 1

Sample output

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

Được gửi lên bởi:sieunhan
Thời gian chạy:0.100s-3.134s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ASM64 GOSU NODEJS OBJC PERL6 PYPY RUST SED SQLITE VB.NET
Nguồn bài:Khuc Anh Tuan - ACM Vietnam Practice

© All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.