【最大流模板题】POJ 3281 Dining
来源:互联网 发布:君の知らない物语 编辑:程序博客网 时间:2024/05/21 17:45
题目链接:http://poj.org/problem?id=3281
题目大意:给定N头牛、F个食物、D个饮料。每头牛有一定的喜好,只喜欢其中几个食物和饮料。每个食物和饮料只能给一头牛即一头牛只能得到一个食物和饮料。而且一头牛必须同时获得一个食物和一个饮料才能满足。问至多有多少头牛可以获得满足。
分析:刚开始想用二分匹配来写,但是有两个限制条件,同时获得食物和饮料才能满足。这样二分匹配显然不行。
建立最大流模型求解:添加一个源点和一个汇点,按照源点->食物->牛->牛->饮料->汇点建立出模型,求解最大流即可。最大流建图是把食物和饮料放在两端。一头牛拆分成两个点,两点之间的容量为1(这样才能保证一头牛能获得一个食物和一个饮料)。把喜欢的食物和饮料跟牛建条边,容量为1。加个源点和汇点。源点与食物、饮料
和汇点的边容量都是1,表示每种食物和饮料只有一个。
建图的时候要弄清楚点之间的关系,虽然看似简单,但感觉对刚接触的人来说还是不好想。
CODE:(Dinic算法)
#include<stdio.h>#include<algorithm>#include<string.h>#include<iostream>#include<vector>#include<queue>#define INF 0x3f3f3f3ftypedef long long LL;using namespace std;const int maxn=1008;struct edge{ int to,cap,rev;};vector<edge>G[maxn];//图的邻接表表示int iter[maxn];//顶点到源点的距离标号int level[maxn];//当前弧,在其之前的弧已经没有用了//向图中增加一条从from到to的容量为cap的边void addedge(int from,int to,int cap){ G[from].push_back((edge){to,cap,G[to].size()}); G[to].push_back((edge){from,0,G[from].size()-1});}//通过bfs计算从源点出发的距离标号void bfs(int s){ memset(level,-1,sizeof(level)); queue<int>que; level[s]=0; que.push(s); while(!que.empty()) { int v=que.front(); que.pop(); for(int i=0; i<G[v].size(); i++) { edge &e=G[v][i]; if(e.cap>0&&level[e.to]<0) { level[e.to]=level[v]+1; que.push(e.to); } } }}//通过dfs寻找增广路int dfs(int v,int t,int f){ if(v==t) return f; for(int &i=iter[v]; i<G[v].size(); i++) { edge &e=G[v][i]; if(e.cap>0&&level[v]<level[e.to]) { int d=dfs(e.to,t,min(f,e.cap)); if(d>0) { e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0;}//求解从s到t的最大流int max_flow(int s,int t){ int flow=0; for(;;) { bfs(s); if(level[t]<0) return flow; memset(iter,0,sizeof(iter)); int f; while((f=dfs(s,t,INF))>0) { flow+=f; //cout<<f<<" "; } }}int main(){ int n,f,d,s,t; while(~scanf("%d%d%d",&n,&f,&d)) { s=0,t=d+f+2*n+1; for(int i=1; i<=t; i++) G[i].clear(); for(int i=1; i<=f; i++) addedge(s,i,1);//源点跟食物之间 int fi,di,x; for(int i=1; i<=n; i++) { addedge(f+i,f+i+n,1);//一头牛之间分两个点建立容量为1的边 scanf("%d%d",&fi,&di); for(int j=1; j<=fi; j++) { scanf("%d",&x); addedge(x,f+i,1);//食物跟牛之间 } for(int j=1; j<=di; j++) { scanf("%d",&x); addedge(f+i+n,f+2*n+x,1);//饮料跟牛之间 } } for(int i=1; i<=d; i++) addedge(f+2*n+i,t,1);//牛与汇点之间 int ans=max_flow(s,t); printf("%d\n",ans); } return 0;}题目描述:
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 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 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
- 【最大流模板题】POJ 3281 Dining
- POJ 3281 B - Dining 最大流+模板
- poj 3281 Dining 最大流dinic 模板题
- poj 3281 Dining 【最大流】
- poj 3281 Dining(最大流)
- POJ 3281 Dining(最大流)
- poj 3281 Dining 最大流
- poj 3281 Dining (最大流)
- poj 3281 Dining 【最大流】
- poj 3281 Dining (最大流)
- poj 3281 Dining 最大流
- POJ 3281 Dining 最大流
- poj 3281 Dining(最大流)
- poj 3281 Dining 最大流
- POJ-3281 Dining(最大流)
- POJ 3281 Dining(最大流)
- POJ-3281 Dining(最大流)
- poj 3281 Dining(最大流)
- oracle 时间类型 与java mybatis.xml 文件配置 resultMap 对应时间类型 JsonConfig 转换oracle里的时间类型
- JSONUtil工具类
- What is saturating activation function?
- fabric源码解析2——peer命令结构
- 重建二叉树
- 【最大流模板题】POJ 3281 Dining
- 面向对象程序设计六大原则-迪米特法则
- MarkDown基本使用
- swift 懒加载
- 竞赛题目讲解-【NOIP2000复赛 普及组】单词接龙
- 从零开始学习音视频编程技术(33) Linux篇之Makefile
- bzoj 1293: [SCOI2009]生日礼物
- 在Leaflet地图上绘制多边形,避免缩放时dynamicMapLayer遮住多边形
- 原来iMindMap还有这样一个视图