洛谷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的边

然后跑最大流即可


  1. #include<iostream>
  2. #include<algorithm>
  3. #include<queue>
  4. #include<vector>
  5. #include<cstring>
  6. using namespace std;
  7. const int inf=1e9;
  8. int n,p,q;
  9. int s=0,t;
  10. int tot=1;
  11. struct node{int v,f,nxt;}E[1000010];
  12. int head[1000010];
  13. int lev[1000010];
  14. int maxf;
  15. void add(int u,int v,int cap)
  16. {
  17. E[++tot].nxt=head[u];
  18. E[tot].v=v;
  19. E[tot].f=cap;
  20. head[u]=tot;
  21. }
  22. bool bfs()
  23. {
  24. memset(lev,-1,sizeof(lev));
  25. queue<int> q;
  26. q.push(s);
  27. lev[s]=0;
  28. while(!q.empty())
  29. {
  30. int u=q.front();
  31. q.pop();
  32. for(int i=head[u];i;i=E[i].nxt)
  33. {
  34. int v=E[i].v;
  35. if(lev[v]==-1&&E[i].f)
  36. {
  37. lev[v]=lev[u]+1;
  38. if(v==t) return true;
  39. q.push(v);
  40. }
  41. }
  42. }
  43. return false;
  44. }
  45. int dfs(int u,int cap)
  46. {
  47. if(u==t)
  48. return cap;
  49. int flow=cap;
  50. for(int i=head[u];i;i=E[i].nxt)
  51. {
  52. int v=E[i].v;
  53. if(lev[v]==lev[u]+1&&flow&&E[i].f>0)
  54. {
  55. int f=dfs(v,min(flow,E[i].f));
  56. flow-=f;
  57. E[i].f-=f;
  58. E[i^1].f+=f;
  59. }
  60. }
  61. return cap-flow;
  62. }
  63. int main()
  64. {
  65. cin>>n>>p>>q;
  66. t=n*2+q+p+1;//超级汇点
  67. for(int i=1;i<=n;i++)
  68. {
  69. add(i,i+n,1);
  70. add(i+n,i,0);
  71. //客人拆点
  72. }
  73. for(int i=1;i<=n;i++)
  74. {
  75. for(int j=1;j<=p;j++)
  76. {
  77. int temp;
  78. cin>>temp;
  79. if(temp==1)
  80. {
  81. add(i+n,2*n+j,1);
  82. add(2*n+j,i+n,0);
  83. //客人向自己喜欢的房间连边
  84. //注意由于拆点,这里客人编号为i+n
  85. }
  86. }
  87. }
  88. for(int i=1;i<=n;i++)
  89. {
  90. for(int j=1;j<=q;j++)
  91. {
  92. int temp;
  93. cin>>temp;
  94. if(temp==1)
  95. {
  96. add(2*n+p+j,i,1);
  97. add(i,2*n+p+j,0);
  98. //每个喜欢的菜向客人连边
  99. //这里客人编号为i
  100. }
  101. }
  102. }
  103. for(int i=2*n+p+1;i<=2*n+p+q;i++)
  104. {
  105. add(s,i,1);
  106. add(i,s,0);//超源向菜连边
  107. }
  108. for(int i=2*n+1;i<=2*n+p;i++)
  109. {
  110. add(i,t,1);
  111. add(t,i,0);//房间向超汇连边
  112. }
  113. while(bfs())//最大流
  114. maxf+=dfs(s,inf);
  115. cout<<maxf;
  116. return 0;
  117. }

[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


题目分析

其实和上面那题是一样的。。。。

改改输入,双倍经验


  1. #include<iostream>
  2. #include<algorithm>
  3. #include<queue>
  4. #include<vector>
  5. #include<cstring>
  6. using namespace std;
  7. const int inf=1e9;
  8. int n,f,d;
  9. int s=0,t;
  10. int tot=1;
  11. struct node{int v,f,nxt;}E[1000010];
  12. int head[1000010];
  13. int lev[1000010];
  14. int maxf;
  15. void add(int u,int v,int cap)
  16. {
  17. E[++tot].nxt=head[u];
  18. E[tot].v=v;
  19. E[tot].f=cap;
  20. head[u]=tot;
  21. }
  22. bool bfs()
  23. {
  24. memset(lev,-1,sizeof(lev));
  25. queue<int> q;
  26. q.push(s);
  27. lev[s]=0;
  28. while(!q.empty())
  29. {
  30. int u=q.front();
  31. q.pop();
  32. for(int i=head[u];i;i=E[i].nxt)
  33. {
  34. int v=E[i].v;
  35. if(lev[v]==-1&&E[i].f)
  36. {
  37. lev[v]=lev[u]+1;
  38. if(v==t) return true;
  39. q.push(v);
  40. }
  41. }
  42. }
  43. return false;
  44. }
  45. int dfs(int u,int cap)
  46. {
  47. if(u==t)
  48. return cap;
  49. int flow=cap;
  50. for(int i=head[u];i;i=E[i].nxt)
  51. {
  52. int v=E[i].v;
  53. if(lev[v]==lev[u]+1&&flow&&E[i].f>0)
  54. {
  55. int f=dfs(v,min(flow,E[i].f));
  56. flow-=f;
  57. E[i].f-=f;
  58. E[i^1].f+=f;
  59. }
  60. }
  61. return cap-flow;
  62. }
  63. int main()
  64. {
  65. cin>>n>>f>>d;
  66. t=n*2+f+d+1;
  67. for(int i=1;i<=n;i++)
  68. {
  69. add(i,i+n,1);
  70. add(i+n,i,0);
  71. }
  72. for(int i=1;i<=n;i++)
  73. {
  74. int food,drink;
  75. cin>>food>>drink;
  76. while(food--)
  77. {
  78. int fnum;
  79. cin>>fnum;
  80. add(i+n,2*n+fnum,1);
  81. add(2*n+fnum,i+n,0);
  82. }
  83. while(drink--)
  84. {
  85. int dnum;
  86. cin>>dnum;
  87. add(2*n+f+dnum,i,1);
  88. add(i,2*n+f+dnum,0);
  89. }
  90. }
  91. for(int i=2*n+f+1;i<=n*2+f+d;i++)
  92. {
  93. add(s,i,1);
  94. add(i,s,0);
  95. }
  96. for(int i=2*n+1;i<=2*n+f;i++)
  97. {
  98. add(i,t,1);
  99. add(t,i,0);
  100. }
  101. while(bfs())
  102. maxf+=dfs(s,inf);
  103. cout<<maxf;
  104. return 0;
  105. }

洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解+代码的更多相关文章

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

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

  2. 洛谷P4589 [TJOI2018]智力竞赛(二分答案 二分图匹配)

    题意 题目链接 给出一个带权有向图,选出n + 1n+1条链,问能否全部点覆盖,如果不能,问不能覆盖的点权最小值最大是多少 Sol TJOI怎么净出板子题 二分答案之后直接二分图匹配check一下. ...

  3. 【洛谷 P1129】 [ZJOI2007]矩阵游戏 (二分图匹配)

    题目链接 看到题目肯定首先会想到搜索. 然鹅数据范围\(n<=200\)这么大(其实也不算太大),肯定是不行的. 如果\((i,j)\)是\(1\),从\(i\)向\(j\)连一条边,表示第\( ...

  4. 洛谷P2756 飞行员配对方案问题(二分图匹配)

    传送门 一个基础的二分图匹配(虽然今天才学会) 因为不会匈牙利算法只好用网络流做 先新建一个超级源和超级汇,源往所有左边的点连边,所有右边的点往汇连边 然后跑一边最大流就好了 顺便记录一下匹配到谁就好 ...

  5. 洛谷P2526 [SHOI2001]小狗散步(二分图匹配)

    题目背景 Grant喜欢带着他的小狗Pandog散步.Grant以一定的速度沿着固定路线走,该路线可能自交.Pandog喜欢游览沿途的景点,不过会在给定的N个点和主人相遇.小狗和主人同时从(X1,Y1 ...

  6. 洛谷 P2055 [ ZJOI 2009 ] 假期的宿舍 —— 二分图匹配

    题目:https://www.luogu.org/problemnew/show/P2055 二分图匹配: 注意要连边的话对方必须有床! 代码如下: #include<iostream> ...

  7. 【洛谷P1963】[NOI2009]变换序列(二分图匹配)

    传送门 题意: 现有一个\(0\)到\(n-1\)的排列\(T\),定义距离\(D(x,y)=min\{|x-y|,N-|x-y|\}\). 现在给出\(D(i, T_i)\),输出字典序最小的符合条 ...

  8. 洛谷P1402 酒店之王(二分图)

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

  9. P1402 酒店之王

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

随机推荐

  1. Linux/Unix系统SSH远程按Backspace键删除时出现^H的处理方法

    在linux/unix系统中连接SSH远程工作时,输出字符后按Backspace键删除时,会出现^H,这对习惯了按Backspace键删除的用户来说,感觉非常别扭,虽然可以通过Ctrl+Backspa ...

  2. 查看php的配置文件Php.ini的位置

    标签:php服务器 浏览器 配置文件 Linux local 近来,有不博友问php.ini存在哪个目录下?或者修改php.ini以后为何没有生效?基于以上两个问题,我觉得有必要教一下刚接触PHP的博 ...

  3. Tomcat 源码分析(二)——Request处理全过程

    前一篇博客,我总结了Tomcat对于生命周期组件的管理.在了解了容器的启动之后,我们开始剖析它的内部运行机制.今天我们来分析一下Tomcat如何处理Request.Socket作为网络通信的基础也是R ...

  4. label+input实现开关切换效果

    Document 主要使用label+input来实现改变left的值,下面是核心代码,意思就是选中的input的兄弟节点.box下的.switch-btn元素的left会变成0px(原来是-37px ...

  5. CSS中设置border:none和border:0的区别

    在我们设置CSS的时候,对标签元素不设置边框属性或者取消边框属性一般设置为:border:none;或border:0;两种方法均可. border:none;与border:0;的区别体现有两点:一 ...

  6. 日历插件——laydate.js

    laydate是一款很好用的日历控件,兼容了包括IE6在内的所有主流浏览器,默认有三种皮肤,如需其它皮肤可去官网下载http://www.layui.com/laydate/  一.核心方法:layd ...

  7. Elasticsearch 全教程--入门

    1.1 初识 Elasticsearch 是一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎,可以说 Lucene 是当今最先进,最高效的全功能开源搜索引擎框架. 但是 L ...

  8. notepad++代码输出电话号

    在网上看到一个程序员找合租的消息,希望找一个程序员合租,所以电话号码以代码的形式输出,闲来没事打出来玩玩.首先新建一个test.java文件,并敲入下面代码: 1 public class test{ ...

  9. fread和fwrite的使用

    fread和fwrite的使用 fread和fwrite一般用于二进制文件的输入/输出,要不然你打开fwrite写入的文件就是乱码. 1.fread和fwrite函数 数据块I/O fread与fwr ...

  10. 【COOKIE 与 SESSION】

    一.相关概念 cookie的出现,解决http协议无状态特性 由于http协议无法保持状态,但实际情况,我们却又需要"保持状态",因此cookie就是在这样一个场景下诞生. 举例: ...