#graph906. 二分图最大权匹配
二分图最大权匹配
Maximum Weight Bipartite Matching
Problem Description
Once upon a time there was a harmonious class, with boys and girls, numbered and respectively.
There are several such conditions: the -th boy and the -th girl are willing to become spouses, and if they do, their happiness is .
What is the maximum total happiness in this class?
Input Format
The first line contains three positive integers .
The following lines each contain three integers , meaning the -th boy and the -th girl are willing to become spouses, with happiness . It is guaranteed that , , and the same pair does not appear twice.
It is guaranteed that , , .
Output Format
The first line outputs one integer: the maximum possible total happiness.
The next line outputs integers describing one optimal scheme. The -th integer is the index of the spouse of boy . If boy has no spouse, output .
Sample Input
2 2 3
1 1 100
1 2 1
2 1 1
Sample Output
100
1 0
Constraints
, , .
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.