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

  1. 5 5
  2. 2 2 5
  3. 3 2 3 4
  4. 2 1 5
  5. 3 1 2 5
  6. 1 2

[编辑]SAMPLE
OUTPUT

  1. 4

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

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

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

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. int map[105][105];
  5. int visit[105],flag[105];
  6. int n,m;
  7. bool dfs(int a) {
  8. for(int i=1;i<=n;i++) {
  9. if(map[a][i] && !visit[i]) {
  10. visit[i]=1;
  11. if(flag[i]==0 || dfs(flag[i])) {
  12. flag[i]=a;
  13. return true;
  14. }
  15. }
  16. }
  17. return false;
  18. }
  19. int main() {
  20. while(cin>>n>>m) {
  21. memset(map,0,sizeof(map));
  22. for(int i=1;i<=m;i++) {
  23. int x,y;
  24. cin>>x>>y;
  25. map[x][y]=1;
  26. }
  27. memset(flag,0,sizeof(flag));
  28. int result=0;
  29. for(int i=1;i<=n;i++) {
  30. memset(visit,0,sizeof(visit));
  31. if(dfs(i)) result++;
  32. }
  33. cout<<result<<endl;
  34. }
  35. return 0;
  36. }

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

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

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

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

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

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

  1. /*
  2. PROG:stall4
  3. ID:juan1973
  4. LANG:C++
  5. */
  6. #include <cstdio>
  7. #include <algorithm>
  8. #include <memory.h>
  9. using namespace std;
  10. int n,m,tot,flow,cnt,aug,v,p,q,i,j,u;
  11. int map[505][505],queue[20005],pre[505];
  12. int main()
  13. {
  14. freopen("stall4.in","r",stdin);
  15. freopen("stall4.out","w",stdout);
  16. memset(map,0,sizeof(map));
  17. scanf("%ld%ld",&n,&m);
  18. for(i=1;i<=n;i++)
  19. {
  20. scanf("%ld",&p);
  21. for (j=1;j<=p;j++)
  22. {
  23. scanf("%ld",&q);
  24. map[i][q+n]=1;
  25. }
  26. }
  27. flow=0;cnt=n+m+1;
  28. for (i=1;i<=n;i++) map[0][i]=1;
  29. for (i=n+1;i<=m+n;i++) map[i][cnt]=1;
  30. memset(queue,0,sizeof(queue));
  31. while(1)
  32. {
  33. memset(pre,-1,sizeof(pre));
  34. queue[1]=0;
  35. for(p=1,q=1;p<=q;p++)
  36. {
  37. u=queue[p];
  38. for(v=1;v<=cnt;v++)
  39. if(pre[v]<0&&map[u][v]>0)
  40. {
  41. pre[v]=u;
  42. queue[++q]=v;
  43. }
  44. if(pre[cnt]>=0)break;
  45. }
  46. if(pre[cnt]<0)break;
  47. aug=2000000000;
  48. for(v=cnt;v!=0;v=pre[v])aug=min(aug,map[pre[v]][v]);
  49. for(v=cnt;v!=0;v=pre[v])
  50. {
  51. map[pre[v]][v]-=aug;
  52. map[v][pre[v]]+=aug;
  53. }
  54. flow+=aug;
  55. }
  56. printf("%ld\n",flow);
  57. return 0;
  58. }

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. Telegram学习解析系列(三) : Build Telegram报错分析总结

    正好通过这次 Telegram 的运行,很想把常见的项目运行的错误好好的总结一下,在前面的博客中,又星星散散的总结过错误和一些警告的消除方法,这次把错误处理一下,还有Telegram项目中有999+的 ...

  2. Nginx教程(三) Nginx日志管理

    Nginx教程(三) Nginx日志管理 1 日志管理 1.1 Nginx日志描述 通过访问日志,你可以得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某 ...

  3. 移动端页面 iPhone + Safari 页面调试 之 正确查看网络请求的姿势

    如题 本文主要将 Safari + iPhone 前端开发调试  之 正确查看网络请求的 姿势 惯例 说下问题场景: 早知道safari(Mac) + iPhone 调试的方便 能解决很多日常调试问题 ...

  4. js实现超出一定字数隐藏并用省略号"..."代替,点击后又可进行展开和收起,

    原理简单阐述:放两个一模一样的div,把你要展示的文字放进去.页面初始化的时候,第一个div展示,第二个 div隐藏,就是这么简单.(ps:可以直接复制代码到你自己项目中,查看效果) 样式部分(记得引 ...

  5. nodeJS之路径PATH模块

    前面的话 path模块包含一系列处理和转换文件路径的工具集,通过 require('path') 可用来访问这个模块.本文将详细介绍path模块 路径组成 [path.dirname(p)] 返回路径 ...

  6. Vulkan Tutorial 05 逻辑设备与队列

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 在选择要使用的物理设备之后,我们需要设置一个逻辑设备用于交 ...

  7. 提高code效率

    分享下个人编码挫折,关于提高编码效率.代码规范.清晰的代码模块顺序.及时总结代码(提取出可复用的)以及清晰的注释,这是我感觉有必要的,因为工作到后期,代码量都非常的大.就是上个周5那天的整体工作效率都 ...

  8. 乐视开放平台技术架构-servlet和spring mvc篇

    在乐视风口浪尖的时候,敢于站出来说我是乐视的而不怕被打脸的,也就是我了.就算我以后不在乐视了,提起来在乐视工作过,我也还是挺骄傲的.因为这是一个有理想,敢拼敢干的公司.想起复仇者联盟里Fury指挥官的 ...

  9. Thrift生成的bean对象,用java内省操作时注意(自己笔记)

    项目需要,需要使用内省操作,将数据写入thrift生成的bean里,于是按常理getWritedMethod.invoke 结果发现set方法找不到,结果看了下thrift自己生成的bean里,set ...

  10. 没有main方法真的不能执行代码了吗?

    今天看北大慕课遇到一段代码,于是下载下来跑了一下,奇葩的是,没有main方法既没报错,还出了结果. 下面贴出代码: class InitialTest { public static void mai ...