题目链接: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. 2018湖湘杯web、misc记录

    1.题目名 Code Check 打开题目,右键发现有id参数的url,简单base64解码以后发现不是明文,说明利用了其他的加密方式,那么应该会有具体的加密方式给我们,于是试试常见的文件泄露,可以发 ...

  2. ztree使用方法 python后台

    一.在提前加载js的地方写一段js,判断该页面是否需要添加ztree,我的项目所有提前加载的js都写在admin.js中 //增加ztree $(document).ready(function() ...

  3. ICONIX方法(用例分析方法实例教程)

  4. pod install 出错

    今天使用cocoapods的时候在执行pod install出错,如下: 使用很多方法都不行,但是问题感觉应该是需要升级,所有就找到升级cocoapods:sudo gem install -n /u ...

  5. PAT 1062 Talent and Virtue

    #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #i ...

  6. cf547D. Mike and Fish(欧拉回路)

    题意 题目链接 Sol 说实话这题我到现在都不知道咋A的. 考试的时候是对任意相邻点之间连边,然后一分没有 然后改成每两个之间连一条边就A了.. 按说是可以过掉任意坐标上的点都是偶数的数据啊.. #i ...

  7. Don't forget, a person's greatest emotional need is to feel appreciated.

    Don't forget, a person's greatest emotional need is to feel appreciated.莫忘记,人类情感上最大的需要是感恩.

  8. The only person standing in your way is you.

    The only person standing in your way is you.唯一阻碍你的人是你自己.

  9. JavaScript简易动画

    <p id="s">fly</p> <script> function move(){ var id=document.getElementBy ...

  10. Windows8中如何打包和安装一个本地的Metro类型应用(转)

    Windows8中如何打包和安装一个本地的Metro类型应用 (转自:http://software.intel.com/zh-cn/blogs/2012/05/09/windows8metro) 微 ...