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.

OUTPUT FORMAT

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

描述

农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术。不幸的是,由于工程问题,每个牛栏都不一样。第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶。上个星期,农夫约翰刚刚收集到了奶牛们的爱好的信息(每头奶牛喜欢在哪些牛栏产奶)。一个牛栏只能容纳一头奶牛,当然,一头奶牛只能在一个牛栏中产奶。

给出奶牛们的爱好的信息,计算最大分配方案。

[编辑]格式

PROGRAM NAME: stall4

INPUT FORMAT:

(file stall4.in)

第一行 两个整数,N (0 <= N <= 200) 和 M (0 <= M <= 200) 。N 是农夫约翰的奶牛数量,M 是新牛棚的牛栏数量。

第二行到第N+1行 一共 N 行,每行对应一只奶牛。第一个数字 (Si) 是这头奶牛愿意在其中产奶的牛栏的数目 (0 <= Si <= M)。后面的 Si 个数表示这些牛栏的编号。牛栏的编号限定在区间 (1..M) 中,在同一行,一个牛栏不会被列出两次。

OUTPUT FORMAT:

(file stall4.out)

只有一行。输出一个整数,表示最多能分配到的牛栏的数量.

[编辑]SAMPLE
INPUT

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

[编辑]SAMPLE
OUTPUT

4

-------------------------------------------------分割线---------------------------------------------------

周围一群大牛说是二分图的最大匹配,于是匈牙利算法应声而出。

然而我对这短小精悍的程序抱有一丝怀疑。以下为代码:

#include<iostream>
#include<cstring>
using namespace std;
int map[105][105];
int visit[105],flag[105];
int n,m;
bool dfs(int a) {
    for(int i=1;i<=n;i++) {
        if(map[a][i] && !visit[i]) {
            visit[i]=1;
            if(flag[i]==0 || dfs(flag[i])) {
                flag[i]=a;
                return true;
            }
        }
    }
    return false;
}
int main() {
    while(cin>>n>>m) {
        memset(map,0,sizeof(map));
        for(int i=1;i<=m;i++) {
            int x,y;
            cin>>x>>y;
            map[x][y]=1;
        }
        memset(flag,0,sizeof(flag));
        int result=0;
        for(int i=1;i<=n;i++) {
            memset(visit,0,sizeof(visit));
            if(dfs(i)) result++;
        }
        cout<<result<<endl;
    }
    return 0;
}

正当我再研究这神奇的算法时,LGS大神路过#$%@^&*。

在他的指导下,我学会了用网络流(呵呵,也是现学的,dinic不太会)来构建这种二分图的匹配。

     我们设左侧蓝点是牛,右侧红点是待匹配的牛栏。

那么我们虚设一个源点和汇点,并且设每条边(包括和源点、汇点相连的边)的权是1.

我们从源点出发,求出去汇点的最大流,那么这个最大流一定是最佳匹配。

以下是代码:(这个网络流模板我是用bfs写的)

/*
PROG:stall4
ID:juan1973
LANG:C++
*/
#include <cstdio>
#include <algorithm>
#include <memory.h>
using namespace std;
int n,m,tot,flow,cnt,aug,v,p,q,i,j,u;
int map[505][505],queue[20005],pre[505];
int main()
{
    freopen("stall4.in","r",stdin);
    freopen("stall4.out","w",stdout);
    memset(map,0,sizeof(map));
    scanf("%ld%ld",&n,&m);
    for(i=1;i<=n;i++)
    {
            scanf("%ld",&p);
            for (j=1;j<=p;j++)
            {
              scanf("%ld",&q);
              map[i][q+n]=1;
            }
    }
    flow=0;cnt=n+m+1;
    for (i=1;i<=n;i++) map[0][i]=1;
    for (i=n+1;i<=m+n;i++) map[i][cnt]=1;
    memset(queue,0,sizeof(queue));
    while(1)
    {
            memset(pre,-1,sizeof(pre));
            queue[1]=0;
            for(p=1,q=1;p<=q;p++)
            {
                          u=queue[p];
                          for(v=1;v<=cnt;v++)
                          if(pre[v]<0&&map[u][v]>0)
                          {
                                      pre[v]=u;
                                      queue[++q]=v;
                          }
                          if(pre[cnt]>=0)break;
            }
            if(pre[cnt]<0)break;
            aug=2000000000;
            for(v=cnt;v!=0;v=pre[v])aug=min(aug,map[pre[v]][v]);
            for(v=cnt;v!=0;v=pre[v])
            {
                    map[pre[v]][v]-=aug;
                    map[v][pre[v]]+=aug;
            }
            flow+=aug;
    }
    printf("%ld\n",flow);
    return 0;
}

usaco training 4.2.2 The Perfect Stall 最佳牛栏 题解的更多相关文章

  1. USACO Section 4.2 The Perfect Stall(二分图匹配)

    二分图的最大匹配.我是用最大流求解.加个源点s和汇点t:s和每只cow.每个stall和t 连一条容量为1有向边,每只cow和stall(that the cow is willing to prod ...

  2. USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

    The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...

  3. POJ1274 The Perfect Stall[二分图最大匹配]

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23911   Accepted: 106 ...

  4. poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16396   Accepted: 750 ...

  5. POJ1274 The Perfect Stall

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25739   Accepted: 114 ...

  6. POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23911   Accepted: 106 ...

  7. poj 1274 The Perfect Stall (二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 810 ...

  8. poj——1274 The Perfect Stall

    poj——1274   The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25709   A ...

  9. poj —— 1274 The Perfect Stall

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26274   Accepted: 116 ...

随机推荐

  1. phpmyadmin上传sql文件大小限制问题解决方案

    近几天在学生做项目时,需要使用phpmyadmin把本地数据库导入到线上数据库,有许多学生遇到了因为文件过大而上传失败的问题.今天给大家整理一下使用phpmyadmin遇到因为文件过大而导致上传失败问 ...

  2. 底层算法系列:Paxos算法

    关于算法,面太广.本系列只研究实际应用中遇到的核心算法.了解这些算法和应用,对java码农进阶是很有必要的. 对于Paxos学习论证过程中,证实一句话:有史以来学习paxos最好的地方wiki:Pax ...

  3. 开涛spring3(5.1&5.2) - Spring表达式语言 之 5.1 概述 5.2 SpEL基础

    5.1  概述 5.1.1  概述 Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,类似于Struts2x中使用的OGNL表达式语言,能在运行 ...

  4. java集合(2)- java中HashMap详解

    java中HashMap详解 基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 H ...

  5. zabbix监控Elasticsearch集群

    本节以 zabbix 为例,介绍如何使用监控系统完成 Elasticsearch 的监控报警. github 上有好几个版本的 ESZabbix 仓库,都源自 Elastic 公司员工 unterge ...

  6. jenkins+ant+jmeter html报告文件作为附件发送(ant-jmeter支持javamail)

    前言:由于ant-jmeter目前的版本不支持javamail,也就是说发送邮件时只能借助jenkins自带的发送邮件插件来发送报告. 但是jenkins发送邮件支持发送邮件内容(且有价值.有营养的内 ...

  7. DDD领域驱动之干货(三)完结篇!

    首先这里发一下结构图,因为是重写的,但是代码都是一样所有如下: 这里我先说一下看了大部分的DDD文章都是采用的WCF做服务,这里呢我用的是webapi做服务,WCF和WEBAPI的区别可以去百度下. ...

  8. 使用Windows Server 2012+ 搭建VPN 简单 高效 稳定

    前几天,在机缘巧合之下,买到了一台性能配置一般的腾讯云服务器(香港的),因为性能比较差,没啥太大用途,就想着试试搭建一个VPN,后来,经过多次尝试和查资料,总结出了一套几乎100%成功的教程,现在拿来 ...

  9. 【Web开发】Mean web开发 01-Express实现MVC模式开发

    简介 Mean是JavaScript的全栈开发框架.更多介绍 用Express实现MVC模式开发是Mean Web全栈开发中的一部分. Express 是一个基于 Node.js 平台的极简.灵活的 ...

  10. openresty使用笔记(一)

    背景介绍 游戏经过一段时间的运营,发现了原来的设计缺陷太多,所以决定重新设计架构.使用到nginx作为核心并通过lua+redis设计实现自己的负载分配方案.先看看下面这张简单的架构图吧~ 从图上看, ...