匈牙利算法

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int G[1003][1003];
int match[1003];
int rb[1004];
int k,mg,nb;
ll n;
bool dfs(int x){
    for(int i=1;i<=nb;i++){
        if(!rb[i]&&G[x][i]){
            rb[i]=1;
            if(!match[i]||dfs(match[i])){
                match[i]=x;
                return true; 
            }
        }
    } 
    return false;
}
ll work(){
    int sum=0;
    nb=n;
    mg=n;
    memset(match,0,sizeof(match));
    for(int i=1;i<=mg;i++){
        memset(rb,0,sizeof(rb));
        if(dfs(i))sum++;
    }
    return sum;
}
void solve(){

    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
        cin>>G[i][j];
    } 
    cout<<work()<<endl;//最大匹配数; 
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
//    freopen("1.in","r",stdin);
//    getlist(1e7);
    int t=1;
//    cin>>t;
    while(t--){
        solve();
    }
}
rush!
原文地址:https://www.cnblogs.com/LH2000/p/14802960.html