POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17895 | Accepted: 8143 |
Description
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 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
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个房子,每一个牛都仅仅住在自己想住的房子里面,一个房子仅仅能住一个牛,问最多能够安排多少头牛入住。
像案例里的,第一头牛仅仅愿意住2,5。第二头牛。仅仅住2,3,4;第三头仅仅住1,5;第四头仅仅住1,2,5;第五头住2。
通过匈牙利算法可求得结果为4;
所以结果为4.
实现代码例如以下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream> using namespace std; const int M = 1000 + 5;
int n, m;
int link[M];
bool MAP[M][M];
bool cover[M];
int ans; void init()
{
int num;
int y;
memset(MAP, false, sizeof(MAP));
for(int i=1; i<=n; i++)
{
scanf("%d", &num);
while( num-- )
{
scanf("%d", &y);
MAP[i][y]=true;
}
} } bool dfs(int x)
{
for(int y=1; y<=m; y++)
{
if(MAP[x][y] && !cover[y])
{
cover[y]=true;
if(link[y]==-1 || dfs(link[y]))
{
link[y]=x;
return true;
}
}
}
return false;
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
ans=0;
init(); memset(link, -1, sizeof(link));
for(int i=1; i<=n; i++)
{
memset(cover, false, sizeof(cover));
if( dfs(i) )
ans++;
}
printf("%d\n", ans);
} return 0;
}
POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)的更多相关文章
- POJ1274 The Perfect Stall 二分图,匈牙利算法
N头牛,M个畜栏,每头牛仅仅喜欢当中的某几个畜栏,可是一个畜栏仅仅能有一仅仅牛拥有,问最多能够有多少仅仅牛拥有畜栏. 典型的指派型问题,用二分图匹配来做,求最大二分图匹配能够用最大流算法,也能够用匈牙 ...
- POJ1274 The Perfect Stall[二分图最大匹配]
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法
二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...
- Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)
解题报告 二分图第一题. 题目描写叙述: 为了參加即将召开的会议,A国派出M位代表,B国派出N位代表,(N,M<=1000) 会议召开前,选出K队代表,每对代表必须一个是A国的,一个是B国的; ...
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU1068 (二分图最大匹配匈牙利算法)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)
http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...
- 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids
题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...
随机推荐
- C#实现HTML转图片(网页快照)
有时候我们需要将网页转成图片,那么可以使用WebBrowser来生成网页快照,废话不多说,代码如下 1.网页快照帮助类(如果是BS或控制台需要引用System.Windows.Forms类库): pu ...
- 通过Cookie来记住用户名出现乱码问题(URL编码)
在登录时,提交一个中文名的用户名到服务器并返回到客户端的Cookie中时, 这个过程会后台会报 java.lang.IllegalArgumentException (非法数据异常) -->在给 ...
- spark热点互动问答
[Spark亚太研究院 决战云计算大数据时代 100期公益大讲堂 互动问答] Q1:我想问,hdfs的namenode挂了,怎么处理? 使用ZooKeeper: 使用Mesos: 使用Yarn: Q2 ...
- 百度之星资格赛 2016 Problem 1004
本文链接:http://www.cnblogs.com/Ash-ly/p/5494630.html 题意: 熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪 ...
- JavaScript处理异步请求的几种方式(取异步函数返回值)
JavaScript处理异步的几种方式 Javascript语言的执行环境是"单线程"(single thread,就是指一次只能完成一件任务.如果有多个任务,就必须排队,前面一个 ...
- AtCoder Grand Contest 023 A - Zero-Sum Ranges
Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement We have an integer seq ...
- Codeforces 853C - Boredom
853C - Boredom 题意 给出一个矩阵,每行每列有且仅有一个点.每次询问一个子矩形,问这些点构成的矩形有多少个与给定的矩形相交(两个处于对角线上的点可以组成矩形). 分析 考虑矩形周围 8 ...
- hdu1269(有向图强连通分量)
hdu1269 题意 判断对于任意两点是否都可以互相到达(判断有向图强连通分量个数是否为 1 ). 分析 Tarjan 算法实现. code #include<bits/stdc++.h> ...
- cogs p服务点设置
5. P服务点设置 ★★ 输入文件:djsc.in 输出文件:djsc.out 简单对比时间限制:2 s 内存限制:128 MB 问题描述为了进一步普及九年义务教育,政府要在某乡镇建立 ...
- 1087: Common Substrings (哈希)
1087: Common Substrings Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/ ...