【luogu P1402 酒店之王】 题解
题目链接: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 酒店之王】 题解的更多相关文章
- luogu P1402 酒店之王
题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等,也有自己所爱的菜,但是该酒店只有p间房间,一天只有固定的q道不同的菜. ...
- BZOJ 1711 吃饭dining/Luogu P1402 酒店之王 拆点+最大流流匹配
题意: (吃饭dining)有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享 ...
- 【题解】 Luogu P1402 酒店之王 (二分图匹配)
懒得复制,原题目戳我 Solution: 这题没想到这么水,就是两个二分图而已 如果房间的二分图没匹配成功就直接进入下一个人 如果房间的二分图匹配成功,食物二分图匹配不成功就把房间的\(be[ ]\) ...
- 【Luogu】P1402 酒店之王 题解
原题链接 这道题,很明显是个配对问题.于是,我们可以想到用网络最大流来做. 先整理一下题目条件. 很明显,根据贪心思想,要使最多人满意,每个人应该最多睡一个房间(似乎也没有人能睡两个房间),吃一道菜. ...
- LUOGU P1402 酒店之王 (网络流)
解题思路 应该比较显然得能看出这是个网络流,将$S$与房间连边,房间与人连边,人与菜连边,菜与汇点连边,边的流量均为1.但这样是错误的,因为有可能一个人跑过去2的流量,所以要将人拆点限流. #incl ...
- 洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解+代码
洛谷P2891 Dining P1402 酒店之王[类二分图匹配]题解+代码 酒店之王 题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的 ...
- P1402 酒店之王【网络流】【最大流】
P1402 酒店之王 提交 5.39k 通过 2.16k 时间限制 1.00s 内存限制 125.00MB 题目提供者yeszy 难度省选/NOI- 历史分数100 提交记录 查看题解 标签 福建省历 ...
- Luogu 1402 酒店之王(二分图最大匹配)
Luogu 1402 酒店之王(二分图最大匹配) Description XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等,也有自 ...
- P1402 酒店之王
P1402 酒店之王 每个人要匹配一个A和一个B,所以这样连边: S向每个房间连边. 每个房间向喜欢这个房间的人连边. 每个人向喜欢的菜连边. 每道菜向T连边. 边权均为1. 注意人要限流. // I ...
随机推荐
- c#高级写法
Linq的参考资料:https://www.cnblogs.com/liqingwen/p/5801249.html 1.判断str字符串中的逗号个数 string str = "1,2,3 ...
- vim源码编译启用python
坑:只指定with-python-config-dir没有指定enable-pythoninterp是没有用的 ./configure --enable-pythoninterp --with-pyt ...
- 移动端本地 H5 秒开方案探索与实现
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 企业微信移动端项目中有需求要展示数据趋势的可视化图表,经过调研,最终决定以单页面 H5 来完成,对 APP 里的一些使用 H5 实现的功能模 ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- JS常用的设计模式(6)——桥接模式
桥接模式的作用在于将实现部分和抽象部分分离开来, 以便两者可以独立的变化.在实现api的时候, 桥接模式特别有用.比如最开始的singleton的例子. var singleton = functio ...
- [转] 微信小程序 页面跳转 传递参数
本文转自:http://blog.csdn.net/qq_31383345/article/details/52795212 微信小程序的页面跳转,页面之间传递参数笔记. CSDN微信小程序开发专栏, ...
- Hibernate中的一对一注解配置
Card类 package cn.OneToOne2017109.entity; import javax.persistence.*; /** * Created by YSS on 2017/10 ...
- IE浏览器下的渐变背景
background: linear-gradient(to bottom, #000000 0%,#ffffff 100%);(标准) linear-gradient 在 ie9 以下是不支持的,所 ...
- linux里终端安转视频播放器的操作及显示
[enilu@enilu ~]$ mplayerbash: mplayer: command not found[enilu@enilu ~]$ yum list | grep mplayer^C^C ...
- Personal Geodatabase - Can't Create New or Open Existing
来自:http://forums.arcgis.com/threads/32110-Personal-Geodatabase-Can-t-Create-New-or-Open-Existing Per ...