Under construction

Still being built. Things may change

Sightseeing Cows

Time limit1sMemory limit128 MB

Summary
Find a directed cycle maximizing the ratio of total fun at its first-visit landmarks to total traversal time, and print that maximum ratio.
Level

Hard8 of 10

Topics
Graph, Binary search, Shortest path, Greedy
Solved
No attempts yet

Problem

Farmer John is rewarding his cows for their hard work with a day trip to the big city, and the cows must decide how to spend their free time.

They have a detailed map of the city showing LL (2≤L≤10002 \le L \le 1000) major landmarks (numbered 11 through LL) and PP (2≤P≤50002 \le P \le 5000) one-way cow paths that connect them. Farmer John drives the cows to a starting landmark of their choice; from there they walk along the paths through a series of landmarks and eventually return to that same starting landmark, where Farmer John picks them up. Because space in the city is scarce, every cow path is narrow and may be walked in one fixed direction only.

The cows may stay in the city as long as they like, but they bore easily: visiting a new landmark is fun, yet walking between landmarks costs time. Every landmark ii has a known fun value FiF_i (1≤Fi≤10001 \le F_i \le 1000).

Cow path ii runs from landmark L1iL1_i to landmark L2iL2_i (in the direction L1i→L2iL1_i \to L2_i) and takes time TiT_i (1≤Ti≤10001 \le T_i \le 1000) to traverse.

To make the most of the day off, the cows want to maximize the average fun value per unit time of their trip. A landmark is only fun the first time it is visited; passing through it again grants no additional fun. Farmer John also insists that the trip visit at least two landmarks so the cows get some exercise.

Help the cows find the maximum average fun per unit time they can achieve.

Input

  • Line 1: two integers LL and PP.
  • Lines 2 to L+1L+1: line i+1i+1 contains the single integer FiF_i.
  • Lines L+2L+2 to L+P+1L+P+1: line L+i+1L+i+1 describes cow path ii with three integers L1iL1_i, L2iL2_i, and TiT_i.

Output

  • One line with the maximum achievable average fun per unit time, truncated (rounded down) to exactly two decimal places. If the cows cannot make any valid round trip, print 0.00.

Hint

In the sample, the trip 1→2→3→5→11 \to 2 \to 3 \to 5 \to 1 collects fun 30+10+10+10=6030 + 10 + 10 + 10 = 60 over time 3+2+2+3=103 + 2 + 2 + 3 = 10, for an average of 6.006.00. The trip 2→3→5→22 \to 3 \to 5 \to 2 averages only 30/6=530 / 6 = 5, and any trip that passes through landmark 44 averages less than 44.

Examples4

  1. Example 1

    Input
    5 7
    30
    10
    10
    5
    10
    1 2 3
    2 3 2
    3 4 5
    3 5 2
    4 5 5
    5 1 3
    5 2 2
    
    Expected output
    6.00
    
  2. Example 2

    Input
    2 2
    10
    20
    1 2 1
    2 1 1
    
    Expected output
    15.00
    
  3. Example 3

    Input
    4 4
    7
    3
    9
    2
    1 2 2
    2 3 2
    3 4 2
    4 1 2
    
    Expected output
    2.62
    
  4. Example 4

    Input
    3 2
    5
    6
    7
    1 2 4
    2 3 4
    
    Expected output
    0.00