【poj 1274 】The Perfect Stall (二分图匹配,匈牙利算法)

二分图匹配(匈牙利算法):
http://blog.csdn.net/dark_scope/article/details/8880547
http://www.cnblogs.com/pony1993/archive/2012/07/25/2607738.html

题意:n牛,m个房子,每个牛都只住在自己想住的房子里面,一个房子只能住一个牛,问最多可以安排多少头牛入住;

分析:匈牙利算法模板
邻接表和邻接矩阵都可以

【邻接表】

#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <sstream>
#include <functional>
#include <algorithm>
using namespace std;

#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define dec(i,n,a) for(int i=n;i>=a;i--)///[n,a]
#define pb push_back
#define fi first
#define se second
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double e=2.718281828459045;
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MOD=258280327;
const int N=1e3+5;
const ll maxn=5e4;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int n,m;
bool vis[N];
int match[N];
vector<int>g[N];
void init()
{
    rep(i,0,n+1) g[i].clear();
}
void addedge(int u,int v)
{
    g[u].push_back(v);
    //g[v].push_back(u);
}
int dfs(int x)
{
    for(int i=0; i<g[x].size(); i++)
    {
        int y=g[x][i];
        if(!vis[y])
        {
            vis[y]=1;
            if(match[y]<0 || dfs(match[y]))
            {
                match[y]=x;
                return true;
            }
        }
    }
    return false;
}
int solve()
{
    int ans=0;
    mem(match,-1);
    for(int i=1; i<=n; i++)
    {
        mem(vis,0);
        if(dfs(i)) ans++;
    }
    return ans;
}
int main()
{
    IO;
    while(cin>>n>>m)
    {
        init();
        for(int i=1; i<=n; i++)
        {
            int cnt,y;
            cin>>cnt;
            while(cnt--)
            {
                cin>>y;
                addedge(i,y);
            }
        }
        cout<<solve()<<endl;
    }
    return 0;
}

【邻接矩阵】

#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <sstream>
#include <functional>
#include <algorithm>
using namespace std;

#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define dec(i,n,a) for(int i=n;i>=a;i--)///[n,a]
#define pb push_back
#define fi first
#define se second
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double e=2.718281828459045;
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MOD=258280327;
const int N=1e3+5;
const ll maxn=5e4;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int n,m;
bool vis[N];
int match[N];
bool a[N][N];
int dfs(int x)
{
    for(int y=1;y<=m;y++)
    {
        if(!vis[y] && a[x][y])
        {
            vis[y]=1;
            if(match[y]<0 || dfs(match[y]))
            {
                match[y]=x;
                return true;
            }
        }
    }
    return false;
}
int solve()
{
    int ans=0;
    mem(match,-1);
    for(int i=1; i<=n; i++)
    {
        mem(vis,0);
        if(dfs(i)) ans++;
    }
    return ans;
}
int main()
{
    IO;
    while(cin>>n>>m)
    {
        mem(a,0);
        for(int i=1; i<=n; i++)
        {
            int cnt,y;
            cin>>cnt;
            while(cnt--)
            {
                cin>>y;
                a[i][y]=1;
            }
        }
        cout<<solve()<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值