建议使用 atcoder 自带的费用流 atcoder::mcf_graph,这里给出一个使用例子:https://atcoder.jp/contests/arc122/submissions/57255248。
atcoder::mcf_graph