POJ 3281 Dining(网络流-拆点)
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 Fiintegers 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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define PI acos(-1)
#define EPS 1e-8
const int INF=0x3f3f3f3f;
const int maxn = ;
int n,m,k,s,t,x,y,z;
struct Edge {
int from, to, cap, flow;
};
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn], cur[maxn]; void Init()
{
memset(d,,sizeof d);
for(int i=;i<=n;i++) G[i].clear();
} void addedge(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()
{
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)
{
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[e.to] == d[x] + && (f=dfs(e.to, min(a, e.cap-e.flow))) > )
{
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())
{
memset(cur,,sizeof cur);
flow += dfs(s, INF);
}
return flow;
} int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
Init();
s=,t=*n+m+k+;
for(int i=;i<=m;i++) addedge(s,i,);
for(int i=;i<=n;i++) addedge(m+i,n+m+i,);
for(int i=*n+m+;i<=*n+m+k;i++) addedge(i,t,);
for(int i=;i<=n;i++)
{
scanf("%d%d",&x,&y);
for(int j=;j<=x;j++) scanf("%d",&z),addedge(z,m+i,);
for(int j=;j<=y;j++) scanf("%d",&z),addedge(m+n+i,z+m+*n,);
}
printf("%d\n",Maxflow(s,t));
}
return ;
}
POJ 3281 Dining(网络流-拆点)的更多相关文章
- poj 3281 Dining 网络流-最大流-建图的题
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...
- POJ - 3281 Dining(拆点+最大网络流)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18230 Accepted: 8132 Descripti ...
- poj 3281 Dining【拆点网络流】
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11828 Accepted: 5437 Descripti ...
- POJ 3281 Dining 网络流最大流
B - DiningTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...
- POJ 3281 Dining (网络流之最大流)
题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给 ...
- POJ 3281 Dining[网络流]
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...
- POJ 3281 Dining (网络流构图)
[题意]有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和 ...
- 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个牛.每一个牛有一些喜欢的 ...
随机推荐
- Python面向对象 | 静态方法 staticmethod
静态方法是类中的函数,不需要实例.静态方法主要是用来存放逻辑性的代码,逻辑上属于类,但是和类本身没有关系,也就是说在静态方法中,不会涉及到类中的属性和方法的操作.可以理解为,静态方法是个独立的.单纯的 ...
- Python项目开发公用方法--excel生成方法
在实际开发中,我们有时会遇到数据导出的需求.一般的,导出的文件格式为Excel形式. 那么,excel的生成就适合抽离出一个独立的公用方法来实现: def generate_excel(excel_n ...
- HTML中的表格标签
表格是网页制作中使用最多的工具之一,在制作网页时,使用表格可以更清晰地排列数据.但是在实际制作过程中,表格更多用在网页布局的定位上.很多网页都是以表格布局的.这是因为表格在文本和图像的位置控制方面 ...
- VMware虚拟机Linux中增加磁盘空间的扩容操作
VMwareware虚拟机安装的Red Hat Enterprise Linux系统剩余空间不足,造成软件无法正常安装.如果重新装一遍系统就需要重新配置好开发环境和软件的安装配置.结合自己的实践,总结 ...
- 【SSM Spring 线程池 OJ】 使用Spring线程池ThreadPoolTaskExecutor
最近做的Online Judge项目,在本地判题的实现过程中,遇到了一些问题,包括多线程,http通信等等.现在完整记录如下: OJ有一个业务是: 用户在前端敲好代码,按下提交按钮发送一个判题请求给后 ...
- [学习笔记] 在Eclipse中使用Hibernate,并创建第一个工程
在Eclipse中使用Hibernate 安装 Hibernate Tools 插件 https://tools.jboss.org/downloads/ Add the following URL ...
- 【dp】you are the one
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4283 题解: 当最优解下, a1在j的位置排出, 则a2 ——aj-1 和 aj——an为两个独立事件 ...
- 关于IP网段划分
IP地址分类(A类 B类 C类 D类 E类) IP地址由四段组成,每个字段是一个字节,8位,最大值是255,, IP地址由两部分组成,即网络地址和主机地址.网络地址表示其属于互联网的哪一个网络 ...
- ubuntu 16.04上源码编译opengv | compile opengv on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/1e5d14ee/,欢迎阅读! compile opengv on ubuntu 16.04 Series compile open ...
- vue 原生添加滚动加载更多
vue中添加滚动加载更多,因为是单页面所以需要在跳出页面时候销毁滚动,要不会出现错乱.我们在mounted建立滚动,destroyed销毁滚动. mounted () { window.addEven ...