poj 3281 Dining(网络流)

题意:有N只牛,F种食物和D种饮料,每只牛最多只能吃它们喜爱的一种食物和一种饮料,问最多能满足多少只牛,所以建图时,如果是源点(0)——食物——牛——饮料——汇点(t),权值全为1,这样会导致多条流从食物——牛——饮料,这样就不符合题意的每只牛最多只能吃它们喜爱的一种食物和一种饮料,所以当建图是源点(0)——食物——牛——牛——饮料——汇点(t)时,就只能有一条路从食物——牛——牛——饮料,建如下图,然后用最简单的KM网络流算法就行了,模板紫书有,建点的标记为,源点为0,食物为1到F,牛为F+1到F+2N,饮料为F+2N+1到F+2N+D,汇点为t。

 

poj <wbr>3281 <wbr>:Dining <wbr>(网络流)

Dining Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu

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

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.
AC代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#define maxn 405
#define INF 999999999
using namespace std;
struct Edge
{
	int from, to, cap, flow;
	Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
int N, F, D;
vector<Edge> edges;
vector<int> G[maxn];
int a[maxn];
int p[maxn];
void init()
{
	for (int i = 0; i < 405; i++)
		edges.clear();
	memset(p, 0, sizeof(p));
}
void AddEdge(int from, int to, int cap)
{
	int mm;
	edges.push_back(Edge(from, to, cap, 0));
	edges.push_back(Edge(to, from, 0, 0));
	mm = edges.size();
	G[from].push_back(mm - 2);
	G[to].push_back(mm - 1);
}
int Maxflow(int s, int t)
{
	int flow = 0;
	while (true)
	{
		memset(a, 0, sizeof(a));
		queue<int> q;
		q.push(s);
		a[s] = INF;
		while (!q.empty())
		{
			int x = q.front(); q.pop();
			for (int i = 0; i < G[x].size(); i++)
			{
				Edge& e = edges[G[x][i]];
				if (!a[e.to] && e.cap > e.flow)
				{
					p[e.to] = G[x][i];
					a[e.to] = min(a[x], e.cap - e.flow);
					q.push(e.to);
				}
			}
			if (a[t])
				break;
		}
		if (!a[t]) break;
		for (int u = t; u != s; u = edges[p[u]].from)
		{
			edges[p[u]].flow += a[t];
			edges[p[u] ^ 1].flow -= a[t];
		}
		flow += a[t];
	}
	return flow;
}
int main()
{
	while (scanf("%d%d%d", &N, &F, &D) != EOF)
	{
		init();
		int s = 0, t = F + 2 * N + D + 1;
		int ff, dd, aa, bb;
		for (int i = 1; i <= F; i++)
			AddEdge(0, i, 1);
		for (int i = 1; i <= N; i++)
		{
			scanf("%d%d", &ff, &dd);
			for (int j = 0; j < ff; j++)
			{
				scanf("%d", &aa);
				AddEdge(aa, F + i, 1);
			}
			for (int j = 0; j < dd; j++)
			{
				scanf("%d", &aa);
				AddEdge(F + N + i, F + 2 * N + aa, 1);
			}
		}
		for (int i = 1; i <= D; i++)
			AddEdge(F + 2 * N + i, t, 1);
		for (int i = 1; i <= N; i++)
			AddEdge(F + i, F + N + i, 1);
		printf("%d\n",Maxflow(s, t));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值