POJ 3281 Dining

本文介绍了一种利用最大流算法解决喂牛问题的方法。该问题要求在有限的食物和饮料种类中尽可能多地满足每头牛特定的饮食偏好,确保每头牛都能得到独特的一份食物和一份饮料。通过构建网络流图并应用Dinic算法或SAP算法,可以找到最优分配方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 preparedD (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.

Source

USACO 2007 Open Gold

解析
这道题转换为最大流模型,但是为了保证一头牛只吃一道菜喝一种饮料就要将牛拆点。如图


//dinic
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>

using namespace std;

#define INF ~0u>>2

int N,F,D,Map[410][410],SuperS,SuperT,dis[410];

void readdata()
{
	memset(Map,0,sizeof(Map));
	memset(dis,0,sizeof(dis));

	SuperS=0; SuperT=2*N+F+D+1;
	for(int i=1;i<=F;i++) Map[SuperS][i]=1;
	for(int i=1;i<=D;i++) Map[F+2*N+i][SuperT]=1;

	for(int i=1;i<=N;i++)
	{
		Map[F+i][F+N+i]=1;
		int fi,di; scanf("%d%d",&fi,&di);
		for(int j=1;j<=fi;j++)
		{
			int a; scanf("%d",&a);
			Map[a][F+i]=1;
		}
		for(int j=1;j<=di;j++)
		{
			int a; scanf("%d",&a);
			Map[F+N+i][F+2*N+a]=1;
		}
	}
}

bool bfs()
{
	memset(dis,0,sizeof(dis));
	queue<int> q;
	dis[SuperS]=1;q.push(SuperS);
	while(!q.empty())
	{
		int v=q.front();q.pop();
		for(int u=SuperS;u<=SuperT;u++)
			if(!dis[u] && Map[v][u]>0)
			{
				dis[u]=dis[v]+1;
				q.push(u);
				if(u==SuperT) return 1;
			}
	}
	return 0;
}

int dfs(int v,int flow)
{
	if(v==SuperT || flow==0) return flow;

	int ans=0;
	for(int u=SuperS;u<=SuperT;u++)
		if(dis[v]+1==dis[u] && Map[v][u]>0)
		{
			int tmp=dfs(u,min(flow-ans,Map[v][u]));
			ans+=tmp;
			Map[v][u]-=tmp; Map[u][v]+=tmp;
		}

	return ans;
}

void dinic()
{
	int ans=0;
	while(bfs()) 
		ans+=dfs(SuperS,INF);
	printf("%d",ans);
}

int main()
{
	freopen("poj3281.in","r",stdin);

	while(scanf("%d%d%d",&N,&F,&D)==3)
	{
		readdata();
		dinic();
		printf("\n");
	}

	while(1);
	return 0;
}

//sap
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>

using namespace std;

#define INF ~0u>>2

int N,F,D,SuperS,SuperT,Map[410][410],dis[410],vd[410];

void readdata()
{
	memset(dis,0,sizeof(dis));
	memset(Map,0,sizeof(Map));
	memset(vd,0,sizeof(vd));
	SuperS=0; SuperT=F+2*N+D+1;

	for(int i=1;i<=F;i++) Map[SuperS][i]=1;
	for(int i=1;i<=D;i++) Map[F+2*N+i][SuperT]=1;

	for(int i=1;i<=N;i++)
	{
		Map[F+i][F+N+i]=1;
		int fi,di;scanf("%d%d",&fi,&di);
		for(int j=1;j<=fi;j++)
		{
			int a;scanf("%d",&a);
			Map[a][F+i]=1;
		}

		for(int j=1;j<=di;j++)
		{
			int a;scanf("%d",&a);
			Map[F+N+i][F+N*2+a]=1;
		}
	}
}

int dfs(int v,int flow)
{
	if(v==SuperT) return flow;

	int ans=0;
	for(int u=SuperS;u<=SuperT;u++)
		if(dis[v]==dis[u]+1 && Map[v][u]>0)
		{
			int tmp=dfs(u,min(flow-ans,Map[v][u]));
			ans+=tmp;
			Map[v][u]-=tmp;Map[u][v]+=tmp;
			if(flow==ans) return ans;
		}
	if(dis[SuperS]>=F+D+2*N+2) return ans;
	if(--vd[dis[v]]<=0) dis[SuperS]=F+D+2*N+2;
	dis[v]++;vd[dis[v]]++;
	return ans;
}

void sap()
{
	vd[SuperS]=F+D+2*N+2;
	int ans=0;
	while(dis[SuperS]<F+D+2*N+2) 
		ans+=dfs(SuperS,INF);

	printf("%d",ans);
}

int main()
{
	freopen("poj3281.in","r",stdin);

	while(scanf("%d%d%d",&N,&F,&D)==3)
	{
		readdata();
		sap();
		printf("\n");
	}

	while(1);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值