POJ 3281
| 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
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
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
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的更多相关文章
- POJ 3281 网络流dinic算法
B - Dining Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
- poj 3281 最大流+建图
很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很 ...
- poj 3281 Dining 网络流-最大流-建图的题
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...
- POJ 3281 Dining (网络流)
POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...
- POJ 3281 Dining(最大流)
POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...
- POJ 3281 网络流 拆点保证本身只匹配一对食物和饮料
如何建图? 最开始的问题就是,怎么表示一只牛有了食物和饮料呢? 后来发现可以先将食物与牛匹配,牛再去和饮料匹配,实际上这就构成了三个层次. 起点到食物层边的容量是1,食物层到奶牛层容量是1,奶牛层到饮 ...
- POJ 3281:Dining(最大流)
http://poj.org/problem?id=3281 题意:有n头牛,f种食物,d种饮料,每头牛有fnum种喜欢的食物,dnum种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种 ...
- POJ 3281 (最大流+匹配+拆点)
题目链接:http://poj.org/problem?id=3281 题目大意:有一些牛,一堆食物,一堆饮料.一头牛要吃一份食物喝一份饮料才算满足,而且牛对某些食物和饮料才有好感,问最多有多少头牛是 ...
- poj 3281 最大流建图
题目链接:http://poj.org/problem?id=3281 #include <cstdio> #include <cmath> #include <algo ...
- POJ 3281 Dining(最大流+拆点)
题目链接:http://poj.org/problem?id=3281 题目大意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 1 ...
随机推荐
- apache 403错
<Directory />Options FollowSymLinksAllowOverride NoneOrder deny,allowAllow from all</Direct ...
- C扩展 C++回顾到入门
引言 C扩展也称C++, 是一个复(za)杂(ji)优(ken)秀(die)的语言. 本文通过开发中常用C++方式来了解和回顾C++这么语言. C++看了较多的书但还是觉得什么都不会. 只能说自己还付 ...
- bootstrap-table-master
http://bootstrap-table.wenzhixin.net.cn/getting-started/ 1.安装bower 2. 3.编译css and js 以上就编译完了boostrap ...
- Nginx服务器架构简析
一.Nginx的模块化 模块化结构的思想是一个很久的概念,但也正是成熟的思想造就了Nginx的巨大优越性. 我们知道Nginx从总体上来讲是有许多个模块构成的.习惯将Nginx分为5大模块分别为:核心 ...
- 一步一步实现Linux设备驱动的Helloworld模块
学了那么多程序语言,总是有一个Hello world开头,不禁感叹Hello world的强大.呵呵,废话少说,咋们的故事当然要从这个Hello world开始. 先查看自己OS使用的内核版本[don ...
- AppCan移动技术全景图:创新、协作、支撑
开发者是移动互联网宏伟蓝图的最终实现者.如果你有创意.有技术,你可以开发一款服务上亿人的应用.所以,我感觉幸运,没有任何一个时代,能像现在这么好,技术人能够服务这么广大的市场,能够撬动百亿级的市场. ...
- hdu 3152 Obstacle Course
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3152 Obstacle Course Description You are working on t ...
- Android 解决图片大量下载:软引用必须懂4点
1.对象的强.软.弱和虚引用为了能更加灵活控制对象的生命周期,需要知道对象引用的4中级别,由高到低依次为 :强引用.软引用.弱引用和虚引用 备注: 这四种的区别: ⑴强引用(StrongReferen ...
- MySQL 触发器简单实例
~~语法~~ CREATE TRIGGER <触发器名称> --触发器必须有名字,最多64个字符,可能后面会附有分隔符.它和MySQL中其他对象的命名方式基本相象.{ BEFORE | ...
- [小技巧]让你的GridView支持IQueryable,并自动实现真分页
众所周知,asp.net自带的GridView在自带分页方面设计得很2,因为它是假分页,即内存分页.而且它不智能支持强大的Iqueryable. 但这表明微软忽略了现实中的分页需求吗?答案应该不是,我 ...