#P6577. 【模板】二分图最大权完美匹配

    ID: 5562 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>Special JudgeO2优化基础算法广度优先搜索,BFSKM算法其它技巧

【模板】二分图最大权完美匹配

Description

Given a bipartite graph, both the left part and the right part have nn vertices. There are mm weighted edges, and it is guaranteed that a perfect matching exists.

Find a perfect matching such that the sum of the weights of the matched edges is maximized.

Input Format

The first line contains two integers n,mn, m, with the meaning described above.

Lines 2m+12 \sim m + 1 each contain three integers y,c,hy, c, h, describing an edge from vertex yy on the left part to vertex cc on the right part, with edge weight hh.

Output Format

This problem has a Special Judge.

The first line contains an integer ansans, the answer.

The second line contains nn integers a1,a2,a3ana_1, a_2, a_3 \cdots a_n, where aia_i is the index of the left-part vertex that is matched with the ii-th vertex on the right part in the perfect matching. If there are multiple solutions, output any one of them.

5 7
5 1 19980600
4 2 19980587
1 3 19980635
3 4 19980559
2 5 19980626
1 2 -15484297
4 5 -17558732

99903007
5 4 1 3 2 

Hint

Constraints

  • For 10%10\% of the testdata, n10n \leq 10.
  • For 30%30\% of the testdata, n100n \leq 100.
  • For 60%60\% of the testdata, n500n \leq 500, and the testdata is guaranteed to be random.
  • For 100%100\% of the testdata, 1n5001 \leq n \leq 500, 1mn21 \leq m \leq n^2, 19980731h19980731-19980731 \leq h \leq 19980731. It is also guaranteed that there are no multiple edges.

The testdata was produced after a long time by a problem setter who is “good at causing failures”.

The kind “Yangcunhua” reminds you: do not forget to carefully check the range of edge weights.

The kind “Yangcunhua” reminds you again: your time complexity may only “look” correct.

Translated by ChatGPT 5