P1402 酒店之王 最大流

(color{#0066ff}{ 题目描述 })

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

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

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

$color{#0066ff}{ 输入格式 } $

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

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

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

(color{#0066ff}{输出格式})

最大的顾客满意数。

(color{#0066ff}{输入样例})

2 2 2
1 0
1 0
1 1
1 1

(color{#0066ff}{输出样例})

1

(color{#0066ff}{数据范围与提示})

1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100

(color{#0066ff}{ 题解 })

题意类似于P2891 [USACO07OPEN]吃饭Dining

#include<bits/stdc++.h>
#define LL long long
LL in() {
	char ch; LL x = 0, f = 1;
	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
	return x * f;
}
const int maxn = 1e4;
struct node {
	int to, dis;
	node *nxt, *rev;
	node(int to = 0, int dis = 0, node *nxt = NULL, node *rev = NULL)
		: to(to), dis(dis), nxt(nxt), rev(rev) {}
	void *operator new(size_t) {
		static node *S = NULL, *T = NULL;
		return (S == T) && (T = (S = new node[1024]) + 1024), S++;
	}
}*head[maxn], *cur[maxn];
int dep[maxn];
int n, s, t, na, nb;
void add(int from, int to, int dis) {
	head[from] = new node(to, dis, head[from], NULL);
}
void link(int from, int to, int dis) {
	add(from, to, dis),	add(to, from, 0);
	(head[from]->rev = head[to])->rev = head[from];
}
bool bfs() {
	for(int i = s; i <= t; i++) dep[i] = 0, cur[i] = head[i];
	std::queue<int> q;
	q.push(s);
	dep[s] = 1;
	while(!q.empty()) {
		int tp = q.front(); q.pop();
		for(node *i = head[tp]; i; i = i->nxt) 
			if(!dep[i->to] && i->dis)
				dep[i->to] = dep[tp] + 1, q.push(i->to);
	}
	return dep[t];
}
int dfs(int x, int change) {
	if(x == t || !change) return change;
	int flow = 0, ls;
	for(node *i = cur[x]; i; i = i->nxt) {
		cur[x] = i;
		if(dep[i->to] == dep[x] + 1 && (ls = dfs(i->to, std::min(change, i->dis)))) {
			change -= ls;
			flow += ls;
			i->dis -= ls;
			i->rev->dis += ls;
			if(!change) break;
		}
	}
	return flow;
}
int dinic() {
	int flow = 0;
	while(bfs()) flow += dfs(s, 0x7fffffff);
	return flow;
}
int main() {
	n = in(), na = in(), nb = in();
	s = 0, t = n + n + na + nb + 1;
	for(int i = 1; i <= na; i++) link(s, i, 1);
	for(int i = 1; i <= nb; i++) link(n + na + n + i, t, 1);
	for(int i = 1; i <= n; i++) link(na + i, na + n + i, 1);
	for(int i = 1; i <= n; i++) 
		for(int j = 1; j <= na; j++) 
			if(in()) link(j, na + i, 1);
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= nb; j++)
			if(in()) link(na + n + i, na + n + n + j, 1);
	printf("%d
", dinic());
	return 0;
}
原文地址:https://www.cnblogs.com/olinr/p/10336666.html