Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8577   Accepted: 3991

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

 
最大流
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; const int MAX_N = ;
struct Edge {int from,to,cap,flow; };
int N,F,D;
vector<Edge> edges;
vector<int> G[MAX_N];
int d[MAX_N],cur[MAX_N];
bool vis[MAX_N]; void add_edge(int from,int to,int cap) {
edges.push_back(Edge {from, to, cap, });
edges.push_back(Edge {to, from, , });
int m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
} bool bfs(int s,int t) {
memset(vis,,sizeof(vis));
queue <int> q;
q.push(s);
d[s] = ;
vis[s] = ;
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] = ;
d[e.to] = d[x] + ;
q.push(e.to);
}
}
} return vis[t];
} int dfs(int x,int a,int t) {
if(x == t || a == ) return a;
int flow = , f;
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),t)) > ) {
e.flow += f;
edges[ G[x][i] ^ ].flow -= f;
flow += f;
a -= f;
if(a == ) break;
}
} return flow;
} int Maxflow(int s,int t) {
int flow = ;
while(bfs(s,t)) {
//printf("fucl\n");
memset(cur, , sizeof(cur));
flow += dfs(s, , t);
} return flow;
}
int main()
{
// freopen("sw.in","r",stdin);
scanf("%d%d%d",&N,&F,&D);
for(int i = ; i <= N; ++i) {
add_edge(i,i + N,);
}
for(int i = ; i <= N; ++i) {
int f,d;
scanf("%d%d",&f,&d);
for(int j = ; j <= f; ++j) {
int ch;
scanf("%d",&ch);
add_edge( * N + ch,i,);
}
for(int j = ; j <= d; ++j) {
int ch;
scanf("%d",&ch);
add_edge(i + N,ch + * N + F,);
} } for(int i = ; i <= F; ++i) {
add_edge(,i + * N,);
}
for(int i = ; i <= D; ++i) {
add_edge(i + * N + F, * N + F + D + ,);
} printf("%d\n",Maxflow(, * N + F + D + )); // cout << "Hello world!" << endl;
return ;
}

POJ 3281的更多相关文章

  1. POJ 3281 网络流dinic算法

    B - Dining Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...

  2. poj 3281 最大流+建图

    很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很 ...

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

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

  4. POJ 3281 Dining (网络流)

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

  5. POJ 3281 Dining(最大流)

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

  6. POJ 3281 网络流 拆点保证本身只匹配一对食物和饮料

    如何建图? 最开始的问题就是,怎么表示一只牛有了食物和饮料呢? 后来发现可以先将食物与牛匹配,牛再去和饮料匹配,实际上这就构成了三个层次. 起点到食物层边的容量是1,食物层到奶牛层容量是1,奶牛层到饮 ...

  7. POJ 3281:Dining(最大流)

    http://poj.org/problem?id=3281 题意:有n头牛,f种食物,d种饮料,每头牛有fnum种喜欢的食物,dnum种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种 ...

  8. POJ 3281 (最大流+匹配+拆点)

    题目链接:http://poj.org/problem?id=3281 题目大意:有一些牛,一堆食物,一堆饮料.一头牛要吃一份食物喝一份饮料才算满足,而且牛对某些食物和饮料才有好感,问最多有多少头牛是 ...

  9. poj 3281 最大流建图

    题目链接:http://poj.org/problem?id=3281 #include <cstdio> #include <cmath> #include <algo ...

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

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

随机推荐

  1. 关于activity_main.xml与fragment_main.xml

    第一种解决办法 新版安装SDK文件一开始有两个XML文件,activity_main.xml和fragment_main.xml,不习惯的可以这样处理:1.删除fragment_main.xml整个文 ...

  2. JS中的函数,Array对象,for-in语句,with语句,自定义对象,Prototype

    一)函数 A)JS中的函数的定义格式: function add(a,b) { var sum = a+b; document.write("两个数的和是:" + sum); // ...

  3. 九度oj 1407 快速找出最小数

    原题链接:http://ac.jobdu.com/problem.php?pid=1407 线段树,区间更新,查询区间最小值. 注意区间更新,查询的时候,区间$\begin{align*}[L,R] ...

  4. Objective - C中属性和点语法的使用

    一.属性        属性是Objective—C 2.0定义的语法,为实例变量提供了setter.getter方法的默认实现能在一定程度上简化程序代码,并且增强实例变量的访问安全性         ...

  5. 归并排序 & 计数排序 & 基数排序 & 冒泡排序 & 选择排序 ----> 内部排序性能比较

    2.3 归并排序 接口定义: int merge(void* data, int esize, int lpos, int dpos, int rpos, int (*compare)(const v ...

  6. 华硕电脑安装ubuntu出现问题及决方案

    问 题 一:华硕电脑安装ubuntu时无线网络禁用解决方案:打开终端(Ctrl+alt+t)运行命令sudo rmmod acer-wmi,然后开启无线,连接上后便可以上网(附上ubuntu论坛上讨论 ...

  7. JQuery 对 Select option 的操作---转载

    <select id="selectID" > <option value="1">1</option> <optio ...

  8. LintCode-Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  9. ubuntu cron.hourly不运行

    服务器没有装NTP,要每天向特定的server进行时间同步,写了一个定时任务,放在/etc/cron.daily下,但是不运行. /etc/crontab文件: # /etc/crontab: sys ...

  10. Careercup - Microsoft面试题 - 5943729928011776

    2014-05-10 21:56 题目链接 原题: Suppose you get number of unique users every second from bing For eg, ,,,, ...