描述

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

样例输入

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

样例输出

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.
题意
N头牛,每头牛有F个喜欢的食物,D个喜欢的饮料,每种食物每瓶饮料只能供一头牛,问最多几头牛能感到满足(即又有喜欢的食物也有喜欢的饮料)
题解
首先食物连源点S流量1,饮料连汇点T流量1,食物连喜欢的牛流量1,饮料也连喜欢的牛流量1
这时候有个问题就是牛只能流出最多1的流量和饮料匹配,所以我们可以把牛拆成左牛和右牛,这样保证从牛最多流出1的流量
那么食物连左牛流量1,左牛连右牛流量1,右牛连饮料流量1
建完图,跑最大流算法即可
代码
 #include<bits/stdc++.h>
using namespace std; const int N=1e5+;
const int M=2e5+;
int n,m,S,T;
int deep[N],q[];
int FIR[N],TO[M],CAP[M],COST[M],NEXT[M],tote; void add(int u,int v,int cap)
{
TO[tote]=v;
CAP[tote]=cap;
NEXT[tote]=FIR[u];
FIR[u]=tote++; TO[tote]=u;
CAP[tote]=;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
bool bfs()
{
memset(deep,,sizeof deep);
deep[S]=;q[]=S;
int head=,tail=;
while(head!=tail)
{
int u=q[++head];
for(int v=FIR[u];v!=-;v=NEXT[v])
{
if(CAP[v]&&!deep[TO[v]])
{
deep[TO[v]]=deep[u]+;
q[++tail]=TO[v];
}
}
}
return deep[T];
}
int dfs(int u,int fl)
{
if(u==T)return fl;
int f=;
for(int v=FIR[u];v!=-&&fl;v=NEXT[v])
{
if(CAP[v]&&deep[TO[v]]==deep[u]+)
{
int Min=dfs(TO[v],min(fl,CAP[v]));
CAP[v]-=Min;CAP[v^]+=Min;
fl-=Min;f+=Min;
}
}
if(!f)deep[u]=-;
return f;
}
int maxflow()
{
int ans=;
while(bfs())
ans+=dfs(S,<<);
return ans;
}
void init()
{
tote=;
memset(FIR,-,sizeof FIR);
}
int main()
{
int v,cow,F,D,food,drink;
init();
cin>>cow>>F>>D;
S=F+*cow+D+,T=S+;
for(int i=;i<=F;i++)
add(S,i,);
for(int i=F+*cow+;i<=F+*cow+D;i++)
add(i,T,);
for(int i=F+;i<=F+cow;i++)
{
add(i,cow+i,);
cin>>food>>drink;
while(food--)cin>>v,add(v,i,);
while(drink--)cin>>v,add(cow+i,F+*cow+v,);
}
cout<<maxflow();
return ;
}

TZOJ 1705 Dining(拆点最大流)的更多相关文章

  1. poj 3281 Dining 拆点 最大流

    题目链接 题意 有\(N\)头牛,\(F\)个食物和\(D\)个饮料.每头牛都有自己偏好的食物和饮料列表. 问该如何分配食物和饮料,使得尽量多的牛能够既获得自己喜欢的食物又获得自己喜欢的饮料. 建图 ...

  2. hdu4289 最小割最大流 (拆点最大流)

    最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割. Problem Descript ...

  3. Control(拆点+最大流)

    Control http://acm.hdu.edu.cn/showproblem.php?pid=4289 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  4. BZOJ 1877 晨跑 拆点费用流

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1877 题目大意: Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧 ...

  5. Risk UVA - 12264 拆点法+最大流+二分 最少流量的节点流量尽量多。

    /** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai ...

  6. POJ3281 Dining(拆点构图 + 最大流)

    题目链接 题意:有F种食物,D种饮料N头奶牛,只能吃某种食物和饮料(而且只能吃特定的一份) 一种食物被一头牛吃了之后,其余牛就不能吃了第一行有N,F,D三个整数接着2-N+1行代表第i头牛,前面两个整 ...

  7. POJ 3281 Dining (拆点)【最大流】

    <题目链接> 题目大意: 有N头牛,F种食物,D种饮料,每一头牛都有自己喜欢的食物和饮料,且每一种食物和饮料都只有一份,让你分配这些食物和饮料,问最多能使多少头牛同时获得自己喜欢的食物和饮 ...

  8. <每日一题>Day 9:POJ-3281.Dining(拆点 + 多源多汇+ 网络流 )

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24945   Accepted: 10985 Descript ...

  9. HDU 3572 Task Schedule(拆点+最大流dinic)

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. 轻量级Java持久化框架,Hibernate完美助手,Minidao 1.6.2版本发布

    Minidao 1.6.2 版本发布,轻量级Java持久化框架(Hibernate完美助手) Minidao产生初衷? 采用Hibernate的J2EE项目都有一个痛病,针对复杂业务SQL,hiber ...

  2. (笨方法)利用stat函数实现ls -l filename

    学习了一段时间的Linux了,但是我感觉我做不出来啥子,后头选择利用系统IO函数实现命令,先从ls走起吧.先来看看ls -l filename给我们显示了什么吧 : 可以看到,一共八项:文件类型.用户 ...

  3. win10 任务栏锁定,win键没反应

    现象:之前用win10,换成win10专业版后,安装360优化系统,过了几天后突然发现任务栏好像被锁定一般,按windows键没有任何反应,任务栏打开的文件,图片等等右键也没有反应,讲道理应该有关闭选 ...

  4. Easy-to-Learn English Travel Phrases and Vocabulary!

    Easy-to-Learn English Travel Phrases and Vocabulary! Share Tweet Share Tagged With: Real Life Englis ...

  5. Haskell语言学习笔记(81)Data.Typeable

    Data.Typeable 利用 Data.Typeable,可以打印动态类型信息. class Typeable (a :: k) where typeRep# :: TypeRep a typeR ...

  6. k8s的Deployment 滚动升级

    首先定义一个Deployment,并创建它 apiVersion: apps/v1beta1 kind: Deployment metadata: name: house-live spec: rep ...

  7. Python在cmd上打印彩色文字

    在Windows上编写python程序时,有时候需要对输出的文字颜色进行设置,特别是日志显示,不同级别的日志设置不同的颜色进行展示可以直观查看.本文主要描述通过ctypes.windll.kernel ...

  8. [jQ/PHP]再谈使用JS数组储值的运用(提交PHP处理)

    --------------------------------------------------------------------------------------------------- ...

  9. C# 模拟多文件上传

    原地址:http://www.cnblogs.com/greenerycn/archive/2010/05/15/csharp_http_post.html 1.客户端代码 用winform写的 pr ...

  10. 构建缓存gradle

    结合Kotlin使用Gradle build cache 宛丘之上兮 关注 2018.03.11 00:21* 字数 1177 阅读 505评论 5喜欢 4 在2017年4月,Gradle发布了bui ...