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

P1402 酒店之王

提交 5.39k
通过 2.16k
时间限制 1.00s
内存限制 125.00MB
题目提供者 yeszy
历史分数100

提交记录

查看算法标签
进入讨论版

相关讨论

 
查看讨论

推荐题目

 
查看推荐
 

展开

题目描述

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

有一天来了n个客人,每个客人说出了自己喜欢哪些房间,喜欢哪道菜。但是很不幸,可能做不到让所有顾客满意(满意的条件是住进喜欢的房间,吃到喜欢的菜)。

这里要怎么分配,能使最多顾客满意呢?

输入格式

第一行给出三个正整数表示n,p,q(<=100)。

之后n行,每行p个数包含0或1,第i个数表示喜不喜欢第i个房间(1表示喜欢,0表示不喜欢)。

之后n行,每行q个数,表示喜不喜欢第i道菜。

输出格式

最大的顾客满意数。

输入输出样例

输入 #1
2 2 2

1 0

1 0

1 1

1 1

输出 #1
1

思路:
  本来想用来练习二分匹配,但是发现匈牙利算法好像无法解决两个关联的二分图问题,画的图却很像拆点的最大流。
  只要把菜或房单独连向超级源、汇,再把人拆开做一个流入流出,随后套dinic的模板就行了。
  不知道为什么ISAP只能过7个点,如果有ISAP过的同学可以教教我嘛。

  1 #include <bits/stdc++.h>
  2 
  3 using namespace std;
  4 const int maxn = 10007;
  5 
  6 int s,t,flow;
  7 int head[maxn];
  8 int pre[maxn];
  9 int n,p,q;
 10 int cnt = 1;
 11 int a[maxn];
 12 
 13 template<class T>inline void read(T &res)
 14 {
 15     char c;T flag=1;
 16     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
 17     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
 18 }
 19 
 20 struct Edge {
 21     int to, nxt, cap;
 22 }edge[maxn];
 23 
 24 void BuildGraph(int u, int v, int cap) {
 25     edge[++cnt].to = v;
 26     edge[cnt].nxt  = head[u];
 27     edge[cnt].cap  = cap;
 28     head[u] = cnt;
 29 
 30     edge[++cnt].to = u;
 31     edge[cnt].nxt  = head[v];
 32     edge[cnt].cap  = 0;
 33     head[v] = cnt;
 34 }
 35 
 36 bool bfs() {
 37     queue <int> q;
 38     memset(a, 0, sizeof(a));
 39     a[s] = 0x3f3f3f3f;
 40     q.push(s);
 41     while(!q.empty()) {
 42         int u = q.front();
 43         q.pop();
 44         for(int i = head[u]; i; i = edge[i].nxt) {
 45             int to = edge[i].to;
 46             if(!edge[i].cap) {
 47                 continue;
 48             }
 49             if(a[to]) {
 50                 continue;
 51             }
 52             a[to] = min(a[u], edge[i].cap);
 53             pre[to] = i;
 54             q.push(to);
 55         }
 56     }
 57     if(!a[t]) {
 58         return 0;
 59     }
 60     flow += a[t];
 61     return 1;
 62 }
 63 
 64 void Dinic() {
 65     int m = t;
 66     for(; pre[m]; m = edge[pre[m]^1].to) {
 67         edge[pre[m]].cap -= a[t];
 68         edge[pre[m]^1].cap += a[t];
 69     }
 70 }
 71 
 72 int main()
 73 {
 74     read(n), read(p), read(q);
 75     s = 2*n+q+p+1;
 76     t = s+1;
 77     for(int i = 1; i <= n; ++i) {
 78         for(int x, j = 1; j <= p; ++j) {
 79             read(x);
 80             if(x) {
 81                 BuildGraph(j, p+i, 1);
 82             }
 83         }
 84     }
 85     for(int i = 1; i <= n; ++i) {
 86         for(int x, j = 1; j <= q; ++j) {
 87             read(x);
 88             if(x) {
 89                 BuildGraph(p+n+q+i, p+n+j, 1);
 90             }
 91         }
 92     }
 93     for(int i = 1; i <= n; ++i) {
 94         BuildGraph(p+i, p+n+q+i,1);
 95     }
 96     for(int i = 1; i <= p; ++i) {
 97         BuildGraph(s, i, 1);
 98     }
 99     for(int i = p+n+1; i <= p+q+n; ++i) {
100         BuildGraph(i, t, 1);
101     }
102     while(bfs()) {
103         Dinic();
104     }
105     printf("%d
",flow);
106     return 0;
107 }
原文地址:https://www.cnblogs.com/orangeko/p/12247203.html