洛谷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 ...
随机推荐
- 织梦dedecmsV5.7联动类型无法显示的处理方法
最近织梦dedecms在新的功能中添加了一个联动类型这样的一个功能.所谓的联动类型,类似于一级目录下有二级目录,二级目录下又有三级目录,可以理解为数据结构中树形结构.级和级之间都是有着联系的.为了让大 ...
- python基础6之迭代器&生成器、json&pickle数据序列化
内容概要: 一.生成器 二.迭代器 三.json&pickle数据序列化 一.生成器generator 在学习生成器之前我们先了解下列表生成式,现在生产一个这样的列表[0,2,4,6,8,10 ...
- PUTTY无法远程连接服务器故障解决[转]
对于一个刚刚了解putty工具的新手来说,在putty工具使用中有时出现了问题而无法解决.今天就来介绍怎么解决putty无法远程连接服务器的故障. 用putty远程连接服务器时,提示错误 server ...
- php连接memcahed出现Cannot assign requested address (99)的解决方法
今天在将服务器合并后,发现php偶尔会报出 Server *.*.*.* (tcp *****) failed with: Cannot assign requested address (99) 的 ...
- Nagios在selinux开启的情况下使用
# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/ # chcon -R -t httpd_sys_content_t /usr/loc ...
- Codeforce D. Make a Permutation!
D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- bzoj2257: [Jsoi2009]瓶子和燃料
2257: [Jsoi2009]瓶子和燃料 Time Limit: 10 Sec Memory Limit: 128 MB Description jyy就一直想着尽快回地球,可惜他飞船的燃料不够了 ...
- python之金融与量化分析
一.金融 二.ipython 基础功能 ipython 快捷键
- win32 dll工程开发创建对话框
界面编程的CreateWindow函数需要instance,只要获取到dll工程的main的instance参数,就可以使用CreateWindow函数了. 创建对话框需要CreateDialog函数 ...
- 安装puppeteer
Puppeteer是一个node库,他提供了一组用来操纵Chrome的API,默认headless也就是无UI的chrome,也可以配置为有UI. 其实有点类似于PhantomJS,但Puppetee ...