POJ1274 The Perfect Stall【二分图最大匹配】

题目链接:

http://poj.org/problem?id=1274

题目大意:

有N头奶牛(编号1~N)和M个牛棚(编号1~M)。每头牛只可产一次奶,每个牛棚也只允许一只牛产奶。

现在给出每头奶牛喜欢去的牛棚的编号。问:最多有多少头奶牛能完成产奶。

思路:

二分图最大匹配问题,建立一个图,用行来表示奶牛,用列来表示牛棚。将奶牛和喜欢去的牛棚编号

连边。然后用匈牙利算法DFS版或BFS版求二分图的最大匹配数即可。这里用了匈牙利算法DFS版。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAXN = 220;

bool Map[MAXN][MAXN];
bool bMask[MAXN];

int NX,NY;
int cx[MAXN],cy[MAXN];

int FindPath(int u)
{
    for(int i = 1; i <= NY; ++i)
    {
        if(Map[u][i] && !bMask[i])
        {
            bMask[i] = 1;
            if(cy[i] == -1 || FindPath(cy[i]))
            {
                cy[i] = u;
                cx[u] = i;
                return 1;
            }
        }
    }
    return 0;
}

int MaxMatch()
{
    int res = 0;
    for(int i = 1; i <= NX; ++i)
        cx[i] = -1;
    for(int i = 1; i <= NY; ++i)
        cy[i] = -1;

    for(int i = 1; i <= NX; ++i)
    {
        if(cx[i] == -1)
        {
            for(int j = 1; j <= NY; ++j)
                bMask[j] = 0;
            res += FindPath(i);
        }
    }
    return res;
}

int main()
{
    int d,id;
    while(~scanf("%d%d",&NX,&NY))
    {
        memset(Map,0,sizeof(Map));

        for(int i = 1; i <= NX; ++i)
        {
            scanf("%d",&id);
            for(int j = 1; j <= id; ++j)
            {
                scanf("%d",&d);
                Map[i][d] = 1;
            }
        }

        printf("%d\n",MaxMatch());
    }

    return 0;
}
时间: 2024-10-15 06:38:26

POJ1274 The Perfect Stall【二分图最大匹配】的相关文章

POJ1274 The Perfect Stall 二分图,匈牙利算法

N头牛,M个畜栏,每头牛只喜欢其中的某几个畜栏,但是一个畜栏只能有一只牛拥有,问最多可以有多少只牛拥有畜栏. 典型的指派型问题,用二分图匹配来做,求最大二分图匹配可以用最大流算法,也可以用匈牙利算法,这里使用匈牙利算法. #include <stdlib.h> #include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string

POJ - 1274 The Perfect Stall 二分图 最大匹配

题目大意:有n头牛,m个牛舍,每个牛舍只能装一头牛.给出每头牛可在的牛舍序号,问最多能有多少头牛能分配到牛舍 解题思路:二分图求最大匹配,牛和牛舍分成两个点集进行匹配 #include<cstdio> #include<vector> #include<cstring> using namespace std; const int N = 210; vector<int> cow[N]; int vis[N], link[N]; int n, m; void

POJ 1274-The Perfect Stall(二分图_最大匹配)

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19272   Accepted: 8737 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

POJ_1274_The Perfect Stall(二分图匹配)

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19908   Accepted: 9009 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)

P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶.上个星期,农夫约翰刚刚收集到了奶牛们的爱好的信息(每头奶牛喜欢在哪些牛栏产奶).一个牛栏只能容纳一头奶牛,当然,一头奶牛只能在一个牛栏中产奶. 给出奶牛们的爱好的信息,计算最大分配方案. 输入输出

POJ1274 The Perfect Stall

Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in

Poj_1274 The Perfect Stall -二分图裸题

题目:给牛找棚,每个棚只能容一只牛,牛在对应的棚才能产奶,问最多能让几只牛产奶. /************************************************ Author :DarkTong Created Time :2016/7/31 10:51:05 File Name :Poj_1274.cpp *************************************************/ //#include <bits/stdc++.h> #inclu

POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17895   Accepted: 8143 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

POJ 1274 The Perfect Stall【二分图最大匹配】

题意:二分图最大匹配 分析:二分图最大匹配 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 205; 8 int n; 9 10 int Link[maxn]; 11 int vis[maxn]; 12 vector<int> G[ma