TOJ 1705 Dining(拆点最大流)
Posted taozi1115402474
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TOJ 1705 Dining(拆点最大流)相关的知识,希望对你有一定的参考价值。
描述
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).
输入
Line 1: Three space-separated integers: N, F, and D
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.
输出
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
样例输入
4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 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.
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int N=1e5+5; 5 const int M=2e5+5; 6 int n,m,S,T; 7 int deep[N],q[400000]; 8 int FIR[N],TO[M],CAP[M],COST[M],NEXT[M],tote; 9 10 void add(int u,int v,int cap) 11 { 12 TO[tote]=v; 13 CAP[tote]=cap; 14 NEXT[tote]=FIR[u]; 15 FIR[u]=tote++; 16 17 TO[tote]=u; 18 CAP[tote]=0; 19 NEXT[tote]=FIR[v]; 20 FIR[v]=tote++; 21 } 22 bool bfs() 23 { 24 memset(deep,0,sizeof deep); 25 deep[S]=1;q[1]=S; 26 int head=0,tail=1; 27 while(head!=tail) 28 { 29 int u=q[++head]; 30 for(int v=FIR[u];v!=-1;v=NEXT[v]) 31 { 32 if(CAP[v]&&!deep[TO[v]]) 33 { 34 deep[TO[v]]=deep[u]+1; 35 q[++tail]=TO[v]; 36 } 37 } 38 } 39 return deep[T]; 40 } 41 int dfs(int u,int fl) 42 { 43 if(u==T)return fl; 44 int f=0; 45 for(int v=FIR[u];v!=-1&&fl;v=NEXT[v]) 46 { 47 if(CAP[v]&&deep[TO[v]]==deep[u]+1) 48 { 49 int Min=dfs(TO[v],min(fl,CAP[v])); 50 CAP[v]-=Min;CAP[v^1]+=Min; 51 fl-=Min;f+=Min; 52 } 53 } 54 if(!f)deep[u]=-2; 55 return f; 56 } 57 int maxflow() 58 { 59 int ans=0; 60 while(bfs()) 61 ans+=dfs(S,1<<30); 62 return ans; 63 } 64 void init() 65 { 66 tote=0; 67 memset(FIR,-1,sizeof FIR); 68 } 69 int main() 70 { 71 int v,cow,F,D,food,drink; 72 init(); 73 cin>>cow>>F>>D; 74 S=F+2*cow+D+1,T=S+1; 75 for(int i=1;i<=F;i++) 76 add(S,i,1); 77 for(int i=F+2*cow+1;i<=F+2*cow+D;i++) 78 add(i,T,1); 79 for(int i=F+1;i<=F+cow;i++) 80 { 81 add(i,cow+i,1); 82 cin>>food>>drink; 83 while(food--)cin>>v,add(v,i,1); 84 while(drink--)cin>>v,add(cow+i,F+2*cow+v,1); 85 } 86 cout<<maxflow(); 87 return 0; 88 }
以上是关于TOJ 1705 Dining(拆点最大流)的主要内容,如果未能解决你的问题,请参考以下文章