Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11844   Accepted: 5444

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: 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种喝的,吃的和喝的只能被用一次,求可以满足的母牛个数;
这个题可以加一个源点,一个终点,将吃的左边与源点连,右边与母牛连,将母牛拆点,母牛右边与喝的连,喝的与终点连。。。
代码:
 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=<<;
int head[MAXM];
int vis[MAXN],dis[MAXN];
int edgnum;
struct Node{
int from,to,next,cup,flow;
};
Node edg[MAXM];
queue<int>dl;
void add(int u,int v,int w){
Node E={u,v,head[u],w,};
edg[edgnum]=E;
head[u]=edgnum++;
E={v,u,head[v],,};
edg[edgnum]=E;
head[v]=edgnum++;
}
void initial(){
mem(head,-);edgnum=;
}
bool bfs(int s,int e){
mem(vis,);mem(dis,-);
while(!dl.empty())dl.pop();
dis[s]=;vis[s]=;dl.push(s);
while(!dl.empty()){
int u=dl.front();
dl.pop();
for(int i=head[u];i!=-;i=edg[i].next){
Node v=edg[i];
if(!vis[v.to]&&v.cup>v.flow){
vis[v.to]=;dl.push(v.to);
dis[v.to]=dis[u]+;
if(v.to==e)return true;
}
}
}
return false;
}
int dfs(int x,int la,int e){
if(x==e||la==)return la;
int temp;
int flow=;
for(int i=head[x];i!=-;i=edg[i].next){
Node &v=edg[i];
if(dis[v.to]==dis[x]+&&(temp=dfs(v.to,min(la,v.cup-v.flow),e))>){
v.flow+=temp;
edg[i^].flow-=temp;
la-=temp;
flow+=temp;
if(la==)break;
}
}
return flow;
}
int maxflow(int s,int e){
int flow=;
while(bfs(s,e)){
flow+=dfs(s,INF,e);
}
return flow;
}
int main(){
int n,F,D,f,d,a,b;
while(~scanf("%d%d%d",&n,&F,&D)){
initial();
for(int i=;i<=n;i++){
scanf("%d%d",&f,&d);
while(f--){
scanf("%d",&a);
add(*n+a,i,);
}
while(d--){
scanf("%d",&a);
add(n+i,*n+F+a,);
}
add(i,n+i,);
}
for(int i=;i<=F;i++)add(,*n+i,);
for(int i=;i<=D;i++)add(*n+F+i,*n+F+D+,);
printf("%d\n",maxflow(,*n+F+D+));//醉了,应该从0开始,找了半天错。。。
}
return ;
}

另一种解法:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=<<;
queue<int>dl;
int vis[MAXN],map[MAXN][MAXN],pre[MAXN];
bool bfs(int s,int e){
mem(vis,);mem(pre,-);
while(!dl.empty())dl.pop(); //忘了初始化。。。。。
vis[s]=;dl.push(s);
int a;
while(!dl.empty()){
a=dl.front();
dl.pop();//忘写pop了。。。
if(a==e)return true;
for(int i=;i<=e;i++){
if(!vis[i]&&map[a][i]){
dl.push(i);
vis[i]=;
pre[i]=a;
//if(i==e)return true;
}
}
}
return false;
}
int maxflow(int s,int e){
int flow=;
while(bfs(s,e)){
int temp=INF;
int r=e;
//puts("fasf");
while(r!=s)temp=min(temp,map[pre[r]][r]),r=pre[r];
r=e;
while(r!=s)map[pre[r]][r]-=temp,map[r][pre[r]]+=temp,r=pre[r];
flow+=temp;
}
return flow;
}
int main(){
int n,F,D,f,d,a,b;
while(~scanf("%d%d%d",&n,&F,&D)){
mem(map,);
for(int i=;i<=n;i++){
scanf("%d%d",&f,&d);
while(f--){
scanf("%d",&a);
// add(2*n+a,i,1);
map[*n+a][i]=;
}
while(d--){
scanf("%d",&a);
// add(n+i,2*n+F+a,1);
map[n+i][*n+F+a]=;
}
//add(i,n+i,1);
map[i][n+i]=;
}
for(int i=;i<=F;i++)map[][*n+i]=;//add(0,2*n+i,1);
for(int i=;i<=D;i++)map[*n+F+i][*n+F+D+]=;//add(2*n+F+i,2*n+F+D+1,1);
printf("%d\n",maxflow(,*n+F+D+));//醉了,应该从0开始,找了半天错。。。
}
return ;
}

Dining(最大流)的更多相关文章

  1. POJ3281 Dining —— 最大流 + 拆点

    题目链接:https://vjudge.net/problem/POJ-3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Subm ...

  2. POJ 3281 Dining(最大流)

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

  3. POJ3281 Dining 最大流

    题意:有f种菜,d种饮品,每个牛有喜欢的一些菜和饮品,每种菜只能被选一次,饮品一样,问最多能使多少头牛享受自己喜欢的饮品和菜 分析:建边的时候,把牛拆成两个点,出和入 1,源点向每种菜流量为1 2,每 ...

  4. 【网络流#7】POJ 3281 Dining 最大流 - 《挑战程序设计竞赛》例题

    不使用二分图匹配,使用最大流即可,设源点S与汇点T,S->食物->牛->牛->饮料->T,每条边流量为1,因为流过牛的最大流量是1,所以将牛拆成两个点. 前向星,Dini ...

  5. P2891 [USACO07OPEN]吃饭Dining 最大流

    \(\color{#0066ff}{ 题目描述 }\) 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类 ...

  6. [poj3281]Dining(最大流+拆点)

    题目大意:有$n$头牛,$f$种食物和$d$种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢 ...

  7. poj3281 Dining 最大流(奇妙的构图)

    我是按照图论500题的文档来刷题的,看了这题怎么也不觉得这是最大流的题目.这应该是题目做得太少的缘故. 什么是最大流问题?最大流有什么特点? 最大流的特点我觉得有一下几点: 1.只有一个起点.一个终点 ...

  8. POJ 3281 Dining(最大流板子)

    牛是很挑食的.每头牛都偏爱特定的食物和饮料,其他的就不吃了. 农夫约翰为他的牛做了美味的饭菜,但他忘了根据它们的喜好检查菜单.虽然他不可能喂饱所有的人,但他想让尽可能多的奶牛吃上一顿有食物和水的大餐. ...

  9. POJ 3281 Dining 最大流

    饮料->牛->食物. 牛拆成两点. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include ...

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

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

随机推荐

  1. Jquery 触发器之treigger()方法简介

    trigger是个很神奇的东西,它可以模拟简单的用户输入操作.并触发点击click, mouseover, keydown 等事件. 具体使用方法如下: $("button").c ...

  2. JavaSE复习日记 : 八种基本数据类型

    /* * 基本数据类型 * * Java里的8种基本数据类型: * byte --- 1 byte = 8 bit; * short --- 2 byte = 16 bit; * int --- 4 ...

  3. Hadoop学习笔记(2)hadoop框架解析

    Hadoop是适合大数据的分布式存储与计算平台 HDFS的架构:主从式结构 主节点只有一个NameNode,从节点可以有很多个DataNode. NameNode负责: (1)接收用户操作请求 (2) ...

  4. PHP调试工具 《Kint》

    Kint使用,简单介绍 是一个简单又强大的PHP调试工具. 1.kint 是什么? kint是用绝对易人识辨的方式展示PHP调试的数据. 换句话说,它可以取var_dump(),debug_blick ...

  5. 在iOS上增加手势锁屏、解锁功能

    在iOS上增加手势锁屏.解锁功能 在一些涉及个人隐私的场景下,尤其是当移动设备包含太多私密信息时,为用户的安全考虑是有必要的. 桌面版的QQ在很多年前就考虑到用户离开电脑后隐私泄露的危险,提供了“离开 ...

  6. perl5 第三章 操作符

    第三章 操作符 by flamephoenix 一.算术操作符二.整数比较操作符三.字符串比较操作符四.逻辑操作符五.位操作符六.赋值操作符七.自增自减操作符八.字符串联结和重复操作符九.逗号操作符十 ...

  7. python+opencv

    $cd numpy $ sudo python setup.py build $ sudo python setup.py installRunning from numpy source direc ...

  8. 禁止apache显示目录索引

    1)修改目录配置: 复制代码 代码如下: <Directory "D:/Apache/blog.phpha.com">Options Indexes FollowSym ...

  9. rman备份优化思路

    本章不讲rman备份原理.仅仅提供一些思路 1.oracle11g 选择压缩算法为中级: 2.添加rman备份的通道. 以上两种做法.添加CPU的利用率,降低IO 3.指定rate參数 这个rate和 ...

  10. jenkins+maven +svn+tomcat7集群部署(一)

    在网上看了好多有关集群部署的文章,感觉都不是太连贯,非常多仅仅是给你说怎么安装而已,可是过程中遇到的问题真不少,可是也攻克了非常多问题,希望我的文章可以帮到那些想学习的人吧,jenkins主要是攻克了 ...