MinDAG

Time limit: 3s Memory limit: 256MB Input: Output:

Task

An undirected graph with NN nodes and MM edges is given.

Each edge (i,j)(i, j) is assigned two costs, aa and bb. It can be directed from ii to jj with a cost of aa, or from jj to ii with a cost of bb.

Choose a direction for each edge such that the resulting directed graph is acyclic and the sum of the costs is minimized.

Input data

The first line of the input contains two integers, NN and MM, representing the number of nodes and the number of edges in the graph.

The next MM lines each contain four integers, ii, jj, aa, and bb, indicating that there is an edge between nodes ii and jj with the properties described above.

Output data

The first line contains the integer CC, representing the required minimum cost.

Constraints and clarifications

  • 1N241 \leq N \leq 24
  • 1MN(N1)21 \leq M \leq \frac{N * (N - 1)}{2}
  • 1a,b1061 \leq a, b \leq 10^6
  • 1i,jN1 \leq i, j \leq N
  • The input edges do not repeat.
# Score Restrictions
1 8 1N81 \leq N \leq 8
2 21 1N151 \leq N \leq 15
3 24 1N201 \leq N \leq 20
4 7 M=N1M = N - 1 and the graph is connected.
5 12 Each node has an exact degree of 22.
6 28 No additional restrictions.

Example

stdin

3 3
1 2 3 5
1 3 7 3
2 3 5 6

stdout

12

Explanation

The edges can be oriented as follows:

  • 121 \rightarrow 2 with a cost of 33
  • 313 \rightarrow 1 with a cost of 33
  • 323 \rightarrow 2 with a cost of 66

Log in or sign up to be able to send submissions!