POJ 3281.Dining 最大流
Posted GeekZRF
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
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 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
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
#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 最大流的主要内容,如果未能解决你的问题,请参考以下文章