B - Dining POJ - 3281(最大流)

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.

农场有N头牛,每头牛都有要吃的食物和要喝的饮料,农夫准备了F种食物和D种饮料,第一行输入N,F,D,第2到N+1行每行输入Fi和Di,后面接着Fi个数和Di个数,代表第i头牛能吃Fi种食物,喝Di种饮料,接着的Fi个数代表食物号,Di个数代表饮料号,一种食物和饮料只有一个,问怎么分配可以使最多数量的牛满足它们的要求(即吃到能吃的食物并喝到能喝的饮料)。

建图,首先设置一个超级源点和超级汇点,首先将每种食物与源点建一条容量为1的边,接着将牛拆点(因为要保证一头牛只吃一种食物喝一种饮料),将前面的点与这头牛要吃的食物建一条容量为1的边,前面的点与后面的点建一条容量为1的边,后面的点与要喝的饮料建一条容量为1的边,最后将饮料与汇点建一条容量为1的边,因此将源点设置为0,汇点设置为N*2+F+D+1,然后套模板。
dinic算法

#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=10000+5;
const int maxm=100000+5;
int head[maxn],dis[maxn],ne,n,m,S,T,ans,f,d;
void init()
{
	ans=0;
	ne=0;
	memset(head,-1,sizeof(head));
}
struct edge{int v,w,nxt;}G[maxm<<1];
void add(int u,int v,int w)
{
	G[ne]=(edge){v,w,head[u]};
	head[u]=ne++;
}
int bfs()
{
	memset(dis,-1,sizeof(dis));
	queue<int> q;
	q.push(S);
	dis[S]=0;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=G[i].nxt)
		{
			int v=G[i].v;
			if(dis[v]==-1&&G[i].w>0)
			{
				dis[v]=dis[u]+1;
				q.push(v);
			}
		}
	}
	return dis[T]!=-1;
}
int dfs(int u,int exp)
{
	if(u==T) return exp;
	int flow=0,tmp=0;
	for(int i=head[u];i!=-1;i=G[i].nxt)
	{
		int v=G[i].v;
		if(dis[v]==dis[u]+1&&G[i].w>0)
		{
			tmp=dfs(v,min(G[i].w,exp));
			if(!tmp) continue;
			exp-=tmp;
			flow+=tmp;
			G[i].w-=tmp;
			G[i^1].w+=tmp;
			if(!exp) break;
		}
	}
	return flow;
}
int main()
{
	init();
	scanf("%d%d%d",&n,&f,&d);
	S=0;
	T=2*n+f+d+1;
	for(int i=1;i<=n;i++)
	{
		add(i,i+n,1);
		add(i+n,i,0);
	}
	for(int i=1;i<=f;i++)
	{
		add(0,2*n+i,1);
		add(2*n+i,0,0);
	}
	for(int i=1;i<=d;i++)
	{
		add(2*n+f+i,T,1);
		add(T,2*n+f+i,0);
	}
	for(int i=0;i<n;i++)
	{
		int fi,di;
		int ff[101],dd[101];
		scanf("%d%d",&fi,&di);
		for(int j=1;j<=fi;j++)
		{
			scanf("%d",&ff[j]);
			add(ff[j]+2*n,i+1,1);
			add(i+1,ff[j]+n*2,0);
		}
		for(int j=1;j<=di;j++)
		{
			scanf("%d",&dd[j]);
			add(i+1+n,dd[j]+2*n+f,1);
			add(dd[j]+2*n+f,i+1+n,0);
		}
	}
	while(bfs())
	{
		ans+=dfs(S,inf);
	}
	printf("%d\n",ans);
}

ISAP算法

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e4+5;
const int maxm=1e5+5;
int head[maxn],cur[maxn],dep[maxn],ne,pre[maxn],num[maxn],n,m,S,T,maxflow,F,D;
queue<int> q;
struct edge
{
	int next,to,cost;
}e[maxm*2];
void add(int u,int v,int w)
{
	e[ne].to=v;
	e[ne].cost=w;
	e[ne].next=head[u];
	head[u]=ne++;
	e[ne].to=u;
	e[ne].cost=0;
	e[ne].next=head[v];
	head[v]=ne++;
}
void bfs(int t)
{
	while(!q.empty()) q.pop();
	for(int i=0;i<=T;i++) cur[i]=head[i];
	for(int i=0;i<=T;i++) dep[i]=T+1;
	dep[t]=0;
	q.push(t);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(dep[e[i].to]==T+1&&e[i^1].cost)
			{
				dep[e[i].to]=dep[u]+1;
				q.push(e[i].to);
			}
		}
	}
}
int addflow(int s,int t)
{
	int ans=INF,u=t;
	while(u!=s)
	{
		ans=min(ans,e[pre[u]].cost);
		u=e[pre[u]^1].to;
	}
	u=t;
	while(u!=s)
	{
		e[pre[u]].cost-=ans;
		e[pre[u]^1].cost+=ans; 
		u=e[pre[u]^1].to;
	}
	return ans;
}
void ISAP(int s,int t)
{
	int u=s;
	bfs(t);
	for(int i=0;i<=T;i++) num[dep[i]]++;//
	while(dep[s]<T+1)
	{
		if(u==t) 
		{
			maxflow+=addflow(s,t);
			u=s;
		}
		int flag=0;
		for(int &i=cur[u];i!=-1;i=e[i].next)
		{
			if(dep[u]==dep[e[i].to]+1&&e[i].cost)
			{
				flag=1;
				pre[e[i].to]=i;
				u=e[i].to;
				break;
			}
		}
		if(!flag)
		{
			int minn=T;
			for(int i=head[u];i!=-1;i=e[i].next)
			{
				if(e[i].cost)
				{
					minn=min(minn,dep[e[i].to]);
				}
			}
			if((--num[dep[u]])==0) break;
			dep[u]=minn+1;
			num[dep[u]]++;
			cur[u]=head[u];
			if(u!=s) u=e[pre[u]^1].to;
		}
	}
}

void init()
{
	memset(head,-1,sizeof(head));
	ne=0;
	memset(num,0,sizeof(num));
	maxflow=0;
}
int main()
{
	while(~scanf("%d%d%d",&n,&F,&D))
	{
		init();
		S=0;
		T=n*2+F+D+1;
		for(int i=1;i<=F;i++) add(S,i,1);
		for(int i=1;i<=D;i++) add(i+n*2+F,T,1);
		for(int i=1;i<=n;i++) add(i+F,i+F+n,1);
		for(int i=1;i<=n;i++)
		{
			int fn,dn;
			scanf("%d%d",&fn,&dn);
			for(int j=1;j<=fn;j++)
			{
				int ffn;
				scanf("%d",&ffn);
				add(ffn,i+F,1);
			}
			for(int j=1;j<=dn;j++)
			{
				int ddn;
				scanf("%d",&ddn);
				add(i+F+n,ddn+F+n*2,1);
			}
		}
		ISAP(S,T);
		printf("%d\n",maxflow);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值