ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Dining G

2022-06-04 09:33:53  阅读:171  来源: 互联网

标签:idx cow food drink Dining cows hd


题目描述

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

输入格式

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.

输出格式

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

样例 #1

样例输入 #1

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

3

提示

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.

看到这么小的数据,还有这种选来选去的东西,考虑网络流。

在建图中把牛放在中间,前面放食物,后面放饮料。

由源点向每个食物放一条流量为1的边,每个饮料向汇点放一条流量为1的边。然后把牛拆成两份,中间流量为1.牛和喜欢的饮料食物连边即可。跑一次最大流,每个牛,饮料,食物都会选一次。刚好就可以跑出答案。

#include<bits/stdc++.h>
using namespace std;
int idx=1,hd[200005],k,v[200005],q[200005],thd[200005],kt,l,r,n,f,d,x,y,z,ans,p,qq,s,t;
struct edge{
	int v,nxt,f;
}e[2000005];
void add_edge(int u,int v)
{
	e[++idx]=(edge){v,hd[u],1};
	hd[u]=idx;
	e[++idx]=(edge){u,hd[v],0};
	hd[v]=idx;
}
int bfs()
{
	memcpy(hd,thd,sizeof(hd));
	memset(v,0,sizeof(v));
	q[l=r=1]=0,v[0]=1;
	while(l<=r)
	{
		for(int i=hd[q[l]];i;i=e[i].nxt)
		{
			if(e[i].f&&!v[e[i].v])
			{
				v[e[i].v]=v[q[l]]+1;
				q[++r]=e[i].v;
			}
		}
		++l;
	}
	return v[t];
}
int dfs(int x,int flow)
{
	if(x==t)
		return flow;
	for(int&i=hd[x];i;i=e[i].nxt)
	{
		if(v[x]+1==v[e[i].v]&&min(flow,e[i].f))
		{
			if(kt=dfs(e[i].v,min(flow,e[i].f)))
			{
				e[i^1].f+=kt;
				e[i].f-=kt;
				return kt;
			}
		}
	}
	return 0;
}
int main()
{
	scanf("%d%d%d",&n,&f,&d);
	s=0,t=1+f+n+d+1;
	for(int i=1;i<=f;i++){
        add_edge(s,1+i);
    }
    for(int i=1;i<=d;i++){
        add_edge(1+f+n+i,t);
    }
    for(int i=1;i<=n;i++){
        add_edge(1+f+i,1+f+n+d+1+i);
    }
    for(int i=1;i<=n;i++){
        scanf("%d%d",&x,&y);
        for(int j=1;j<=x;j++)
		{
            scanf("%d",&z);
            add_edge(1+z,1+f+i);
        }
        for(int j=1;j<=y;j++)
		{
            scanf("%d",&z);
            add_edge(1+f+n+d+1+i,1+f+n+z);
        }
    }
	memcpy(thd,hd,sizeof(hd));
	while(bfs())
		while(k=dfs(0,2147483647))
			ans+=k;
	printf("%d",ans);
	return 0;
} 

标签:idx,cow,food,drink,Dining,cows,hd
来源: https://www.cnblogs.com/mekoszc/p/16341268.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有