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 ...
随机推荐
- hdu 4631Sad Love Story<计算几何>
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 题意:依次给你n个点,每次求出当前点中的最近点对,输出所有最近点对的和: 思路:按照x排序,然后用s ...
- android string.xml %问题
反复检查后发现是string.xml中的 % 导致编译失败, 这是由于新的SDK采用了新版本的aapt(Android项目编译器),这个版本的aapt编译起来会比老版本更加的严格,然后在Android ...
- 记录一下mvc发布
让别人也可以访问你电脑上的ASP.NET MVC创建的网站 http://www.cnblogs.com/laoqi/p/4169184.html
- iOS开发之Pch预编译文件的创建
在Xcode6之前,创建一个新工程xcode会在Supporting files文件夹下面自动创建一个“工程名-Prefix.pch”文件,也是一个头文件,pch头文件的内容能被项目中的其他所有源文件 ...
- Android实现KSOAP2访问WebService
Android实现KSOAP2访问WebService 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 写一个工具类来给主界面使用,作用是使用 ...
- Xcode 添加代码块
我们经常会定义一些retain的property,而且大概每次我们都会像这样写: @property (nonatomic, retain) Type *name; 每次都要老老实实的把“@prop ...
- JSON WEB TOKENS
用JWT来保护我们的ASP.NET Core Web API 在上一篇博客中,自己动手写了一个Middleware来处理API的授权验证,现在就采用另外一种方式来处理这个授权验证的问题,毕竟现在也 ...
- 初学MFC
学习使用MFC搭建界面.尝试使用MFC搭建了一个简单的基于对话框的计算器界面,包括模态对话框.非模态对话框.向导对话框等. // MFCApplicationAddDlg.h : 头文件 // #pr ...
- Careercup - Microsoft面试题 - 5428361417457664
2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...
- NSIS中判断.net framework 是否安装
在网上找了几个脚本,原理都是读取注册表进行判断,都可以用. 1.CheckDotNetFramework /* * Name: CheckDotNetFramework.nsh * Version: ...