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 ...
随机推荐
- javascript 关于Date 时间类型 处理方法
上一篇博客中和大家分享了关于 字符串转时间类型 这一篇顺便整理下 javascript 中 Date 类型的一些方法 var time = new Date(); var year=time.getY ...
- oracle DML错误日志(笔记)
DML错误日志是oracle10gR2引入的一个类似于SQL*Loader的错误日志功能.它的基本原理是把任何可能导致语句失败的记录转移,放到一张错误日志表中. 具体使用如下: 1.使用DBMS_ER ...
- listview 优化
ListView的优化: (前两点都是利用ListView的自身优化机制优化[缓存优化]) 1.利用ListView自身的缓存机制,他会缓存条目中的一个条目item,当listview第一屏显示完成之 ...
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- 一个.net程序员教你使用less
我是一个.net 程序员,虽然说一直做后台,但是web 前端也会去学,虽然说技术只是层窗户纸,但是像我这种多动症患者,不捅破我心难受啊! 好!废话不多提,下面直接正题,至于less 是什么这里不多讲因 ...
- 条款20:以const-reference传递替换by-value传递
缺省情况下,C++中函数参数的传递方式为by-value.即函数都是以实际参数的副本进行传递,而函数返回的也是一个副本.考虑如下实例程序: #include <iostream> clas ...
- 14.quartus联合modelsim仿真
在quartus调用modelsim仿真过程中,出现了一个错误,如下所示: Check the NativeLink log file I:/Quartus11.0/Myproject/testi_n ...
- linux中的文件类型
1.使用ls -l命令可以查看文件的类型和权限 [tansheng@localhost etc]$ ls -l ----------. root root 10月 : gshadow -------- ...
- 典型用户 persona
persona 典型用户 1.姓名:王涛 2.年龄:22 3.收入:基本无收入 4.代表用户在市场上的比例和重要性:王涛为铁道学生.本软件的用户主要是学生和老师,尤其是广大的铁大学子,所以此典型用户的 ...
- 三、freemarker数据、模版指令
数据类型 1. 直接指定值(字符串.数值.布尔值.集合.Map对象) 2. 字符串:直接指定字符串使用单引号.双引号,字符中间可以使用转义符“\”,如果字符内有大量特殊 ...