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

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/feng_zhiyu/article/details/79309746

二分图匹配(匈牙利算法):
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;
}
展开阅读全文

The Perfect Stall

10-12

DescriptionnnFarmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall. nGiven the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible. nInputnnThe input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.nOutputnnFor each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.nSample Inputnn5 5n2 2 5n3 2 3 4n2 1 5n3 1 2 5n1 2 nSample Outputnn4 问答

没有更多推荐了,返回首页