#P3199. [HNOI2009] 最小圈

    ID: 2248 远端评测题 5000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2009各省省选湖南深度优先搜索,DFS分数规划

[HNOI2009] 最小圈

Description

Consider a weighted directed graph G=(V,E)G=(V,E) and w:ERw: E \rightarrow \mathbb{R}. Each edge e=(i,j)e=(i,j) (iji \neq j, i,jVi, j \in V) has weight wi,jw_{i,j}. Let n=Vn = |V|.

A sequence c=(c1,c2,,ck)c=(c_1, c_2, \cdots, c_k) (ciVc_i \in V) is a cycle in GG if and only if (ci,ci+1)(c_i, c_{i+1}) (1i<k1 \le i < k) and (ck,c1)(c_k, c_1) are both in EE. We call kk the length of cycle cc, and set ck+1=c1c_{k+1} = c_1. The average value of cycle c=(c1,c2,,ck)c=(c_1, c_2, \cdots, c_k) is defined as

$$\mu(c)= \frac 1 k \sum\limits_{i=1}^{k} w_{c_i,c_{i+1}}.$$

Let μ(G)=mincμ(c)\mu'(G)=\min_c \mu(c) be the minimum average value over all cycles cc in GG.

Given G=(V,E)G=(V,E) and w:ERw: E \rightarrow \mathbb{R}, compute the minimum average value μ(G)\mu'(G) over all cycles cc in GG.

Input Format

The first line contains two positive integers nn and mm, separated by a space. Here n=Vn = |V| and m=Em = |E| denote the number of vertices and edges, respectively.

The next mm lines each contain three numbers i,j,wi,ji, j, w_{i,j}, indicating there is an edge (i,j)(i, j) with weight wi,jw_{i,j}. Note that edge weights can be real numbers. The input guarantees that the graph G=(V,E)G=(V,E) is connected, contains at least one cycle, and there exists a vertex that can reach all other vertices.

Output Format

Output a real number μ(G)\mu'(G), printed with 88 digits after the decimal point.

4 5
1 2 5
2 3 5
3 1 5
2 4 3
4 1 3
3.66666667
2 2
1 2 -2.9
2 1 -3.1
-3.00000000

Hint

  • Constraints: For 100%100\% of the testdata, 2n30002 \le n \le 3000, 1m100001 \le m \le 10000, wi,j107|w_{i,j}| \le 10^7, 1i,jn1 \le i, j \le n and iji \neq j.
  • Note: There exists an O(nm)O(nm) solution; an O(nmlogn)O(nm \log n) solution also passes.

Translated by ChatGPT 5