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

确保每个f和d都只能经过一次,将牛拆点,分别建f到牛,牛到d的容量为1的路,设置一个总起点0和总汇点2*n+d+f+1,跑一遍网络流

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define pn printf("\n")
using namespace std;
typedef long long ll; int n,f,d;
const int MAXN = ;
const int MAXM = ;
struct Edge
{
int to,next,cap,flow;
}edge[MAXM];
set <pair<int,int> > st;
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN]; void init()
{
tol = ;
memset(head,-,sizeof(head));
}
//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=) {
edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
edge[tol].flow = ;head[u] = tol++;
edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];
edge[tol].flow = ;head[v]=tol++;
} int sap(int start,int end,int N) {
memset(gap,,sizeof(gap));
memset(dep,,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -;
gap[] = N;
int ans = ;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
for(int i = pre[u];i != -; i = pre[edge[i^].to])
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for(int i = pre[u];i != -; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -;i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v]+ == dep[u])
{
flag = true;
cur[u] = pre[v] = i; break;
}
}
if(flag)
{
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -;i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(!gap[dep[u]])return ans;
dep[u] = Min+;
gap[dep[u]]++;
if(u != start) u = edge[pre[u]^].to;
}
return ans;
}
int main()
{
init();
int fi, di, x;
cin >> n >> f >> d;
for(int i=;i<=f;i++) addedge(, *n+i, );
for(int i=;i<=d;i++) addedge(*n+f+i, *n+f+d+, );
for(int i=;i<=n;i++) addedge(i, n+i, );
for(int i=;i<=n;i++)
{
scanf("%d%d",&fi,&di);
for(int j=;j<fi;j++)
{
scanf("%d",&x);
addedge(*n+x, i, );
}
for(int j=;j<di;j++)
{
scanf("%d",&x);
addedge(i+n,*n+f+x,);
}
}
cout << sap(, *n+f+d+, *n+f+d+) << endl;
}

POJ 3281 Dining[网络流]的更多相关文章

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

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

  2. POJ 3281 Dining 网络流最大流

    B - DiningTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...

  3. POJ 3281 Dining (网络流之最大流)

    题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给 ...

  4. POJ 3281 Dining (网络流构图)

    [题意]有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和 ...

  5. POJ 3281 Dining(网络流-拆点)

    Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...

  6. POJ 3281 Dining (网络流)

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

  7. POJ 3281 Dining(最大流)

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

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

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

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

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

随机推荐

  1. IE-FSC

    Top3: Top2: FSC related to Redis: (Redis = https://www.cnblogs.com/ngtest/p/10693750.html) FSC statu ...

  2. 一个站点的诞生02--用Scrapy抓取数据

    假设想抓数据,就须要有爬虫程序,业内叫crawler或者spider. 有各种语言版本号的开源爬虫.c++, Java,  php,在github上搜一下,以"spider c++" ...

  3. Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set

    C. Reberland Linguistics     First-rate specialists graduate from Berland State Institute of Peace a ...

  4. luogu1445 [violet]樱花 阶乘分解

    题目大意 求方程$$\frac{1}{x}+\frac{1}{y}=\frac{1}{N!}$$的正整数解的组数. 思路 咱们把式子整理得$$xy-(x+y)N!=0$$.$xy$和$x+y$?貌似可 ...

  5. 火狐Vimperator插件

    http://www.iplaysoft.com/vimium-and-vimperator.html http://wangbixi.com/x2923/comment-page-1/

  6. Android开机logo修改方法 【转】

    本文转载自:http://blog.csdn.net/qq258711519/article/details/7766303 一体机平台开机logo修改方法 1:修改Kernel中的Logo: 若是要 ...

  7. 用虚拟机创建win7 32位系统来测试win 7 64位系统无法安装cad 2004 缺少acdb16.dll的问题

  8. 【转】In ASP.NET using jQuery Uploadify upload attachment

    Upload Uploadify is a JQuery plug-in, achieve the effect is very good, with progress display. Upload ...

  9. CharSequence源码分析

    CharSequence是一个接口,表示一个char值的可读序列,此接口为多种char序列提供统一的.只读的通道.既然是接口,就不能通过new来进行赋值,只能通过以下方式赋值: CharSequenc ...

  10. 【BZOJ3218】【UOJ#77】a + b Problem

    题目 题目在这里 思路&做法 明显的最小割(其实是之前做过一道类似的题) S向每一个格子连容量为\(b_i\)的边 每一个格子向T连容量为\(w_i\)的边 对于格子\(i\)向满足条件的格子 ...