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 ...
随机推荐
- ios view的frame和bounds之区别(位置和大小)
前言: 学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,发现bound怎么也想不明白,好像饶你了死胡同里,经过一番尝试和思考,终于弄明白bo ...
- 算法系列7《CVN》
计算CVN时使用二个64位的验证密钥,KeyA和KeyB. 1) 计算CVN 的数据源包括: 主账号(PAN).卡失效期和服务代码,从左至右顺序编排. 4123456789012345+8701+11 ...
- bat文件编写(无线承载网络设置)
就弄个例子,自己看执行效果,然后模仿写就行. 1)获取当前时间: @echo off set YEAR=%date:~0,4% set MONTH=%date:~5,2% set DAY=%date: ...
- ios中如何计算(页数,行数,等等的算法)
页数 = (总个数 + 每页最大显示个数 - 1) / 每页显示最大的个数
- java intellij 写控制台程序 窗口程序
建一个空项目,建一个main函数 用application,就可以运行了 /** * Created by robin on 15/10/11. */public class hello { pu ...
- 从烙铁手到IT男
时间:2015年8月27日 21:37:44 作者:luomg 摘要: 简要记录一写自己干的这个行当,多少是个回忆,不然某一天呜呼哀哉了啥也没有记录,会随着时间更新(大学时熬夜绘制了很多altium ...
- FPGA仿真设置步骤
1.FPGA仿真流程图 2.FPGA时序图 3.FPGA开发方式
- 如何在Quartus II中设置Virtual pin
为了验证FPGA工程中的某个模块的功能和时序的正确性,常常需要对其单独进行验证,但是这些模块通常都与内部的众多信号相连(如系统总线,中断信号线等),往往一个模块的对外接口引脚会多达几百个,对其单独仿真 ...
- java导出excel表格
java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...
- Socket 一对多通信
服务器(TCPServer.java): package visec; import java.net.*; import java.io.*; public class TCPServer{ pub ...