POJ - 3281 Dining (拆点+dinic最大流)

Dining

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 22184 Accepted: 9851

Description

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).

Input

Line 1: Three space-separated integers: NF, 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.

Output

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

Sample Input

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

Sample Output

3

Hint

One way to satisfy three cows is: 
Cow 1: no meal 
Cow 2: Food #2, Drink #2 
Cow 3: Food #1, Drink #1 
Cow 4: Food #3, Drink #3 
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.

 

     农夫有f个食物和d个饮料,有n头牛,每头牛有各自喜欢的食物和饮料,农夫的食物和饮料同时只能给一个人,问最大的同时满足食物和饮料的牛的个数是多少;

     但是第一眼看到的时候本来是想用二分图做的,之后发现,牛喜欢的食物和饮料是由多种的,在二分图匹配的时候很难去判断不同的边是否要删除(因为一头牛可以后多种边)以及最后技术也是个问题。假如一头牛只有一个喜欢的食物以及饮料的话,就可以用二分图了;

     准备用网络流的时候,发现把牛只当作一个点是不行的,会出现多个食物以及饮料通过这头牛进行匹配的情况,比如一头牛喜欢食物1,2,喜欢饮料1,2,在不拆点的情况下,会有1-牛-1,以及2-牛-2 的情况出现,最后输出就会出现2了。这样拆点之后,中间建立一条权值为1的边,这样就能保证最多只有一对食物及饮料像配对了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 100010;
const int maxm = 1000100;
struct fuck {
	int v, w, ne;
}ed[maxm];
int n, m, cnt;
int head[maxn], dis[maxn], cur[maxn];
void init() {
	cnt = 0;
	memset(head, -1, sizeof(head));
}
void add(int u, int v, int w) {
	ed[cnt].v = v; ed[cnt].w = w;
	ed[cnt].ne = head[u]; head[u] = cnt++;
	ed[cnt].v = u; ed[cnt].w = 0;
	ed[cnt].ne = head[v]; head[v] = cnt++;
}
int bfs(int st, int en) {
	queue<int>q;
	memset(dis, 0, sizeof(dis));
	dis[st] = 1;
	q.push(st);
	while (!q.empty()) {
		int u = q.front(); q.pop();
		if (u == en)return 1;
		for (int s = head[u]; ~s; s = ed[s].ne) {
			int v = ed[s].v;
			if (dis[v] == 0 && ed[s].w > 0) {
				dis[v] = dis[u] + 1; q.push(v);
			}
		}
	}
	return dis[en] != 0;
}
int dfs(int st, int en, int flow) {
	int ret = flow, a;
	if (st == en || flow == 0)return flow;
	for (int &s = cur[st]; ~s; s = ed[s].ne) {
		int v = ed[s].v;
		if (dis[v] == dis[st] + 1 && (a = dfs(v, en, min(ret, ed[s].w)))) {
			ed[s].w -= a;
			ed[s ^ 1].w += a;
			ret -= a;
			if (!ret)break;
		}
	}
	if (ret == flow)dis[st] = 0;
	return flow - ret;
}
int dinic(int st, int en) {
	int ans = 0;
	while (bfs(st, en)) {
		for (int s = 0; s <= n; s++) 
			cur[s] = head[s];
		ans += dfs(st, en, inf);
	}
	return ans;
}
int main()
{
	int a, b, c;
	while (~scanf("%d%d%d", &a, &b, &c))
	{
		n = 2 * a + b + c + 1;
		init();
		for (int s = 1; s <= a; s++) {
			add(s + b, s + b + a, 1);
			int sua, sub, t; scanf("%d%d", &sua, &sub);
			while (sua--) {
				scanf("%d", &t); add(t, s + b, 1);
			}
			while (sub--) {
				scanf("%d", &t); add(s + b + a, t + b + 2 * a, 1);
			}
		}
		for (int s = 1; s <= b; s++)
			add(0, s, 1);
		for (int s = 1; s <= c; s++)
			add(b + 2 * a + s, n, 1);
		int ans = dinic(0, n);
		printf("%d\n", ans);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值