#graph906. 二分图最大权匹配

二分图最大权匹配

Maximum Weight Bipartite Matching

Problem Description

Once upon a time there was a harmonious class, with nln_l boys and nrn_r girls, numbered 1,…,nl1, \dots, n_l and 1,…,nr1, \dots, n_r respectively.

There are several such conditions: the vv-th boy and the uu-th girl are willing to become spouses, and if they do, their happiness is ww.

What is the maximum total happiness in this class?

Input Format

The first line contains three positive integers nl,nr,mn_l, n_r, m.

The following mm lines each contain three integers v,u,wv, u, w, meaning the vv-th boy and the uu-th girl are willing to become spouses, with happiness ww. It is guaranteed that 1≤v≤nl1 \leq v \leq n_l, 1≤u≤nr1 \leq u \leq n_r, and the same pair (v,u)(v, u) does not appear twice.

It is guaranteed that 1≤nl,nr≤4001 \leq n_l, n_r \leq 400, 1≤m≤1600001 \leq m \leq 160000, 1≤w≤1091 \leq w \leq 10^9.

Output Format

The first line outputs one integer: the maximum possible total happiness.

The next line outputs nln_l integers describing one optimal scheme. The vv-th integer is the index of the spouse of boy vv. If boy vv has no spouse, output 00.

Sample Input

2 2 3
1 1 100
1 2 1
2 1 1

Sample Output

100
1 0

Constraints

1≤nl,nr≤4001 \leq n_l, n_r \leq 400, 1≤m≤1600001 \leq m \leq 160000, 1≤w≤1091 \leq w \leq 10^9.

Hint

  • This is the assignment problem / maximum-weight bipartite matching, solvable with the Kuhn-Munkres (Hungarian) algorithm or with a minimum-cost maximum-flow formulation. Output any optimal matching.