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个牛.每一个牛有一些喜欢的 ...
随机推荐
- 深入理解计算机系统 第三章 程序的机器级表示 part1
如题所示,这一章讲解了程序在机器中是怎样表示的,主要讲汇编语言与机器语言. 学习什么,为什么学,以及学了之后有什么用 我们不用学习如何创建机器级的代码,但是我们要能够阅读和理解机器级的代码. 虽然现代 ...
- poj 3281 Dining (Dinic)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22572 Accepted: 10015 Descript ...
- 力扣(LeetCode)找不同 个人题解
给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...
- ZeroC Ice发送大数据
继上文,我们使用ZeroC Ice传递大块数据时,通常有两种做法,一种是一次请求,另一种就是分多次请求(,这种做法在官方文档有例子).选哪一种根据需要而定. 当分多次请求来完成一大块数据,到底选择每次 ...
- 一 linuk系统简介
开源软件 使用的自由,绝大多数开源软件免费 研究的自由,可以获得软件源代码 散播及改良的自由,可以自由传播 改良甚至销售 linuk应用领域 基于linuk的企业服务器 扫描踩点网站www.netcr ...
- bootstrap中图片响应式
主要解决的是在轮播图中图片响应式的问题 目的 各种终端都需要正常显示图片 移动端应该使用更小(体积)的图片 实现方式 给标签添加两个data-属性(如:data-img-sm="小图路径&q ...
- JSAPI 基于arcgis_js_api3.3的部署
JSAPI,即ArcGIS API For JavaScript,是arcgis基于JavaScript环境下的开发包.包含Dojo框架. ||Dojo结构如下:=================== ...
- 性能测试:深入理解线程数,并发量,TPS,看这一篇就够了
并发数,线程数,吞吐量,每秒事务数(TPS)都是性能测试领域非常关键的数据和指标. 那么他们之间究竟是怎样的一个对应关系和内在联系? 测试时,我们经常容易将线程数等同于表述为并发数,这一表述正确吗? ...
- day01_爬虫和数据
1.什么是爬虫 1.1.爬虫的定义 脚本,程序--->自动抓取万维网上信息的程序. 1.2.爬虫的分类 2.1.通用爬虫 通用网络爬虫 是 捜索引擎抓取系统(Baidu.Google ...
- 01-tornado练习-tornado简介
# coding = utf-8 """ 启动一个tornado的web服务 """ import tornado.web from tor ...