洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解+代码
洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解+代码
酒店之王
题目描述
XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化。由于很多来住店的旅客有自己喜好的房间色调、阳光等,也有自己所爱的菜,但是该酒店只有p间房间,一天只有固定的q道不同的菜。
有一天来了n个客人,每个客人说出了自己喜欢哪些房间,喜欢哪道菜。但是很不幸,可能做不到让所有顾客满意(满意的条件是住进喜欢的房间,吃到喜欢的菜)。
这里要怎么分配,能使最多顾客满意呢?
输入格式:
第一行给出三个正整数表示n,p,q(<=100)。
之后n行,每行p个数包含0或1,第i个数表示喜不喜欢第i个房间(1表示喜欢,0表示不喜欢)。
之后n行,每行q个数,表示喜不喜欢第i道菜。
输出格式:
最大的顾客满意数。
输入样例
2 2 2
1 0
1 0
1 1
1 1
输出样例
1
题目分析
这个题目一眼就看得出是匹配
但是点集有三个
怎么办呢
以这题为例
我们把n个客人拆成2*n个点
i与i+n连一条连一条容量为1的边
(因为要保证,每个客人只被匹配一次)
每个客人由自己喜欢的菜连一条容量为1的入边
每个客人向喜欢的房间连一条容量为1的出边
建立超级汇点与超级源点
超级源点向每个菜连一条容量为1的边
每个房间向超级汇点连一条容量为1的边
然后跑最大流即可
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int inf=1e9;
int n,p,q;
int s=0,t;
int tot=1;
struct node{int v,f,nxt;}E[1000010];
int head[1000010];
int lev[1000010];
int maxf;
void add(int u,int v,int cap)
{
E[++tot].nxt=head[u];
E[tot].v=v;
E[tot].f=cap;
head[u]=tot;
}
bool bfs()
{
memset(lev,-1,sizeof(lev));
queue<int> q;
q.push(s);
lev[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].v;
if(lev[v]==-1&&E[i].f)
{
lev[v]=lev[u]+1;
if(v==t) return true;
q.push(v);
}
}
}
return false;
}
int dfs(int u,int cap)
{
if(u==t)
return cap;
int flow=cap;
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].v;
if(lev[v]==lev[u]+1&&flow&&E[i].f>0)
{
int f=dfs(v,min(flow,E[i].f));
flow-=f;
E[i].f-=f;
E[i^1].f+=f;
}
}
return cap-flow;
}
int main()
{
cin>>n>>p>>q;
t=n*2+q+p+1;//超级汇点
for(int i=1;i<=n;i++)
{
add(i,i+n,1);
add(i+n,i,0);
//客人拆点
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=p;j++)
{
int temp;
cin>>temp;
if(temp==1)
{
add(i+n,2*n+j,1);
add(2*n+j,i+n,0);
//客人向自己喜欢的房间连边
//注意由于拆点,这里客人编号为i+n
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=q;j++)
{
int temp;
cin>>temp;
if(temp==1)
{
add(2*n+p+j,i,1);
add(i,2*n+p+j,0);
//每个喜欢的菜向客人连边
//这里客人编号为i
}
}
}
for(int i=2*n+p+1;i<=2*n+p+q;i++)
{
add(s,i,1);
add(i,s,0);//超源向菜连边
}
for(int i=2*n+1;i<=2*n+p;i++)
{
add(i,t,1);
add(t,i,0);//房间向超汇连边
}
while(bfs())//最大流
maxf+=dfs(s,inf);
cout<<maxf;
return 0;
}
[USACO07OPEN] Dining
题目描述
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.
Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100)
输入格式:
Line 1: Three space-separated integers: N, F, and D
Lines 2..N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.
输出格式:
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
输入样例
4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3
输出样例
3
题目分析
其实和上面那题是一样的。。。。
改改输入,双倍经验
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int inf=1e9;
int n,f,d;
int s=0,t;
int tot=1;
struct node{int v,f,nxt;}E[1000010];
int head[1000010];
int lev[1000010];
int maxf;
void add(int u,int v,int cap)
{
E[++tot].nxt=head[u];
E[tot].v=v;
E[tot].f=cap;
head[u]=tot;
}
bool bfs()
{
memset(lev,-1,sizeof(lev));
queue<int> q;
q.push(s);
lev[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].v;
if(lev[v]==-1&&E[i].f)
{
lev[v]=lev[u]+1;
if(v==t) return true;
q.push(v);
}
}
}
return false;
}
int dfs(int u,int cap)
{
if(u==t)
return cap;
int flow=cap;
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].v;
if(lev[v]==lev[u]+1&&flow&&E[i].f>0)
{
int f=dfs(v,min(flow,E[i].f));
flow-=f;
E[i].f-=f;
E[i^1].f+=f;
}
}
return cap-flow;
}
int main()
{
cin>>n>>f>>d;
t=n*2+f+d+1;
for(int i=1;i<=n;i++)
{
add(i,i+n,1);
add(i+n,i,0);
}
for(int i=1;i<=n;i++)
{
int food,drink;
cin>>food>>drink;
while(food--)
{
int fnum;
cin>>fnum;
add(i+n,2*n+fnum,1);
add(2*n+fnum,i+n,0);
}
while(drink--)
{
int dnum;
cin>>dnum;
add(2*n+f+dnum,i,1);
add(i,2*n+f+dnum,0);
}
}
for(int i=2*n+f+1;i<=n*2+f+d;i++)
{
add(s,i,1);
add(i,s,0);
}
for(int i=2*n+1;i<=2*n+f;i++)
{
add(i,t,1);
add(t,i,0);
}
while(bfs())
maxf+=dfs(s,inf);
cout<<maxf;
return 0;
}
洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解+代码的更多相关文章
- 【题解】 Luogu P1402 酒店之王 (二分图匹配)
懒得复制,原题目戳我 Solution: 这题没想到这么水,就是两个二分图而已 如果房间的二分图没匹配成功就直接进入下一个人 如果房间的二分图匹配成功,食物二分图匹配不成功就把房间的\(be[ ]\) ...
- 洛谷P4589 [TJOI2018]智力竞赛(二分答案 二分图匹配)
题意 题目链接 给出一个带权有向图,选出n + 1n+1条链,问能否全部点覆盖,如果不能,问不能覆盖的点权最小值最大是多少 Sol TJOI怎么净出板子题 二分答案之后直接二分图匹配check一下. ...
- 【洛谷 P1129】 [ZJOI2007]矩阵游戏 (二分图匹配)
题目链接 看到题目肯定首先会想到搜索. 然鹅数据范围\(n<=200\)这么大(其实也不算太大),肯定是不行的. 如果\((i,j)\)是\(1\),从\(i\)向\(j\)连一条边,表示第\( ...
- 洛谷P2756 飞行员配对方案问题(二分图匹配)
传送门 一个基础的二分图匹配(虽然今天才学会) 因为不会匈牙利算法只好用网络流做 先新建一个超级源和超级汇,源往所有左边的点连边,所有右边的点往汇连边 然后跑一边最大流就好了 顺便记录一下匹配到谁就好 ...
- 洛谷P2526 [SHOI2001]小狗散步(二分图匹配)
题目背景 Grant喜欢带着他的小狗Pandog散步.Grant以一定的速度沿着固定路线走,该路线可能自交.Pandog喜欢游览沿途的景点,不过会在给定的N个点和主人相遇.小狗和主人同时从(X1,Y1 ...
- 洛谷 P2055 [ ZJOI 2009 ] 假期的宿舍 —— 二分图匹配
题目:https://www.luogu.org/problemnew/show/P2055 二分图匹配: 注意要连边的话对方必须有床! 代码如下: #include<iostream> ...
- 【洛谷P1963】[NOI2009]变换序列(二分图匹配)
传送门 题意: 现有一个\(0\)到\(n-1\)的排列\(T\),定义距离\(D(x,y)=min\{|x-y|,N-|x-y|\}\). 现在给出\(D(i, T_i)\),输出字典序最小的符合条 ...
- 洛谷P1402 酒店之王(二分图)
P1402 酒店之王 题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等,也有自己所爱的菜,但是该酒店只有p间房间,一天只 ...
- P1402 酒店之王
P1402 酒店之王 每个人要匹配一个A和一个B,所以这样连边: S向每个房间连边. 每个房间向喜欢这个房间的人连边. 每个人向喜欢的菜连边. 每道菜向T连边. 边权均为1. 注意人要限流. // I ...
随机推荐
- Typescript学习笔记
什么是 TypeScript TypeScript 是 JavaScript 的类型的超集,它可以编译成纯 JavaScript. 安装 TypeScript 命令行工具安装: npm install ...
- jquery mobile-按钮控件
jQuery Mobile 中的按钮会自动获得样式,这增强了他们在移动设备上的交互性和可用性.我们推荐您使用 data-role="button" 的 <a> 元素来创 ...
- [知了堂学习笔记]_MVC设计模式与JavaWEB三层架构
一.MVC设计模式 MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controlle ...
- 【js 笔记】读阮一峰老师 es6 入门笔记 —— 第二章
第二章:变量的解构赋值 在es6 版本前,如果要为多个变量赋不同值,我想是件比较麻烦的事情.但es6 版本新推出了一个新技术那就是今天的主角变量的解构赋值. 变量解构赋值分为两种方法:数组解构赋值 和 ...
- 寄存器(CPU原理)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- Javascript学习--时间
digit = [ [ [0,0,1,1,1,0,0], [0,1,1,0,1,1,0], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1, ...
- 搞个小项目吧,做一个ppt播放器
先来两个参考链接,接下来再进行实战 http://www.geek-workshop.com/forum.php?mod=viewthread&tid=1137 http://www.geek ...
- python3 第二章 - 第一个程序
1、安装 打开官网 https://www.python.org/downloads/ 下载python3.6.4 如果你是windows\mac电脑,直接双击安装包,一路next即可,如果你是lin ...
- git 文件状态与工作区域
在上一篇简单讲述了文件状态与工作区域,在这里结合相关git命令详细了解文件的状态变更. 目录 1. 介绍 2. 常用命令 3. 实际操作 1. 介绍 git的文件状态是其git核心内容,了解后对后续的 ...
- background是什么样式?
background是什么样式? 给标签添加背景图片 分为: background: url("图片路径"); #添加图片 background-position: xpx ...