NOI '12 P4 - Lost in the Park

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 0.6s
Memory limit: 512M

Problem types
National Olympiad in Informatics, China, 2012

The summer holidays are here, and little Z feels too bored at home. So, he decided to go alone to the amusement park. Upon entering, little Z took a look at the map. He noticed that the entire amusement park can be represented as a connected, undirected graph made up of n attractions and m trails joining them. Also, the graph contains at most one cycle (thus, m can either be n or n-1). Little Z's current location (the entrance) also happens to be an attraction. Little Z doesn't know which attractions are fun, so he decided to start from his current location and at each step afterwards, travel to another attraction which is connected to the current attraction by a trail. Furthermore, the same attraction (including the entrance attraction) should not be visited twice. Playful little Z will continue to play and play until the current attraction he's at and all of its adjacent attractions have been visited.

The sequence of attractions little Z visits can be interpreted as a simple path. He wants to know, what is the expected length of this path?

Little Z brought home the map of the amusement park, but forgot which attraction is the entrance. He can only assume that any of the attractions may be the entrance, where every attraction has equal probability to be the entrance. At the same time, every time he selects the next attraction to visit, all of the adjacent, unvisited attractions will have equal probability of being selected.

Input Specification

The first line of input consists of two integers n and m, respectively representing the number of attractions and trails.
For the following m lines, each line will contain three integers X_i, Y_i, and W_i, indicating that the i-th trail connects attractions X_i and Y_i, and has a length of W_i. All of the attractions are numbered from 1 to n, and there will be at most one trail between any two attractions.

Output Specification

Output a single line containing a single real number, the expected length of the path. Your answer will be considered correct if it differs from the correct answer by no more than 0.01.

Sample Input

4 3
1 2 3
2 3 1
3 4 4

Sample Output

6.00000000

Explanation

There are 6 different paths in the sample input.

PathLengthProbability
1 \to 48\frac 1 4
2 \to 13\frac 1 8
2 \to 45\frac 1 8
3 \to 14\frac 1 8
3 \to 44\frac 1 8
4 \to 18\frac 1 4

Thus, the expected length = \frac 8 4 + \frac 3 8 + \frac 5 8 + \frac 4 8 + \frac 4 8 + \frac 8 4 = 6.00.

Constraints

For 100\% of the test cases, 1 \le W_i \le 100.

Test CasenmRemarks
1n = 10m = n-1The graph is guaranteed to be a linked list
2n = 100Only node 1 has a degree larger than 2
3n = 1000/
4n = 100\,000/
5n = 100\,000/
6n = 10m = n/
7n = 100The number of nodes in the cycle \le 5
8n = 1\,000The number of nodes in the cycle \le 10
9n = 100\,000The number of nodes in the cycle \le 15
10n = 100\,000The number of nodes in the cycle \le 20

Problem translated to English by Alex.


Comments

There are no comments at the moment.