#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 NN locations numbered from 11 to NN. You start at location 11. There are MM bidirectional roads (numbered from 11 to MM). The ii-th road connects a pair of distinct locations (Ui,Vi)(U_i, V_i), and it takes DiD_i minutes to travel on it in either direction. It is guaranteed that it is possible to reach any other location from location 11 by travelling on one or more roads.

At time 00, a Codejamon will appear at a uniformly random location other than your current location (which is location 11 at time 00). Uniformly random means that the probability that it will appear at each of the N1N - 1 locations other than your current location is exactly 1/(N1)1 / (N - 1). 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 PP 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 TT: the number of test cases. TT test cases follow.

Each test case begins with one line containing 33 integers NN, MM and PP, indicating the number of locations, roads, and Codejamon to catch, respectively.

Then, each test case continues with MM lines; the i-th of these lines contains three integers UiU_i, ViV_i and DiD_i, indicating that the i-th road is between locations UiU_i and ViV_i, and it takes DiD_i 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 PP Codejamon. Your answer will be considered correct if it is within an absolute or relative error of 10410^{-4} 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 22, 33, 44, and 55, which are at distances of 11, 33, 22, and 33, respectively, from our starting location 11. So the expected time it will take is (1+3+2+3)/4=2.25(1 + 3 + 2 + 3) / 4 = 2.25 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 200200 times, taking 55 minutes each time, for a total of 10001000 minutes.

Sample Case #3 uses the same map as Sample Case #1. There are 1616 ordered-pair possibilities for where the two Codejamon will appear, and doing the math yields an expected 87/16=5.437587/16 = 5.4375 minutes.

In Sample Case #4, the one Codejamon we need to catch will appear at location 22 or location 33. If it appears at location 22, it is better for us to get there in two minutes via the 11-to-33 and 33-to-22 roads, instead of taking the more time-consuming 11-to-22 road. So the expected time taken is (2+1)/2=1.5(2 + 1) / 2 = 1.5 minutes.

Limits

1T1001 \le T \le 100.

N1M(N×(N1))/2N - 1 \le M \le (N \times (N - 1)) / 2.

1Di101 \le D_i \le 10, for all i.

1Ui<ViN1 \le U_i < V_i \le N, for all i.

For all i and j with i \neq j, Ui_i \neq Uj_j and/or Vi_i \neq Vj_j. (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)

2N502 \le N \le 50. 1P2001 \le P \le 200.

Large dataset (Test set 2 - Hidden)

2N1002 \le N \le 100. 1P1091 \le P \le 10^9.