POJ 3281 Dining(sap模板大法好)
来源:互联网 发布:数据流程图符号含义 编辑:程序博客网 时间:2024/05/01 05:35
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 theDi 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 牛1-----牛1 水1
源点s 食物2 牛2-----牛2 水2 终点
食物3 牛3-----牛3 水3
食物4 牛4-----牛4 水4
牛和牛之间有一条路为1,控制每只牛最多拥有一种食物。
AC代码:
#include <stdio.h>#include <string.h>#define maxn 500#define maxx 30000#define inf 0x3fffffff#define MIN(a,b) a>b?b:a#define mems(p) memset(p,0,sizeof(p))struct E{ int to,val,next;} e[maxx];int nn[maxn],ff[maxn],dd[maxn],nnn[maxn];int vs,S,T,cnt;int dis[maxn],gap[maxn],head[maxn];void add(int from,int to,int val){ e[cnt].to=to; e[cnt].val=val; e[cnt].next=head[from]; head[from]=cnt++; e[cnt].to=from; e[cnt].val=0; e[cnt].next=head[to]; head[to]=cnt++;}int dfs(int s,int aug){ if(s==T) return aug; int flow=0,mi=vs-1; for(int i=head[s]; i; i=e[i].next) if(e[i].val) { if(dis[s]==dis[e[i].to]+1) { int t=dfs(e[i].to,MIN(aug-flow,e[i].val)); e[i].val-=t; e[i^1].val+=t; flow+=t; if(dis[S]>=vs)return flow; if(aug==flow)break; } mi=MIN(mi,dis[e[i].to]); } if(!flow) { if(!(--gap[dis[s]])) gap[S]=vs; ++gap[dis[s]=mi+1]; } return flow;}int slove(int s,int ed){ mems(gap); mems(dis); gap[0]=vs=ed; S=s; T=ed; int ans=0; while(dis[S]<vs) ans+=dfs(S,inf); return ans;}int main(){ int n,d,f,a,b,c; while(~scanf("%d%d%d",&n,&f,&d)) { mems(head); cnt=2;//因为head数组我清零了,所以不能出现编号为零的路 int num=2; for(int i=1; i<=f; i++) ff[i]=num++;//对所有的节点进行编号 for(int i=1; i<=n; i++) nn[i]=num++; for(int i=1; i<=n; i++) nnn[i]=num++; for(int i=1; i<=d; i++) dd[i]=num++; for(int i=1;i<=f;i++) add(1,ff[i],1);//建边 for(int i=1;i<=n;i++) add(nn[i],nnn[i],1); for(int i=1;i<=d;i++) add(dd[i],num,1); for(int i=1;i<=n;i++) { scanf("%d%d",&a,&b); for(int j=1;j<=a;j++) { scanf("%d",&c); add(ff[c],nn[i],1); } for(int j=1;j<=b;j++) { scanf("%d",&c); add(nnn[i],dd[c],1); } } printf("%d\n",slove(1,num)); }}
- POJ 3281 Dining(sap模板大法好)
- poj 3281 Dining (sap模板)
- poj 3281 Dining //SAP
- POJ 3281 Dining(最大流dinic&&SAP)
- POJ 3281 B - Dining 最大流+模板
- 【最大流模板题】POJ 3281 Dining
- poj Dining 3281 (最大流问题&&机智建图) 好题
- 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 (ISAP EK Dinic)
- POJ 3281 Dining(最大流-Dinic)
- POJ 3281 Dining (网络流)
- LeetCode
- POJ1511 Invitation Cards(SPFA)
- Android关于沉浸式状态栏总结
- Python高级教程之作用域
- js系列教程3-字符串、正则表达式全解
- POJ 3281 Dining(sap模板大法好)
- SQL CREATE TABLE 语句
- 关于ESP8266WIFI转串口的速率问题(含测试数据)
- HDU6092---Rikka with Subset(2017多校联赛:逆向dp)
- jQuery基础
- 神经网络和深度学习(二)——BP(Backpropagation Algorithm, 反向传播算法)
- 无刷直流电机
- http://blog.csdn.net/bolu1234/article/details/51867099
- 极大连通子图 + 极小连通子图 + 连通分量