博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ - 3281 Dining (最大流dinic)
阅读量:5256 次
发布时间:2019-06-14

本文共 4075 字,大约阅读时间需要 13 分钟。

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 
Fiintegers 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 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 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.
 
很巧妙的建图的方法,建立s->food->cow1->cow2->drink->T的图,然后跑最大流即可,S到food以及drink到T的流量设为1,这样保证每种食物和饮料只被使用一次,且如果我们完成一次流量为1的流,一定经过一个drink和food.
然后我们需要进行拆点,拆点的对象是牛,因为如果我们只把每一头牛作为一个点,那么它在跑最大流的时候,可能同一个牛会被多次使用,所以我们需要进行拆点,将牛的点分为cow1和cow2,且它们之间的流量为1.这样就可以解决这个问题
并且,题目中是两种物品的挑选,我们可以类推到如果是三种,甚至更多物品的挑选,我们可以采用类似的方法,只需要将除了源点和汇点连接的对象之外都进行拆点操作即可。
 
代码如下:
#include 
#include
#include
#include
using namespace std;int n,f,d,b,c,x,n1,n2;struct Edge{ int to,Next,flow;}edge[100010];int first[17000],size1;int dl[17100],deep[17100];void add_edge(int x,int y,int z){ size1++; edge[size1].to=y; edge[size1].flow=z; edge[size1].Next=first[x]; first[x]=size1;}bool bfs(int S,int T){ int head=0,tail=1; memset(deep,0,sizeof(deep)); deep[S]=1,dl[1]=S; while(head!=tail){ int v=dl[++head]; for(int u=first[v];u;u=edge[u].Next){ if(!deep[edge[u].to]&&edge[u].flow>0){ deep[edge[u].to]=deep[v]+1; dl[++tail]=edge[u].to; } } } return deep[T];}int dfs(int now,int aim,int flow){ if(now==aim)return flow; int F=0; for(int u=first[now];u&&flow;u=edge[u].Next){ if(deep[edge[u].to]==deep[now]+1&&edge[u].flow){ int ret=dfs(edge[u].to,aim,min(flow,edge[u].flow)); F+=ret;flow-=ret;edge[u].flow-=ret,edge[u^1].flow+=ret; } } if(!F)deep[now]=-2; return F;}int maxflow(int S,int T){ int ret=0; while(bfs(S,T)){ ret+=dfs(S,T,(1<<30)); } return ret;}int S,T;int main(){ while(scanf("%d%d%d",&n,&f,&d)!=EOF) { memset(first,0,sizeof(first)); S=0,T=f+n*2+d+1; size1=1; for(int i=1;i<=n;i++) { scanf("%d%d",&n1,&n2); for(int k=1;k<=n1;k++) { scanf("%d",&x); add_edge(x,f+i*2-1,1); add_edge(f+i*2-1,x,0); } add_edge(f+i*2-1,f+i*2,1); add_edge(f+i*2,f+i*2-1,0); for(int k=1;k<=n2;k++) { scanf("%d",&x); add_edge(f+i*2,f+n*2+x,1); add_edge(f+n*2+x,f+i*2,0); } } for(int i=1;i<=f;i++) { add_edge(S,i,1); add_edge(i,S,0); } for(int i=1;i<=d;i++) { add_edge(f+n*2+i,T,1); add_edge(T,f+n*2+i,0); } printf("%d\n",maxflow(S,T)); } return 0;}

 

 

转载于:https://www.cnblogs.com/a249189046/p/9781400.html

你可能感兴趣的文章
stap-prep 需要安装那些内核符号
查看>>
转载:ASP.NET Core 在 JSON 文件中配置依赖注入
查看>>
socket初识
查看>>
磁盘测试工具
查看>>
代码变量、函数命名神奇网站
查看>>
redis cli命令
查看>>
Problem B: 占点游戏
查看>>
python常用模块之sys, os, random
查看>>
HDU 2548 A strange lift
查看>>
Linux服务器在外地,如何用eclipse连接hdfs
查看>>
react双组件传值和传参
查看>>
[Kaggle] Sentiment Analysis on Movie Reviews
查看>>
价值观
查看>>
mongodb命令----批量更改文档字段名
查看>>
使用&nbsp;SharedPreferences 分类: Andro...
查看>>
TLA+(待续...)
查看>>
题解: [GXOI/GZOI2019]与或和
查看>>
MacOS copy图标shell脚本
查看>>
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>