The Perfect Stall [USACO]

这题还是可以用最大流。只要添加一个源点和一个汇点,并添加源点至牛,汇点至各牧场的边即可转化为标准的最大流

不过,关于二分匹配有专用的算法,匈牙利算法,本质上来说,匈牙利算法应该属于XXX-XXX方法,就是添加增广路径的那个。只不过在二分匹配里面的增广路径是有特点的----它是一个交错路径(从起点算起1..2k+1,第奇数条边为非匹配的边,第偶数条边为匹配过的边)。交错路的搜索可以bfs,也可以dfs。 dfs写起来比较简单,bfs则需要记录前驱从而在搜索成功后回溯匹配过程。

第一次提交时,我用的dfs在test8时超时了,原因与dfs代码中被我注释掉的那句有关。

改了之后提交OK,又顺便试了一下bfs,也没有问题。

 

----------------------------------------------------------------------------------------------------------------------

/*
ID: zhangyc1
LANG: C++
TASK: stall4
*/
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;

ofstream fileout("stall4.out");

const int MAXNODENUM = 201;
int arrLinkL[MAXNODENUM], arrLinkR[MAXNODENUM], arrPre[MAXNODENUM];
bool arrVisited[MAXNODENUM];
int N, M, nCount = 0;

struct SCowStall 
{
    int nSize;
    int arrStalls[MAXNODENUM - 1];
};
SCowStall arrCowStall[MAXNODENUM];

void prepairData()
{
    ifstream filein("stall4.in");
    filein >> N >> M;
    for (int i = 1; i<= N; i++)
    {
        filein >> arrCowStall[i].nSize;
        for (int j = 0; j < arrCowStall[i].nSize; j++)
        {
            filein >> arrCowStall[i].arrStalls[j];
        }
    }
    filein.close();
    memset(arrLinkL, 0, sizeof(arrLinkL));
    memset(arrLinkR, 0, sizeof(arrLinkR));
}

bool dfs(int x)
{
    for (int j = 0; j < arrCowStall[x].nSize; j++)
    {
        int y = arrCowStall[x].arrStalls[j];
        if (!arrVisited[y])
        {
            arrVisited[y] = true;
            if (arrLinkR[y] == 0 || dfs(arrLinkR[y]))
            {
                arrLinkL[x] = y;
                arrLinkR[y] = x;
                return true;
            }
            // 这个很重要,因为在一次子过程在y节点失败,则以后无论怎样搜索路径,到y节点都会失败。可以反证来证明。
            //arrVisited[y] = false;
        }
    }
    return false;
}

bool bfs(int u)
{
    memset(arrPre, 0, sizeof(arrPre));
    queue<int> q;
    q.push(u);
    arrVisited[u] = true;
    bool bFound = false;
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        for (int i = 0; i < arrCowStall[x].nSize; i++)
        {
            int y = arrCowStall[x].arrStalls[i];
            if (arrLinkR[y] == 0)
            {
                // 找到了增广路径
                int yy = y;
                while (arrLinkL[x] != 0)
                {
                    int temp = arrLinkL[x];
                    arrLinkL[x] = yy;
                    arrLinkR[yy] = x;
                    yy = temp;
                    x = arrPre[yy];
                }
                arrLinkL[x] = yy;
                arrLinkR[yy] = x;
                return true;
            }
            // 防止重复
            else if (!arrVisited[arrLinkR[y]])
            {
                arrPre[y] = x;
                q.push(arrLinkR[y]);
                arrVisited[arrLinkR[y]] = true;
            }
        }
    }
    return false;
}

void process()
{
    for (int i = 1; i <= N; i++)
    {
        // 这个条件可以省去,因为每次更新路径只会改变左侧图中1号至当前节点的匹配情况
        //if (arrLinkL[i] == 0)
        {
            memset(arrVisited, 0, sizeof(arrVisited));
            if (bfs(i))
            {
                nCount++;
            }
        }
    }
    fileout << nCount << endl;
}

int main(){
    prepairData();
    process();
    fileout.close();    
    return 0;
}

---------------------------------------------------题目如下------------------------

The Perfect Stall
Hal Burch

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 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.

Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.

PROGRAM NAME: stall4

INPUT FORMAT

Line 1:One line with 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.
Line 2..N+1:N lines, each corresponding 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.

SAMPLE INPUT (file stall4.in)

5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2 

OUTPUT FORMAT

A single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

SAMPLE OUTPUT (file stall4.out)

4

转载于:https://www.cnblogs.com/doublemystery/archive/2013/03/19/2968903.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值