HDU 1150 Machine Schedule

http://acm.hdu.edu.cn/showproblem.php?pid=1150

二分图最大匹配,匈牙利算法

View Code
#include <stdio.h>
#include <string.h>
const int MAX1=101;
const int MAX2=1001;
int k,m,n;
int match[MAX1];
int u[MAX2],v[MAX2];
int first[MAX1],next[MAX2];
int vis[MAX1];
int find(int ue)
{
    for(int e=first[ue];e!=-1;e=next[e])
        if(!vis[v[e]])
        {
            vis[v[e]]=1;
            if(match[v[e]]==-1||find(match[v[e]]))
            {
                match[v[e]]=ue;
                return 1;
            }
        }
    return 0;
}
int max_match()
{
    int ans=0;
    memset(match,-1,sizeof(match));
    for(int e=1;e<=m;e++)
    {
        memset(vis,0,sizeof(vis));
        ans+=find(e);
    }
    return ans;
}
void read_graph()
{
    memset(first,-1,sizeof(first));
    for(int e=0;e<k;e++)
    {
        scanf("%*d%d%d",&u[e],&v[e]);
        next[e]=first[u[e]];
        first[u[e]]=e;
    }
}
int main()
{
    while(scanf("%d",&n),n)
    {
        scanf("%d%d",&m,&k); 
        read_graph();
        int ans=max_match();
        printf("%d\n",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xiaohongmao/p/2512001.html