Minimum Cycle Mean

Given a weighted simple digraph, find the minimum mean weight over all simple directed cycles, or 0 0 if no cycle exists, and output it as a reduced fraction.

Hard9Dynamic programmingGraphBinary searchShortest pathNo attempts yetTime limit2sMemory limit512 MB

Problem

A metabolic network is modeled as a directed graph. A vertex is a state, and an edge is a transition from one state to another. Each edge carries a weight, such as the cost or the energy that the transition needs. The mean weight of a directed cycle is the total weight of its edges divided by the number of its edges. The efficiency of the network is measured by the smallest mean weight among the directed cycles in the network, and your task is to compute that value.

More precisely, you are given a digraph G=(V,E)G = (V, E) with nn vertices in which every edge has a positive weight. A cycle CC in GG is simple if the vertices along CC are all distinct. The weight w(C)w(C) of a simple directed cycle CC is the total weight of the edges in CC, and the mean weight of CC is w(C)/Cw(C)/|C|, where C|C| is the number of edges of CC, that is, the length of CC. The minimum cycle mean of GG is the smallest mean weight among the simple directed cycles of GG. The digraph GG is simple: no edge goes from a vertex to itself, and for any two distinct vertices uu and vv there is at most one edge from uu to vv. Every simple cycle in GG therefore has length at least two.

Figure 1. A digraph with 6 vertices and 9 edges. The vertices a to f correspond to the id numbers 0 to 5.

The digraph in Figure 1 has four simple directed cycles in total: b to c to b, a to b to c to a, b to d to e to c to b, and a to b to d to e to c to a, of length 2, 3, 4, and 5. Their weights are 4, 6, 6, and 8, so their mean weights are 4/2=24/2 = 2, 6/3=26/3 = 2, 6/4=1.56/4 = 1.5, and 8/5=1.68/5 = 1.6. The smallest mean weight is 1.51.5, reached by the cycle b to d to e to c to b.

Write a program that prints the minimum cycle mean of a simple digraph GG.

Input

The first line contains two integers nn and mm (2n1,0002 \le n \le 1{,}000, 1m1051 \le m \le 10^5), the number of vertices and the number of edges of the digraph GG. The vertices have distinct id numbers from 00 to n1n - 1.

Each of the next mm lines describes one edge with three integers uu, vv, ww separated by a single space. The edge is directed from uu to vv (0u,vn10 \le u, v \le n - 1, uvu \ne v) and has weight ww (1w1,0001 \le w \le 1{,}000).

Output

Print one line with two integers aa and bb separated by a single space, where aa and bb are relatively prime and a/ba/b is the minimum cycle mean of GG. When the minimum cycle mean is an integer, bb is 11. If GG has no cycle, print two zeroes separated by a single space.