POJ-3281 Dining (网络流 最大流 建图) ʕ •ᴥ•ʔ

本文介绍了一个经典的匹配问题——牛饲料分配。农民需为每头牛分配其偏好的食物与饮料,且每种食物和饮料只能分配给一头牛。文章详细阐述了解决方案,包括构建网络流图的具体步骤和代码实现。

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

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Sample Output

3
  • 1
  • 2

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.

思路

先说题意:

农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100)
种饮料。每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛。最多能有多少头牛可以同时得到喜欢的食物和饮料?

因为每种食物或者每种饮料都只能分配给一头牛,所以我们要进行拆点,来保证饮料和食物只从一条边上面经过,不拆点不能保证牛只能选择一种食物和一种饮料这一条件。即限定牛结点的容量为1。那么我们来建图:

需要建立一个源头,汇头。

网络流难在建图 然后直接敲板子

我们需要搞清楚他们各自的范围

  • 牛:1~2∗n
  • 食物:2∗n+1~2∗n+f
  • 饮料:2∗n+f+1~2∗n+f+d
  • 超级源点:0
  • 超级汇点:2∗n+f+d+1

是不是有些迷 (反正当时我迷了) 下面我们通过图来解释:

1~8是牛 f 为食物 b为饮料

 

 然后如下图

 

 根据题意连接各边

 

 

然后就可以啦 然后直接模板! 模板最好自己存一个 模板很好理解 敲多了也就记住了

 

#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <map>
#include<queue> 
#define ll long long
#define pi acos(-1.0)
#define N 500
using namespace std;
int x[500][500];
int vis[500];
int s,e;
int bfs()//这个bfs 让我想起了 spfa 算法 
{
	queue<int>q;
	memset(vis,0,sizeof(vis));
	vis[s]=1;
	q.push(s);
	while(!q.empty())
	{
		int temp=q.front();
		q.pop();
		for(int i=1;i<=e;i++)
		{
			if(!vis[i]&&x[temp][i])
			{
				vis[i]=vis[temp]+1;
				q.push(i);
			}
		}
	}
	if(vis[e]>0)
	return 1;
	return 0;
}
int dfs(int here,int h)
{
	if(here==e)
	return h;
	int ans;
	for(int i=1;i<=e;i++)
	{
		if(x[here][i]&&vis[i]==vis[here]+1&&(ans=dfs(i,min(h,x[here][i]))))
		{
			x[here][i]-=ans;
			x[i][here]+=ans;
			return ans;
		}
	}	
	return 0;
} 
int main()
{
	int n,f,d;
	cin>>n>>f>>d;
	memset(x,0,sizeof(x));
	// 建图 
	for(int i=1;i<=n;i++)
	{
		x[2*i-1][2*i]=1;
	}
	for(int i=1;i<=f;i++)
	{
		x[0][2*n+i]=1;
	}
	for(int i=1;i<=d;i++)
	{
		x[2*n+f+i][2*n+f+d+1]=1;
	}
	//根据要求连接对应点 
	for(int i=1;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		for(int j=1;j<=a;j++)
		{
			int c;
			cin>>c;
			x[2*n+c][2*i-1]=1;	 
		}
		for(int j=1;j<=b;j++)
		{
			int c;
			cin>>c;
			x[2*i][2*n+f+c]=1;
		}
	}
	int ans=0;
	s=0,e=2*n+f+d+1;
	while(bfs())
	{
		ans+=dfs(s,e);
	}
	cout<<ans<<endl;
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值