National Olympiad in Informatics, China, 2012

Day 2, Problem 1 - Lost in the Park

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 Format

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 Xi, Yi, and Wi, indicating that the i-th trail connects attractions Xi and Yi, and has a length of Wi. All of the attractions are numbered from 1 to n, and there will be at most one trail between any two attractions.

Output Format

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



There are 6 different paths in the sample input.

1 → 481/4
2 → 131/8
2 → 451/8
3 → 141/8
3 → 441/8
4 → 181/4

Thus, the expected length = 8/4 + 3/8 + 5/8 + 4/8 + 4/8 + 8/4 = 6.00


For 100% of the test cases, 1 ≤ Wi ≤ 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 = 100000/
5n = 100000/
6n = 10m = n/
7n = 100The number of nodes in the cycle ≤ 5
8n = 1000The number of nodes in the cycle ≤ 10
9n = 100000The number of nodes in the cycle ≤ 15
10n = 100000The number of nodes in the cycle ≤ 20

All Submissions
Best Solutions

Point Value: 20 (partial)
Time Limit: 1.00s
Memory Limit: 512M
Added: Aug 13, 2014

Languages Allowed:

Comments (Search)

It's quiet in here...