POJ 3041 最小不相交路径覆盖 匈牙利算法

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxv=510;
int n,m,v1,v2,ret;
bool mmap[maxv][maxv],visit[maxv];
int link[maxv];
bool dfs(int x){//匈牙利算法
    for(int y=1;y<=v2;y++){
        if(mmap[x][y]&&visit[y]==false){
            visit[y]=true;
            if(link[y]==0||dfs(link[y])){
                link[y]=x;
                return true;
            }
        }
    }
    return false;
}
void solve(){
    for(int x=1;x<=v1;x++){
        memset(visit,false,sizeof visit);
        if(dfs(x))
            ret++;
    }
    cout<<ret<<endl;
}
int main(void){
#ifndef ONLINE_JUDGE
    freopen("E:\\input.txt","r",stdin);
#endif // ONLINE_JUDGE
    cin>>n>>m;
    v1=v2=n;
    int t1,t2;
    for(int i=0;i<m;i++){
        cin>>t1>>t2;
        mmap[t1][t2]=true;
    }
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shadandeajian/article/details/81380906