poj 1274 The Perfect Stall 解题报告
题目链接:http://poj.org/problem?id=1274
题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 stall 最大匹配数是多少。
二分图匹配,匈牙利算法入门题,留个纪念吧。
书上看到的一些比较有用的知识点:
增广:通俗地说,设当前二分图中,已有 x 个匹配边(也就是假设代码中match[i] 不为0的个数有x个),现在对 i 点(也就是代码中dfs中的参数 x) 指定一个匹配点 j, 由于 j 可能有匹配点设为 k(map[x][i]),必须要为k找到一个新匹配 (对应黑屏中的前两次增广,本来的match[2] 从 1 变为 2 了,因为要为牛2找stall ,遇到map[2][2]边的时候发现match[2] 有数,只能调用dfs(match[2]) 为 match[2]找另一个匹配,也就是map[1][5],发现能找到,于是match[5] = 1,match[2] 就顺利成章变为2了)。

若能够找到,则表示匹配成功了x + 1 条边;若不能找到,相当于还是只有 x 个匹配边,只不过换了一种匹配方案而已,若达不到增加一条边的目的则称为对 i 增广不成功。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring> using namespace std;
const int maxn = + ; int map[maxn][maxn];
int match[maxn]; // match[m]存储了匹配的方案, match[i] = j :仓库i里是牛j
bool vis[maxn];
int n, m, cnt; int dfs(int x) // 深搜找增广路径
{
for (int i = ; i <= m; i++)
{
if (!vis[i] && map[x][i]) // 对x的每个邻接点
{
vis[i] = true;
if (match[i] == || dfs(match[i])) // 若 match[i] 点是一个初始值,表示i点原来没有匹配,则增广成功返回
{ // 或者 match[i]点能重新找到一个新的匹配点
match[i] = x; // 保证新的匹配方案
return ;
}
}
}
return ;
} void Hungary()
{
cnt = ;
for (int i = ; i <= n; i++) // 对 n 个点依次进行增广
{
memset(vis, , sizeof(vis));
cnt += dfs(i); // 增广成功,表示i点找到了一个匹配,多了一条匹配边
}
} int main()
{
int k, to; // k: stall_num, to:stall_id
while (scanf("%d%d", &n, &m) != EOF)
{
memset(map, , sizeof(map));
memset(match, , sizeof(match)); for (int i = ; i <= n; i++)
{
scanf("%d", &k);
for (int j = ; j <= k; j++)
{
scanf("%d", &to);
map[i][to] = ;
}
}
Hungary();
printf("%d\n", cnt);
}
}
poj 1274 The Perfect 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 Perfect Stall【匈牙利算法模板题】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20874 Accepted: 942 ...
- poj 1274 The Perfect Stall (二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17768 Accepted: 810 ...
- poj —— 1274 The Perfect Stall
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26274 Accepted: 116 ...
- [题解]poj 1274 The Perfect Stall(网络流)
二分匹配传送门[here] 原题传送门[here] 题意大概说一下,就是有N头牛和M个牛棚,每头牛愿意住在一些牛棚,求最大能够满足多少头牛的要求. 很明显就是一道裸裸的二分图最大匹配,但是为了练练网络 ...
- [POJ] 1274 The Perfect Stall(二分图最大匹配)
题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...
随机推荐
- hihocoder #1290 : Demo Day
传送门 #1290 : Demo Day 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics star ...
- sgu 102模拟欧拉函数
感觉自己弱爆了,做做SGU吧... #include<iostream> #include<cmath> //欧拉函数 using namespace std; int eul ...
- Delphi使用进行post数据时超时设置
因项目需要进行http的post提交数据,开始时用indy的idHttp组件,但是测试时发现当网络中断(如拔掉网线),idHttp的超时设置无效果,要等20秒才提示超时(参考网上的做法,将indy9升 ...
- Linux命令之ss
1.ss -s 显示socket的统计信息 2.ss -a显示socket的详细信息 (ta:tcp,ua:udp) 3.ss -l显示本机监听的端口 4.ss -pl 显示本机监听的端口和程序 ht ...
- T1013 求先序排列 codevs
http://codevs.cn/problem/1013/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- 如何查看stm32固件库版本及MDK和keil uvision的关系
一.方法如上图: 本人的keil uvision4.12版本如下,晕倒! * Version : V2.0.1* Date : 06/13/2008 二.keil MDK和keil uvision的关 ...
- luogu P3420 [POI2005]SKA-Piggy Banks
题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can either be opened with its correspon ...
- (入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)
1.配置tomcat数据源: # 数据源基本配置spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true ...
- AtoS查看iOS Crash log中的16进制代码日志
注意:crash_log一定要和打包时的archive对应上: 方法1)在Orgnizer里找到某一个archive,即:/Users/handywang/Library/Developer/Xcod ...
- Info.plist 的字段解释
bundle字段 这些字段名都是XML中的名称,在xcode的属性编辑器中,名字并不相同 bundle目录中的属性列表详细描述了有关该bundle的信息.Finder和一些系统API在一些情况下会使用 ...