poj3281 Dining 题解
来源:互联网 发布:mac终端进入指定目录 编辑:程序博客网 时间:2024/05/22 10:10
传送门
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).
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.
4 3 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 1 1 3 3
3
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.
这道题就是一个最大流的变体,将牛分成两边,一边和食物匹配,一边和饮料匹配。然后相同的牛之间连一条边,再增加一个超级原点s和超级汇点t即可。
如上图所示建图,然后套用网络流模板即可。
#include<stdio.h>#include<iostream>#include<set>#include<queue>#include<algorithm>#include<math.h>#include<string.h>#include<string>#include<vector>using namespace std;const int inf = 0x3f3f3f3f ;const int maxn = 505 ;struct edge{ int to , cap , rev ;};vector<edge> G[maxn];bool used[maxn];void add_edge(int from , int to , int cap){ edge e1 , e2 ; e1.to = to; e1.cap = cap ; e1.rev = G[to].size(); G[from].push_back(e1); e2.to = from; e2.cap = 0 ; e2.rev = G[from].size() - 1; G[to].push_back(e2);}int dfs(int v , int t , int f){ if(v == t) return f ; used[v] = true; for(int i = 0 ; i <G[v].size() ; i ++){ edge &e = G[v][i] ; if(!used[e.to] && e.cap > 0){ 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;}int max_flow(int s , int t){ int flow = 0 ; for( ; ; ){ memset(used , false , sizeof(used)); int f = dfs(s , t , inf); if(f == 0 ) return flow ; flow += f ; }}//最大流模板结束int n , f , d ;int main(){ while(~scanf("%d%d%d",&n,&f,&d)){ int s = 0 , t = 2*n + f + d + 1; int i1 ,i2 ; for(int i = 0 ; i <= t ; i ++) G[i].clear();//一系列的建图 for(int i = 1 ; i <= f ; i ++) add_edge(0 , i , 1) ; for(int i = f+2*n+1 ; i <= f+2*n+d ; i ++) add_edge(i , t , 1) ; for(int i = f+1 ; i <= f+n ; i ++){ add_edge(i , i + n , 1) ; } for(int i = 1 ; i <= n ; i ++){ int x; int fi , di ; scanf("%d %d",&fi , &di); for(int i1 = 0 ; i1 < fi ; i1++){ scanf("%d" , &x) ; add_edge(x , i + f , 1 ); } for(int i2 = 0 ; i2 < di ; i2 ++){ scanf("%d" , &x); add_edge(f+n+i , f+2*n+x , 1); } } printf("%d\n" ,max_flow(s , t)); } return 0;}
- 【题解】poj3281 dining
- poj3281 Dining 题解
- poj3281 - Dining
- POJ3281--Dining
- poj3281 Dining
- poj3281 Dining
- POJ3281-Dining
- POJ3281 Dining
- POJ3281 Dining
- poj3281 Dining
- POJ3281:Dining
- 【POJ3281 Dining 】最大流
- POJ3281 Dining (SAP+GAP)
- 【poj3281】【最大流】Dining
- POJ3281 网络流 dining
- poj3281 Dining Dinic
- 【POJ3281】Dining 网络流
- POJ3281 Dining(最大流)
- 保存bitmap 图片路径
- C++之关于static关键字的充电---补充(7)《Effective C++》
- 机器学习和数据挖掘(6):雷蒙保罗MAPA泛化理论
- 数学模型的适用范围
- A_S_M
- poj3281 Dining 题解
- 2017年7月22日20:45:16
- MapReduce编程基础(一)——基础知识
- 按图形输出
- 最大子矩阵(悬挂线)
- 相对定位
- 截屏有截取当前屏幕
- 怎么在地图上标注自己的店铺
- [Leetcode] Week One ------ Array