Dining

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

Description

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: NF, 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.

Source

  都在代码里了,干/drink.jpg

/*
POJ 3281 最大流 + 拆点
源点 -> food -> 牛左 -> 牛右 -> Drink -> 汇点
建图时注意将上面的所有边的容量设置为1,这样就可以保证一头牛
只吃一种食物喝一种饮料,转化之后肯定就知道是最大流了 拆点技巧:为了保证同一个东西满足两个条件,则将其拆分为两个
公共边的点分别进行求解。 嘤嘤嘤,为什么做完之后感觉这个题其实建图也是很好想的,就是拆个点,原谅自己太差
唉,都是幻觉
*/ #include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std; const int maxn=+, INF = 0x3f3f3f3f;
struct Edge
{
Edge(){}
Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}
int from,to,cap,flow;
}; struct Dinic
{
int n,m,s,t; //结点数,边数(包括反向弧),源点与汇点编号
vector<Edge> edges; //边表 edges[e]和edges[e^1]互为反向弧
vector<int> G[maxn]; //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn]; //BFS使用,标记一个节点是否被遍历过
int d[maxn]; //从起点到i点的距离
int cur[maxn]; //当前弧下标 void init(int n,int s,int t)
{
this->n=n,this->s=s,this->t=t;
for(int i=;i<=n;i++) G[i].clear();
edges.clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,) );
edges.push_back( Edge(to,from,,) );
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS()
{
memset(vis,false,sizeof(vis));
queue<int> Q;//用来保存节点编号的
Q.push(s);
d[s]=;
vis[s]=true;
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to] = d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if(x==t || a==)return a;
int flow=,f;//flow用来记录从x到t的最小残量
for(int& i=cur[x]; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+==d[e.to] && (f=DFS( e.to,min(a,e.cap-e.flow) ) )> )
{
e.flow +=f;
edges[G[x][i]^].flow -=f;
flow += f;
a -= f;
if(a==) break;
}
}
return flow;
} int Maxflow()
{
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
}Dinic; int main() {
int n, f, a, b, c, d;
scanf("%d %d %d", &n, &f, &d);
int s = n * + f + d + , t = s + ;
Dinic.init(n, s, t);
for(int i = ; i <= f; i ++) {//n * 2 - 1 ~ n * 2 + f - 1存储从s -> 食物的边
Dinic.AddEdge(s, n * + i, );
}
for(int i = ; i <= d; i ++) {//n * 2 + f ~ n * 2 + f + d - 1存储从饮料 -> t的边
Dinic.AddEdge(n * + f + i, t, );
}
for(int i = ; i <= n; i ++) {
Dinic.AddEdge(i ,n + i, );//牛拆点之后的建立的边1 ~ 2 * n
scanf("%d %d", &a, &b);
for(int j = ; j < a; j ++) {
scanf("%d", &c);
Dinic.AddEdge(n * + c, i, );//食物与牛建边
}
for(int j = ; j < b; j ++) {
scanf("%d", &c);
Dinic.AddEdge(i + n, n * + f + c, );//饮料与牛建边
}
}
printf("%d\n", Dinic.Maxflow());//模版最大流?
return ;
}

<每日一题>Day 9:POJ-3281.Dining(拆点 + 多源多汇+ 网络流 )的更多相关文章

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

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

  2. poj 3281 Dining 拆点 最大流

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

  3. POJ 3281 Dining (网络流)

    POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...

  4. POJ 3281 Dining(最大流)

    POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...

  5. poj 3281 Dining 网络流-最大流-建图的题

    题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...

  6. 2018.07.06 POJ 1459 Power Network(多源多汇最大流)

    Power Network Time Limit: 2000MS Memory Limit: 32768K Description A power network consists of nodes ...

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

    题目链接:http://poj.org/problem?id=3281 题目大意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 1 ...

  8. POJ 3281 Dining(最大流)

    http://poj.org/problem?id=3281 题意: 有n头牛,F种食物和D种饮料,每头牛都有自己喜欢的食物和饮料,每种食物和饮料只能给一头牛,每头牛需要1食物和1饮料.问最多能满足几 ...

  9. POJ 3281 Dining(网络流拆点)

    [题目链接] http://poj.org/problem?id=3281 [题目大意] 给出一些食物,一些饮料,每头牛只喜欢一些种类的食物和饮料, 但是每头牛最多只能得到一种饮料和食物,问可以最多满 ...

随机推荐

  1. NTC电阻Rt与温度T关系

    NTC电阻Rt与温度T公式如下: Rt=10000*exp(3950*(1/(273.15+T)-1/(273.15+25))). 例:0摄氏度时,电阻为33620.6037214357 欧姆 Rt= ...

  2. CF261E Maxim and Calculator (质数,完全背包)

    CF261E Maxim and Calculator 题目大意: 有两个初始参数 $ a=1 $ , $ b=0 $ ,你可以对它们进行两个操作: $ b~+=1 $ 或 $ a~\times =b ...

  3. 阅读《Effective Java》每条tips的理解和总结(1)

    <Effective Java>这本书的结构是90来条tips,有长有短,每条tip都值的学习.这里根据对书中每条tip的理解做简短的总结,方便日后回顾.持续更新~ 1. 考虑用静态方法代 ...

  4. 【LuoguP3329&4123】[ZJOI2011]最小割&[CQOI2016]不同的最小割

    链接1 链接2 题意简述 第一个题 : 问图中有多少不同的最小割数值 第二个题 : \(q\) 次询问图中多少对点对之间的最小割小于 \(x\) . Sol 两个都是模板题就放一起了. 求完最小割树直 ...

  5. ubuntu 14.04 安装openjdk 8

    最近准备在ubuntu14.04上安装Oracle,但是需要提前安装jdk,发现问题挺多的,后面看到了如下的操作步骤,成功安装,特意记录下来. 致谢:https://www.yangshenglian ...

  6. 2,Executor线程池

    一,Executor框架简介 在Java 5之后,并发编程引入了一堆新的启动.调度和管理线程的API.Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java.util.co ...

  7. Java——类的继承、访问控制

    [继承] <1>Java只支持单继承,不支持多继承. <2>继承父类的私有成员变量,只有所有权,没有使用权.   [继承中的构造方法]

  8. sh_07_买苹果增强版

    sh_07_买苹果增强版 # 1. 输入苹果的单价 price_str = input("苹果的单价:") # 2. 输入苹果的重量 weight_str = input(&quo ...

  9. 【BZOJ2200】道路和航线(并查集,拓扑排序,最短路)

    题意:n个点,有m1条双向边,m2条单向边,双向边边长非负,单向边可能为负 保证如果有一条从x到y的单项边,则不可能存在从y到x的路径 问从S出发到其他所有点的最短路 n<=25000,n1,m ...

  10. 深入理解Spring(一):初识Spring

    深入理解Spring(一):初识Spring 一. Spring介绍        Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnso ...