#P16633. [GKS 2017 #F] Catch Them All
[GKS 2017 #F] Catch Them All
Problem Description
After the release of Codejamon Go, you, like many of your friends, took to the streets of your city to catch as many of the furry little creatures as you could. The objective of the game is to catch Codejamon that appear around your city by going to their locations. You are wondering how long it would take for you to catch them all!
Your city consists of locations numbered from to . You start at location . There are bidirectional roads (numbered from to ). The -th road connects a pair of distinct locations , and it takes minutes to travel on it in either direction. It is guaranteed that it is possible to reach any other location from location by travelling on one or more roads.
At time , a Codejamon will appear at a uniformly random location other than your current location (which is location at time ). Uniformly random means that the probability that it will appear at each of the locations other than your current location is exactly . The instant that a Codejamon appears, you can immediately start moving towards it. When you arrive at a location containing a Codejamon, you instantly catch it, and then a new Codejamon will instantly appear at a uniformly random location other than your current location, and so on. Notice that only one Codejamon is present at any given time, and you must catch the existing one before the next will appear.
Given the layout of your city, calculate the expected time to catch Codejamon, assuming that you always take the fastest possible route between any two locations.
Input Format
The input starts with one line containing one integer : the number of test cases. test cases follow.
Each test case begins with one line containing integers , and , indicating the number of locations, roads, and Codejamon to catch, respectively.
Then, each test case continues with lines; the i-th of these lines contains three integers , and , indicating that the i-th road is between locations and , and it takes minutes to travel on it in either direction.
Output Format
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the expected time in minutes to catch Codejamon. Your answer will be considered correct if it is within an absolute or relative error of of the correct answer.
4
5 4 1
1 2 1
2 3 2
1 4 2
4 5 1
2 1 200
1 2 5
5 4 2
1 2 1
2 3 2
1 4 2
4 5 1
3 3 1
1 2 3
1 3 1
2 3 1
Case #1: 2.250000
Case #2: 1000.000000
Case #3: 5.437500
Case #4: 1.500000
Hint
In Sample Case #1, there is only one Codejamon for us to catch. With equal probability, it will appear at locations , , , and , which are at distances of , , , and , respectively, from our starting location . So the expected time it will take is minutes.
In Sample Case #2, there are only two locations connected by one road. Every time a Codejamon appears, it will be in the location other than our current one, and we will have to take the road to get there. So we take the road times, taking minutes each time, for a total of minutes.
Sample Case #3 uses the same map as Sample Case #1. There are ordered-pair possibilities for where the two Codejamon will appear, and doing the math yields an expected minutes.
In Sample Case #4, the one Codejamon we need to catch will appear at location or location . If it appears at location , it is better for us to get there in two minutes via the -to- and -to- roads, instead of taking the more time-consuming -to- road. So the expected time taken is minutes.
Limits
.
.
, for all i.
, for all i.
For all i and j with i j, U U and/or V V. (There is at most one road between any two locations.)
It is guaranteed that it is possible to reach any other location from location 1 by travelling on one or more roads.
Small dataset (Test set 1 - Visible)
. .
Large dataset (Test set 2 - Hidden)
. .