POJ-3281 Dining(最大流)
来源:互联网 发布:adidas跑鞋系列 知乎 编辑:程序博客网 时间:2024/06/08 02:51
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.
题解:最大流拆点,把牛的点拆开,用一条容量为1的边连起来,对于每头牛,把每个喜欢吃的食物与牛连起来,在把喜欢喝的饮料与牛连起来,最后把所有的食物与源点连起来,把所有饮料与汇点连起来,跑一遍最大流,最大流就是最多能满足的牛的数目
#include<cstdio>#include<algorithm>#include<string.h>#include<queue>using namespace std;const int maxn = 405;const int maxe = 4*maxn*maxn;const int inf = 0x3f3f3f3f;struct MaxFlow { struct Edge { int v, w, nxt; } edge[maxe]; int head[maxn], tot, level[maxn]; void init(){ memset(head,-1,sizeof(head)); tot=0; } void add(int u, int v, int w) { edge[tot].v = v; edge[tot].w = w; edge[tot].nxt = head[u]; head[u] = tot++; edge[tot].v = u; edge[tot].w = 0; edge[tot].nxt = head[v]; head[v] = tot++; } bool bfs(int s, int t) { memset(level, -1, sizeof(level)); queue<int>q; q.push(s); level[s] = 0; while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; ~i; i = edge[i].nxt) { if(edge[i].w > 0 && level[edge[i].v] < 0) { level[edge[i].v] = level[u] + 1; q.push(edge[i].v); } } } return level[t] > 0; } int dfs(int u, int t, int f) { if(u == t) return f; for(int i = head[u]; ~i; i = edge[i].nxt) { int v = edge[i].v; if(edge[i].w > 0 && level[v] > level[u]) { int d = dfs(v, t, min(f, edge[i].w)); if(d > 0) { edge[i].w -= d; edge[i ^ 1].w += d; return d; } } } level[u] = -1; //不太确定,如果WA了把这句删掉试试 return 0; } int solve(int s, int t) { int flow = 0, f; while(bfs(s, t)) { while(f = dfs(s, t, inf)) flow += f; } return flow; }}F;int main(){ int n,f,d; // freopen("in.txt","r",stdin); while(~scanf("%d%d%d",&n,&f,&d)){ F.init(); for(int i=1;i<=n;i++){ int t1,t2,v; scanf("%d%d",&t1,&t2); for(int j=1;j<=t1;j++){ scanf("%d",&v); F.add(v,i+f,1); } for(int j=1;j<=t2;j++){ scanf("%d",&v); F.add(i+n+f,v+2*n+f,1); } } for(int i=1;i<=n;i++) F.add(i+f,i+f+n,1); for(int i=1;i<=f;i++) F.add(0,i,1); for(int i=1;i<=d;i++) F.add(i+2*n+f,2*n+f+d+1,1); printf("%d\n",F.solve(0,2*n+f+d+1)); } return 0;}
- 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(最大流)
- POJ 3281 Dining 最大流
- POJ-3281 Dining(最大流)
- POJ 3281 Dining (最大流)
- 慕课网-十天精通CSS3-CSS3中的变形与动画(上)
- 守护进程
- ext4 学习笔记(三) Ext.window.Window(白鹤翔第一季)
- 【Lightoj 1414 - February 29 】
- UVA - 10870 Recurrences (矩阵快速幂)
- POJ-3281 Dining(最大流)
- dnotify监控文件行为
- HDU-1598-find the most comfortable road
- mac关于.bash_profile环境变量配置的问题
- 简单的停车场程序
- 关于进程虚拟地址为什么是4G的讨论
- 作用域(基础篇)
- uboot源码分析
- 关于杨辉三角的解决方案