#P5805. [SEERC 2019] Graph and Cycles

[SEERC 2019] Graph and Cycles

Description

There is an undirected complete graph with nn vertices and edge weights, where nn is odd.

A cycle edge group of size kk is an array of edges [e1,e2,,ek][e_1, e_2, \dots, e_k] that satisfies:

  • kk is greater than 11.
  • For any integer ii in [1,k][1, k], the edge eie_i shares exactly one common endpoint with each of ei1e_{i-1} and ei+1e_{i+1} (where e0=eke_0 = e_k and ek+1=e1e_{k+1} = e_1).

Obviously, the edges in a cycle edge group form a cycle in the graph.

Define a function f(e1,e2)f(e_1, e_2) for two edges e1,e2e_1, e_2 as the larger of their edge weights.

Define the value of a cycle edge group C=[e1,e2,,ek]C = [e_1, e_2, \dots, e_k] as the sum of f(ei,ei+1)f(e_i, e_{i+1}) over all integers ii in [1,k][1, k] (where ek+1=e1e_{k+1} = e_1).

Define a cycle decomposition of a graph as a set of pairwise disjoint cycle edge groups whose union contains all edges of the graph. Define the value of a cycle decomposition as the sum of the values of all cycle edge groups in it.

A graph may have multiple cycle decompositions. Given a graph, your task is to find the cycle decomposition with the minimum value and output this minimum value.

Input Format

The first line contains an integer n (3n999,nmod2=1)n \ (3 \leq n \leq 999, n \bmod 2 = 1), representing the number of vertices in the graph.

The next n(n1)2\frac{n\cdot (n-1)}{2} lines each contain three integers u,vu, v and $w \ (1 \leq u, v \leq n, u \neq v, 1 \leq w \leq 10^9)$, indicating that there is an edge of weight ww between vertex uu and vertex vv.

Output Format

Output one integer, representing the value of the minimum-value cycle decomposition of the given graph.

3
1 2 1
2 3 1
3 1 1
3
5
4 5 4
1 3 4
1 2 4
3 2 3
3 5 2
1 4 3
4 2 2
1 5 4
5 2 4
3 4 2
35

Hint

In the following sample explanations, edges are numbered in input order, and eie_i denotes the ii-th edge in the input order.

In the first sample, the only cycle decomposition is S={[e1,e2,e3]}S = \{ [e_1, e_2, e_3] \}. $f(e_1, e_2) + f(e_2, e_3) + f(e_3, e_1) = 1 + 1 + 1 = 3$.

In the second sample, an optimal cycle decomposition is $S = \{ [e_3, e_8, e_9], [e_2, e_4, e_7, e_{10}, e_5, e_1, e_6] \}$. The value of the cycle edge group [e3,e8,e9][e_3, e_8, e_9] is 1212, and the value of [e2,e4,e7,e10,e5,e1,e6][e_2, e_4, e_7, e_{10}, e_5, e_1, e_6] is 2323, so the value of the cycle decomposition is 3535.

Translated by ChatGPT 5