题目链接:https://www.luogu.org/problemnew/show/P1402

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e6 + 10;
const int inf = 1e9;
int n, p, q, s, t, deep[maxn], maxflow;
struct edge{
int flow, next, to;
}e[maxn<<2];
int head[maxn], cnt = -1;
queue<int> q1;
void add(int u, int v, int w)
{
e[++cnt].flow = w; e[cnt].next = head[u]; e[cnt].to = v; head[u] = cnt;
e[++cnt].flow = 0; e[cnt].next = head[v]; e[cnt].to = u; head[v] = cnt;
}
bool bfs(int s, int t)
{
memset(deep, 0x7f, sizeof(deep));
while(!q1.empty()) q1.pop();
q1.push(s); deep[s] = 0;
while(!q1.empty())
{
int now = q1.front(); q1.pop();
for(int i = head[now]; i != -1; i = e[i].next)
{
if(deep[e[i].to] > inf && e[i].flow)
{
deep[e[i].to] = deep[now] + 1;
q1.push(e[i].to);
}
}
}
if(deep[t] < inf) return true;
else return false;
}
int dfs(int now, int t, int limit)
{
if(!limit || now == t) return limit;
int flow = 0, f;
for(int i = head[now]; i != -1; i = e[i].next)
{
if(deep[e[i].to] == deep[now] + 1 && (f = dfs(e[i].to, t, min(e[i].flow, limit))))
{
flow += f;
limit -= f;
e[i].flow -= f;
e[i^1].flow += f;
if(!limit) break;
}
}
return flow;
}
void Dinic(int s, int t)
{
while(bfs(s, t))
maxflow += dfs(s, t, inf);
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d%d",&n,&p,&q);
s = 1, t = n + n + p + q + 2;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= p; j++)
{
int u;
scanf("%d",&u);
if(u == 1)
{
add(j + n + n + 1, i + 1, 1);
}
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= q; j++)
{
int u;
scanf("%d",&u);
if(u == 1)
{
add(i + n + 1, j + n + n + p + 1, 1);
}
}
for(int i = 1; i <= n; i++)
add(i + 1, i + n + 1, 1);
for(int i = 1; i <= p; i++)
add(s, i + n + n + 1, 1);
for(int i = 1; i <= q; i++)
add(i + n + n + p + 1, t, 1);
Dinic(s, t);
printf("%d",maxflow);
return 0;
}

【luogu P1402 酒店之王】 题解的更多相关文章

  1. luogu P1402 酒店之王

    题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等,也有自己所爱的菜,但是该酒店只有p间房间,一天只有固定的q道不同的菜. ...

  2. BZOJ 1711 吃饭dining/Luogu P1402 酒店之王 拆点+最大流流匹配

    题意: (吃饭dining)有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享 ...

  3. 【题解】 Luogu P1402 酒店之王 (二分图匹配)

    懒得复制,原题目戳我 Solution: 这题没想到这么水,就是两个二分图而已 如果房间的二分图没匹配成功就直接进入下一个人 如果房间的二分图匹配成功,食物二分图匹配不成功就把房间的\(be[ ]\) ...

  4. 【Luogu】P1402 酒店之王 题解

    原题链接 这道题,很明显是个配对问题.于是,我们可以想到用网络最大流来做. 先整理一下题目条件. 很明显,根据贪心思想,要使最多人满意,每个人应该最多睡一个房间(似乎也没有人能睡两个房间),吃一道菜. ...

  5. LUOGU P1402 酒店之王 (网络流)

    解题思路 应该比较显然得能看出这是个网络流,将$S$与房间连边,房间与人连边,人与菜连边,菜与汇点连边,边的流量均为1.但这样是错误的,因为有可能一个人跑过去2的流量,所以要将人拆点限流. #incl ...

  6. 洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解+代码

    洛谷P2891 Dining P1402 酒店之王[类二分图匹配]题解+代码 酒店之王 题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的 ...

  7. P1402 酒店之王【网络流】【最大流】

    P1402 酒店之王 提交 5.39k 通过 2.16k 时间限制 1.00s 内存限制 125.00MB 题目提供者yeszy 难度省选/NOI- 历史分数100 提交记录 查看题解 标签 福建省历 ...

  8. Luogu 1402 酒店之王(二分图最大匹配)

    Luogu 1402 酒店之王(二分图最大匹配) Description XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等,也有自 ...

  9. P1402 酒店之王

    P1402 酒店之王 每个人要匹配一个A和一个B,所以这样连边: S向每个房间连边. 每个房间向喜欢这个房间的人连边. 每个人向喜欢的菜连边. 每道菜向T连边. 边权均为1. 注意人要限流. // I ...

随机推荐

  1. android AIDL服务

    这篇文章http://byandby.iteye.com/blog/1026110我们介绍了android的本地服务:它只能由承载它的应用程序使用.现在我们将介绍如何构建可由其他进程通过 RPC 使用 ...

  2. 深入理解JavaScript系列(13):This? Yes,this!

    介绍 在这篇文章里,我们将讨论跟执行上下文直接相关的更多细节.讨论的主题就是this关键字.实践证明,这个主题很难,在不同执行上下文中this的确定经常会发生问题. 许多程序员习惯的认为,在程序语言中 ...

  3. SQL脚本整理系列 三

    触发器 SQL 2008 怎么实现删除学生表里面的一条记录,成绩表里面关于这个学生的记录也同时删掉,谢求具体代码 --创建表 DROP TABLE tstudent GO CREATE TABLE t ...

  4. 06.do-while循环的练习

    练习1: namespace _09.do_while循环练习01 { class Program { static void Main(string[] args) { //计算1到100之间的整数 ...

  5. App调用safar

    /调用safar打开网页 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.cnblo ...

  6. HDU 4268 multiset

    http://acm.hust.edu.cn/vjudge/contest/123100#problem/B #include <iostream> #include <string ...

  7. CSS文字有关属性

    font-size|family|weight|style 大小字体加粗斜体 color|opacity 颜色透明度 height+line-height:垂直居中 overflow:hidden|v ...

  8. angularjs如何默认选中radio

    (1). 使用 ng-checked 即可.   <label class="radio-inline"> <input name="display&q ...

  9. 企业Web应用创新实验

    我现在设计一个小而美的管理工具,为此费劲心思搞“创新”.“创新”一词已经被滥用,但我...真的想弄出一点创新. 基于Web的企业应用,如CRM.项目管理.OA等软件,尽管经历十几年发展,所谓的理论有所 ...

  10. Android中的Service与进程间通信(IPC)详解

    Service 什么是Service 在后台长期运行的没有界面的组件.其他组件可以启动Service让他在后台运行,或者绑定Service与它进行交互,甚至实现进程间通信(IPC).例如,可以让服务在 ...