/**
* Author: Benjamin Qi, chilli
* Date: 2020-04-04
* License: CC0
* Source: https://github.com/bqi343/USACO/blob/master/Implementations/content/graphs%20(12)/Matching/Hungarian.h
* Description: Given a weighted bipartite graph, matches every node on
* the left with a node on the right such that no
* nodes are in two matchings and the sum of the edge weights is minimal. Takes
* cost[N][M], where cost[i][j] = cost for L[i] to be matched with R[j] and
* returns (min cost, match), where L[i] is matched with
* R[match[i]]. Negate costs for max cost. Requires $N \le M$.
* Time: O(N^2M)
* Status: Tested on kattis:cordonbleu, stress-tested
*/#pragma once
pair<ll,vector<int>>hungarian(constvector<vector<ll>>&a){if(a.empty())return{0,{}};intn=a.size()+1,m=a[0].size()+1;vector<ll>u(n),v(m);vector<int>p(m),ans(n-1);for(inti=1;i<n;i++){p[0]=i;intj0=0;// add "dummy" worker 0vector<ll>dist(m,LLONG_MAX);vector<int>pre(m,-1);vector<bool>done(m+1);do{// dijkstradone[j0]=true;inti0=p[j0],j1;lldelta=LLONG_MAX;for(intj=1;j<m;j++)if(!done[j]){autocur=a[i0-1][j-1]-u[i0]-v[j];if(cur<dist[j])dist[j]=cur,pre[j]=j0;if(dist[j]<delta)delta=dist[j],j1=j;}for(intj=0;j<m;j++){if(done[j])u[p[j]]+=delta,v[j]-=delta;elsedist[j]-=delta;}j0=j1;}while(p[j0]);while(j0){// update alternating pathintj1=pre[j0];p[j0]=p[j1],j0=j1;}}for(intj=1;j<m;j++)if(p[j])ans[p[j]-1]=j-1;return{-v[0],ans};// min cost}
Traceback(mostrecentcalllast):File"/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/site-packages/onlinejudge_verify/documentation/build.py",line71,in_render_source_code_statbundled_code=language.bundle(stat.path,basedir=basedir,options={'include_paths':[basedir]}).decode()~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/site-packages/onlinejudge_verify/languages/cplusplus.py",line187,inbundlebundler.update(path)~~~~~~~~~~~~~~^^^^^^File"/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/site-packages/onlinejudge_verify/languages/cplusplus_bundle.py",line312,inupdateraiseBundleErrorAt(path,i+1,"#pragma once found in a non-first line")onlinejudge_verify.languages.cplusplus_bundle.BundleErrorAt:src/flows/hungarian-algorithm.hpp:line15:#pragmaoncefoundinanon-firstline