POJ 3281.Dining 最大流
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18479 | Accepted: 8243 |
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
Line 1: Three space-separated integers: N, F, and DLines 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
Output
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishesSample Input
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
Sample Output
3
Hint
One way to satisfy three cows is: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
USACO 2007 Open Gold 題目連接:http://poj.org/problem?id=3281 題意:有n頭牛,f個食物,d個飲料。每頭牛喜歡都有各自喜歡的食物和飲料,但每個食物,飲料只能分給一頭牛。有多少牛可以同時得到喜歡的食物和飲料。 思路:最大流。將牛拆分。源點——食物——牛——牛——飲料——匯點。 代碼:#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> #include<set> using namespace std; #define PI acos(-1.0) typedef long long ll; typedef pair<int,int> P; const int maxn=1e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7; const ll INF=1e13+7; priority_queue<P,vector<P>,greater<P> >q; struct edge { int from,to; int cap; int rev;///方向邊的編號 }; int n,f,d; vector<edge>G[maxn]; int used[maxn]; void addedge(int u,int v,int c) { edge e; e.from=u,e.to=v,e.cap=c,e.rev=G[v].size(); G[u].push_back(e); e.from=v,e.to=u,e.cap=0,e.rev=G[u].size()-1; G[v].push_back(e); } int dfs(int u,int t,int f) { if(u==t) return f; used[u]=true; for(int i=0; i<G[u].size(); i++) { edge e=G[u][i]; int v=e.to,c=e.cap; if(!used[v]&&c>0) { int d=dfs(v,t,min(f,c)); if(d>0) { G[u][i].cap-=d; G[v][e.rev].cap+=d; return d; } } } return 0; } int max_flow(int s,int t) { int flow=0; while(true) { memset(used,0,sizeof(used)); int f=dfs(s,t,inf); if(f==0) return flow; flow+=f; } } int main() { scanf("%d%d%d",&n,&f,&d); for(int i=0; i<=2*n+f+d+1; i++) G[i].clear(); for(int i=1; i<=n; i++) { int x,fi,di; scanf("%d%d",&fi,&di); for(int j=1; j<=fi; j++) { scanf("%d",&x); addedge(2*n+x,i,1); } for(int j=1; j<=di; j++) { scanf("%d",&x); addedge(n+i,2*n+f+x,1); } } for(int i=1; i<=n; i++) addedge(i,n+i,1); for(int i=1; i<=f; i++) addedge(0,2*n+i,1); for(int i=1; i<=d; i++) addedge(2*n+f+i,2*n+f+d+1,1); cout<<max_flow(0,2*n+f+d+1)<<endl; return 0; }最大流
POJ 3281.Dining 最大流