Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18230   Accepted: 8132

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.

Source

题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料。每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛。最多能有多少头牛可以同时得到喜欢的食物和饮料?
其实这就是一个最大流 关键是建图
因为题目问的是多少头牛可以得到喜欢的食物和饮料,当时建图我就按照食物-->牛-->饮料这样建图
但存在一种牛可以满足同时得到多种自己喜欢的食物和饮料,所以这样是错误的  后来看了题解  才发现要将牛拆成两个点
食物-->牛---->牛----->饮料   流量都为1  这样就可以保证求到的一定是最大流
写了个前向星+Dinc的板子
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int INF=1e9;
const int N=+;
int vis[][];
int dis[N];
int head[N];
int a[N],b[N];
int n,f,d;
int tot;
struct node{
int to,next,flow,c;
}edge[N<<];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v,int c){
edge[tot].to=v;
edge[tot].flow=c;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].to=u;
edge[tot].flow=;
edge[tot].next=head[v];
head[v]=tot++; }
int BFS(int s,int t){
queue<int>q;
memset(dis,-,sizeof(dis));
dis[s]=;
q.push(s);
while(!q.empty()){
int x=q.front();
q.pop();
if(x==t)return ;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(edge[i].flow&&dis[v]==-){
dis[v]=dis[x]+;
q.push(v);
}
}
}
if(dis[t]==-)return ;
return ;
}
int DFS(int s,int flow){
if(s==*n+d+f+)return flow;
int ans=;
for(int i=head[s];i!=-;i=edge[i].next){
int v=edge[i].to;
if(edge[i].flow&&dis[v]==dis[s]+){
int f=DFS(v,min(flow-ans,edge[i].flow));
edge[i].flow-=f;
edge[i^].flow+=f;
ans+=f;
if(ans==flow)return ans;
}
}
return ans;
}
int Dinc(int s,int t){
int flow=;
while(BFS(s,t)){
//cout<<1<<endl;
flow+=DFS(s,INF);
//cout<<flow<<endl;
}
return flow;
}
int main(){
while(scanf("%d%d%d",&n,&f,&d)!=EOF){
init();
int s=;
for(int i=;i<=f;i++){
add(s,*n+i,);
}
for(int i=;i<=d;i++){
add(*n+f+i,*n+f+d+,);
}
for(int i=;i<=n;i++){
add(i,i+n,);
}
int D,F;
for(int i=;i<=n;i++){
scanf("%d%d",&D,&F);
for(int j=;j<=D;j++){
scanf("%d",&a[j]);
add(*n+a[j],i,);
}
for(int j=;j<=F;j++){
scanf("%d",&b[j]);
add(n+i,*n+f+b[j],);
}
}
cout<<Dinc(s,*n+f+d+)<<endl;
} }

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. POJ 3281 Dining(网络流拆点)

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

  7. poj 3281 Dining(网络流+拆点)

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20052   Accepted: 8915 Descripti ...

  8. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

  9. poj 3281 Dining【拆点网络流】

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11828   Accepted: 5437 Descripti ...

随机推荐

  1. 6、scala Map和Tuple

    1.  创建Map 2.访问Map元素 3.修改Map元素的值 4.遍历Map 5.SortedMap和LinkedHashMap 6.Map的元素类型Tuple 1.  创建Map 创建不可变的Ma ...

  2. docloud后台管理项目(前端篇)

    以下内容与主题无关,如果不想看可以直接忽视 !--忽视开始--! 给大家推荐一款强大的编辑器,那就是集响应快.体验好.逼格高.功能丰富为一体的sublime text 3.它除了以上特点,还有一个最重 ...

  3. chinason工作室-兄弟的工作室开张了,欢迎来访喔!

    Chinason工作室,团队成员由多位多年从事软件开发及大型生产企业系统维护的工程师组成,借重传统国外协同软件的开发经验,结合国内企业实际需求,致力于本土企业工作流软件研发,workflow系统定制开 ...

  4. (转)Struts2访问Servlet的API及......

    http://blog.csdn.net/yerenyuan_pku/article/details/67315598 Struts2访问Servlet的API 前面已经对Struts2的流程已经执行 ...

  5. The Standard SSL Handshake

    The following is a standard SSL handshake when RSA key exchange algorithm is used: 1.  Client Hello ...

  6. [API 开发管理] EOLINKER 升级为多产品架构, AMS V4.5 版本常见问题汇总

    自AMS4.5开始,eoLinker 全面升级为多产品架构,部分操作方式较以前有较大改变,本文针对改进部分做重点说明. 在说明之前,我们先通过以下的图文看看AMSV4.5更新了哪些内容: Q:我可以创 ...

  7. Django - 创建多对多及增加示例

    创建多对多: 方式一: 自定义关系表 备注:自定义表Host.Application,通过自定义表,将表Host和Application进行关联(通过外键方式工): 执行语句:python manag ...

  8. 洛谷——P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  9. UID中RUID、EUID和SUID的区别

    看UNIX相关的书时经常能遇到这几个概念,但一直没有好好去理清这几个概念,以致对这几个概念一直一知半解.今天好好区分了一下这几个概念并总结如下.说白了这几个UID引出都是为了系统的权限管理. 下面分别 ...

  10. TensorFlow 学习笔记(2)----placeholder的使用

    此系列将会每日持续更新,欢迎关注 在TensorFlow中输入值的方式是通过placeholder来实现 例如:做两个数的乘法时,是先准备好两个place, 再将输出值定义成两数的乘法 最后利用ses ...