poj 1274 The Perfact Stall
***The Perfect Stall***
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 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.
Input
The 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.
Output
For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.
Sample Input
5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2
Sample Output
4
题目大意:一共同拥有n头牛,m面墙。每头牛有自己喜欢的墙,要求每堵墙仅仅能有一头牛,求最多的匹配数
解题思路:,题目要求最大的匹配数,也就是通过左边的点,从右边的点穿出。使得穿出的个数最多,每 个点仅仅能穿过一次,我们在图中加个源点和汇点即可了。
。。然后,有了源点和汇点。源点到左边每一个的流量都是 1,也就是仅仅能通过 1 次,汇点也相似,而左边的点到右 的点相应的边的边容量为 1,就这样,这道题成功转换为最大流问题。建图后最大流解决
详细看代码:
/*
Date : 2015-8-21 晚上
Author : ITAK
Motto :
今日的我要超越昨日的我。明日的我要胜过今日的我;
以创作出更好的代码为目标。不断地超越自己。
*/
#include <iostream>
#include <cstdio>
using namespace std;
///oo表示无穷大
const int oo = 1e9+5;
///mm表示边的最大数量。由于要双向建边
const int mm = 111111;
///点的最大数量
const int mn = 1000;
///node:节点数,src:源点,dest:汇点,edge:边数
int node, src, dest, edge;
///ver:边指向的结点,flow:边的流量,next:链表的下一条边
int ver[mm], flow[mm], next[mm];
///head:节点的链表头,work:用于算法中的暂时链表头,dis:距离
int head[mn], work[mn], dis[mn], q[mn];
///初始化
void Init(int _node, int _src, int _dest)
{
node = _node, src = _src, dest = _dest;
for(int i=0; i<node; i++)
head[i] = -1;
edge = 0;
}
///添加边
void addedge(int u, int v, int c)
{
ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
///广搜计算出每一个点与源点的最短距离,假设不能到达汇点说明算法结束
bool Dinic_bfs()
{
int i, u, v, l, r = 0;
for(i=0; i<node; i++)
dis[i] = -1;
dis[q[r++]=src] = 0;
for(l=0; l<r; l++)
for(i=head[u=q[l]]; i>=0; i=next[i])
if(flow[i] && dis[v=ver[i]]<0)
{
///这条边必须有剩余流量
dis[q[r++]=v] = dis[u] + 1;
if(v == dest)
return 1;
}
return 0;
}
///寻找可行流的增广路算法。按节点的距离来找。加高速度
int Dinic_dfs(int u, int exp)
{
if(u == dest)
return exp;
///work 是暂时链表头,这里用 i 引用它,这样寻找过的边不再寻找*
for(int &i=work[u],v,tmp; i>=0; i=next[i])
{
if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
{
///正反向边容量改变
flow[i] -= tmp;
flow[i^1] += tmp;
return tmp;
}
}
return 0;
}
///求最大流。直到没有可行流
int Dinic_flow()
{
int i, ret=0, data;
while(Dinic_bfs())
{
for(i=0; i<node; i++)
work[i] = head[i];
while(data = Dinic_dfs(src, oo))
ret += data;//cout<<666<<endl;
}
return ret;
}
int main()
{
int n, m, u, v, c;
while(cin>>n>>m)
{
Init(n+m+2, 0, n+m+1);
for(u=1; u<=n; u++)
{
addedge(src, u, 1);
cin>>c;
while(c--)
{
cin>>v;
addedge(u, v+n, 1);
}
}
while(m)
addedge(n+m--, dest, 1);
cout<<Dinic_flow()<<endl;
}
return 0;
}
poj 1274 The Perfact Stall的更多相关文章
- Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)
Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配) Description 农夫约翰上个 ...
- poj——1274 The Perfect Stall
poj——1274 The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25709 A ...
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
- POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24081 Accepted: 106 ...
- [题解]poj 1274 The Prefect Stall
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22736 Accepted: 10144 Description Far ...
- poj 1274 The Prefect Stall - 二分匹配
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22736 Accepted: 10144 Description Far ...
- poj 1274 The Perfect Stall 解题报告
题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...
- [题解]poj 1274 The Perfect Stall(网络流)
二分匹配传送门[here] 原题传送门[here] 题意大概说一下,就是有N头牛和M个牛棚,每头牛愿意住在一些牛棚,求最大能够满足多少头牛的要求. 很明显就是一道裸裸的二分图最大匹配,但是为了练练网络 ...
- [POJ] 1274 The Perfect Stall(二分图最大匹配)
题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...
随机推荐
- summernote图片上传功能保存到服务器指定文件夹+php代码+java方法
1.summernote富文本编辑器 summernote是一款基于bootstrap的富文本编辑器,是一款十分好用的文本编辑器,还附带有图片和文件上传功能. 那么在我们网站中想吧这个图片上传到服务器 ...
- (7) go 函数
1.格式 调用 2.包 (1)包 本质 文件夹.每一个文件都必须属于一个包 (2)给包取别名 (3)函数的首字母大小,决定是否能被外包访问 (3) 3.多返回值 4.递归 5.基本数据类型和数组都是拷 ...
- js中复制方法总结
js中有深拷贝和浅拷贝两种复制形式,下面总结一下常用方法,方便平时工作复习使用 一.浅拷贝 1.json对象浅拷贝 var newObj = JSON.parse(JSON.stringify( so ...
- HZAU 1205 Sequence Number(双指针)
题目链接:http://acm.hzau.edu.cn/problem.php?id=1205 [题意]给你一串数,要求你找到两个数a[i],a[j],使得a[i]<=a[j]且j>=i且 ...
- DZY Loves Chinese / DZY Loves Chinese II
题面在这里! 这两个其实是一个题啦..双倍经验加成23333 可以很简单的发现如果把一条树边和所有覆盖它的非树边都删去的话,那么图会不连通: 如果再手玩一下可以发现,如果把两个被非树边覆盖的集合相同的 ...
- 【数论】【二次剩余】【map】hdu6128 Inverse of sum
部分引用自:http://blog.csdn.net/v5zsq/article/details/77255048 所以假设方程 x^2+x+1=0 在模p意义下的解为d,则答案就是满足(ai/aj) ...
- 升级到XCode6后,iOS8里设置tableview的setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
我们在使用tableview时会发现分割线的左边会短一些,通常可以使用 setSeparatorInset:UIEdgeInsetsZero 来解决.但是升级到XCode6之后,在iOS8里发现没有效 ...
- 如何使用 DBCC MEMORYSTATUS 命令来监视 SQL Server 2005 中的内存使用情况
https://technet.microsoft.com/en-us/solutionaccelerators/dd537566.aspx 注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完 ...
- Understanding how SQL Server executes a query
https://www.codeproject.com/Articles/630346/Understanding-how-SQL-Server-executes-a-query https://ww ...
- 基于tiny4412的Linux内核移植(支持device tree)(三)
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...