POJ 3281 Dining(网络流最大匹配)
分析:
数学模型是三个集合A,B,C,(a,b,c)构成一个匹配。因为图一个点只能匹配一次,把a拆点a',a",
在可以匹配的点上连边,s - b - a' - a" - c - t,然后最大流就好了。
/*********************************************************
* ------------------ *
* author AbyssalFish *
**********************************************************/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
using namespace std; typedef long long ll; const int maxv = , maxe = +;
int hd[maxv],to[maxe],nx[maxe],ec, cap[maxe];
#define eachEage int i = hd[u]; i; i = nx[i]
void add(int u,int v,int cp)
{
nx[++ec] = hd[u];
to[ec] = v;
cap[ec] = cp;
hd[u] = ec;
}
void Add(int u,int v,int cp = )
{
add(u,v,cp);
add(v,u,);
} int S,T; int lv[maxv];
bool vis[maxv];
int que[maxv]; bool bfs()
{
memset(vis,,sizeof(vis));
int *q = que, l = , r = ;
lv[q[r++] = S] = ; vis[S] = true;
while(l<r){
int u = q[l++];
for(eachEage){
int v = to[i];
if(!vis[v] && cap[i]){
lv[v] = lv[u] + ;
vis[v] = true;
q[r++] = v;
}
}
}
return vis[T];
} int cur[maxv]; int aug(int u,int a)
{
if(u == T || !a) return a;
int flow = ,f;
for(int &i = cur[u]; i; i = nx[i]){
int v = to[i];
if(lv[v] == lv[u]+ && (f = aug(v,min(a,cap[i])))){
flow += f; a -= f;
cap[i] -= f; cap[i^] += f;
if(!a) break;
}
}
return flow;
} int maxFlow()
{
int flow = ;
while(bfs()){
memcpy(cur,hd,sizeof(int)*(T+));
flow += aug(S,<<);
}
return flow;
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
ec = ;
int N, F, D; scanf("%d%d%d",&N,&F,&D);
int N2 = N*, N2F = N2+F;
S = ; T = N2F+D+;
for(int i = ; i <= N; i++){
int Fi, Di, x; scanf("%d%d",&Fi,&Di);
while(Fi--){
scanf("%d", &x);
Add(N2+x,i);
}
Add(i,N+i);
while(Di--){
scanf("%d", &x);
Add(N+i,N2F+x);
}
}
for(int i = ; i <= F; i++) Add(S,N2+i);
for(int i = ; i <= D; i++) Add(N2F+i,T);
printf("%d\n", maxFlow());
return ;
}
POJ 3281 Dining(网络流最大匹配)的更多相关文章
- poj 3281 Dining 网络流-最大流-建图的题
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...
- 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(网络流-拆点)
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...
- 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 Dining (最大网络流)
题目链接: http://poj.org/problem?id=3281 题目大意: 有n头牛,f种食物,d种饮料,第i头牛喜欢fi种食物和di种饮料,每种食物或者饮料被一头牛选中后,就不能被其他的牛 ...
随机推荐
- cinder 服务启动与请求流程源码分析
文章以ocata版本进行分析 cinder api 的创建和启动,和 nova api 类似,都是通过在 api-paste.ini 中定义 app ,然后将 app 加载之后,启动 wsgi ...
- Unix Tutorial Eight
1.UNIX 变量 变量是在运行时将信息从shell传递到程序的一种方式.程序在特定的变量中查找“在环境中”,如果发现它们将使用存储的值.有些是由系统设置的,另一些是由你设置的,还有一些是由shell ...
- Go:Nsq消息队列
Nsq服务端简介 在使用Nsq服务之前,还是有必要了解一下Nsq的几个核心组件整个Nsq服务包含三个主要部分 nsqlookupd 先看看官方的原话是怎么说:nsqlookupd是守护进程负责管理拓扑 ...
- 洛谷P3380 【模板】二逼平衡树(树套树)(线段树+树状数组)
P3380 [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数 ...
- MVVM与Controller瘦身实践
MVC是一个做iOS开发都知道的设计模式,也是Apple官方推荐的设计模式.实际上,Cocoa Touch就是按照MVC来设计的. 这里,我们先不讲MVC是什么,我们先来谈谈软件设计的一些原则或者说理 ...
- 验证您的Shell为Bash
内容介绍 在管理Linux服务器时,命令行操作无疑是最为耗时的环节.对大多数用户而言,这意味着将大量时间用于操作Bash shell. 尽管大多数发行版都提供默认的用户类型与root prompts, ...
- 关于text-overflow实现·多余字符省略·的正确打开方式
1. 字符包裹元素要能控制宽度 2. 包裹元素中包含代码 .wrapper { ... text-overflow: ellipsis; overflow: hidden; white-space: ...
- Error: Attribute application@allowBackup value=(false) from AndroidManifest.xml:14:7-34 is also present at [:react-native-qq] AndroidManifest.xml:14:18-44 value=(true).
解决方法: 修改文件: 在manifest标签中添加 xmlns:tools="http://schemas.android.com/tools" 在application标签中添 ...
- JS模块
本文主要内容翻译自<Exploring ES6-upgrade to the next version of javascript> 模块系统 定义模块接口,通过接口 隐藏模块的内部实现, ...
- Nginx停止服务和各种命令
1.停止Nginx服务的四种方法 从容停止服务这种方法较stop相比就比较温和一些了,需要进程完成当前工作后再停止. nginx -s quit 立即停止服务这种方法比较强硬,无论进程是否在工作,都直 ...