题解 P1894 【[USACO4.2]完美的牛栏The Perfect Stall】
题面
农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术。不幸的是,由于工程问题,每个牛栏都不一样。第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶。上个星期,农夫约翰刚刚收集到了奶牛们的爱好的信息(每头奶牛喜欢在哪些牛栏产奶)。一个牛栏只能容纳一头奶牛,当然,一头奶牛只能在一个牛栏中产奶。
给出奶牛们的爱好的信息,计算最大分配方案。
题意
如题面。
题解
每只奶牛只喜欢在自己喜欢的牛棚里面产奶。(匹配)
一个牛棚只能容纳一头奶牛。
最大的分配方案。
匈牙利算法(匹配算法),个人感觉实质上就是一个DFS的匹配算法。
伪代码:
bool dfs(int x)
{
while(找到Xi的关联顶点Yj){
if(顶点Yj不在增广路径上){
将Yj加入增广路
if(Yj是未覆盖点或者Yj的原匹配点Xk能找到增广路径){ //扩充集合M
将Yj的匹配点改为Xi;
返回true;
}
}
返回false;
}
根据题意可以知道这是一道匈牙利算法的模板题。于是开始根据伪代码来造代码。
代码
代码1(my code)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10010;
const int maxm = 10010;
int n,m,tong[maxm],ans;
bool vis[maxn],love[maxn][maxm];
inline int read(){
int k=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}
return k*f;
}
inline void init()
{
cin >> n >> m;
for(int i=1;i<=n;i++){
int l=read();
while(l--){
love[i][read()]=true;
}
}
}
bool dfs(int x)
{
for(int i = 1;i <= m;++i)
{
if(!vis[i] && love[x][i])
{
vis[i] = true;
if(tong[i]==0 || dfs(tong[i]))
{
tong[i] = x;
return true;
}
}
}
return false;
}
int main(int argc, char const *argv[])
{
init();
for(int i = 1;i <= n;++i)
{
memset(vis,0,sizeof(vis));
if(dfs(i)) ans++;
}
cout << ans;
return 0;
}
代码2(rank 1)
#include <cstdio>
#include <cstring>
using namespace std;
#define M(x) memset(x, false, sizeof(x))
const int maxn = 205;
int N, M, ans, link[maxn];
bool vis[maxn], A[maxn][maxn];
bool dfs(int x){
for(int i = 1; i <= N; i++)
if(A[x][i] && !vis[i]){
vis[i] = true;
if(!link[i] || dfs(link[i])){
link[i] = x;
return true;
}
}
return false;
}
inline int GetInt(){
char x;
int ret;
while((x = getchar()) < '0' || x > '9');
ret = x - '0';
while((x = getchar()) >= '0' && x <= '9') ret = ret * 10 + x - '0';
return ret;
}
int main(){
N = GetInt(), M = GetInt();
for(int i = 1; i <= N; i++){
int sum;
sum = GetInt();
for(int j = 1, OP; j <= sum; j++){
OP = GetInt();
A[i][OP] = true;
}
}
for(int i = 1; i <= N; i++){
M(vis);
if(dfs(i)) ans++;
}
printf("%d", ans);
return 0;
}
题解 P1894 【[USACO4.2]完美的牛栏The Perfect Stall】的更多相关文章
- 洛谷——P1894 [USACO4.2]完美的牛栏The Perfect Stall
P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)
P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...
- 洛谷 P1894 [USACO4.2]完美的牛栏The Perfect Stall
P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall题解
题目 二分图最大匹配问题 cow数组标现在牛栏里的牛是几号牛 每次寻找都要清空vis数组 如果可行有两种情况 1.这个牛栏里没有牛 2.这个牛栏里的牛可以到别的牛栏去 根据此递归即可 Code: #i ...
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall
题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在 ...
- P1894 [USACO4.2]完美的牛栏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 农夫约翰上个 ...
- [USACO4.2]完美的牛栏The Perfect Stall
题目:USACO Training 4.2(在官网上提交需加文件输入输出).洛谷P1894. 题目大意:有n头奶牛m个牛栏,每头牛只会在自己喜欢的牛栏里产奶,问一次最多有多少奶牛能产奶. 解题思路:二 ...
- 洛谷 1894 [USACO4.2]完美的牛栏The Perfect Stall
[题解] 其实是个二分图最大匹配的模板题,直接上匈牙利算法就好了. #include<cstdio> #include<algorithm> #define N 1010 #d ...
随机推荐
- oracle 比较日期相等
where to_char(date1, 'yyyymmdd')=to_char(date2,'yyyymmdd'); or where to_date(char1, 'yyyymmdd')=to_d ...
- c#中的out和ref
不知大家有没有遇到过需要一个函数返回多个值的情况. 当写代码要返回多个值的时候,当然可以返回一个数组来实现,但如果遇到需要返回的多个值的类型不同呢?这个时候怎么办? c#中,out关键字和ref关键字 ...
- Hibernate中查询优化策略
Hibernate查询优化策略 ² 使用延迟加载等方式避免加载多余数据 ² 通过使用连接查询,配置二级缓存.查询缓存等方式减少select语句数目 ² 结合缓存机制,使用iterate()方法减少查询 ...
- 在pom.xml中添加Spring依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- Bash:常用命令工具-uniq
NAME uniq - report or omit repeated lines SYNOPSIS uniq [OPTION]... [INPUT [OUTPUT]] DESCRIPTION Fil ...
- Code Signal_练习题_Array Replace
Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem. Exam ...
- FIO测试磁盘的iops
FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...
- python简单验证码
安装图片处理模块pillow : pip install pillow pillow官网:http://pillow.readthedocs.io/en/latest/ 在views.py添加视图 ...
- 润乾V4导出TXT时自定义分隔符
◆ 背景说明 报表中,导出text时,默认没有分隔符:应用中对导出Text,希望能自定义分隔符.在tag中定义了 textDataSeparator属性,让用户在导出Text时自定义分隔符,从而 ...
- Linux pyenv环境安装
python工作环境管理 pyenv安装: git clone https://github.com/pyenv/pyenv ~/.pyenv echo 'export PYENV_ROOT=&quo ...