Dining POJ - 3281
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.
1 #include<vector> 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 8 const int maxn=1005; 9 const int INF=0x3f3f3f3f; 10 11 struct edge{ 12 int to,w; 13 int nxt,rev; 14 }e[maxn*10]; 15 16 int N,F,D,tot; 17 int head[maxn],vis[maxn]; 18 19 void Init(){ 20 tot=0; 21 memset(head,-1,sizeof(head)); 22 } 23 24 void addedge(int u,int v,int w){ 25 e[tot].to=v,e[tot].w=w,e[tot].nxt=head[u],e[tot].rev=tot+1,head[u]=tot++; 26 e[tot].to=u,e[tot].w=0,e[tot].nxt=head[v],e[tot].rev=tot-1,head[v]=tot++; 27 } 28 29 int DFS(int s,int t,int f){ 30 if(s==t) return f; 31 vis[s]=true; 32 for(int i=head[s];i!=-1;i=e[i].nxt){ 33 int v=e[i].to; 34 if(!vis[v]&&e[i].w>0){ 35 int d=DFS(v,t,min(e[i].w,f)); //改成 min(f,e[i].w)会多30多毫秒,???? 36 if(d<=0) continue; 37 e[i].w-=d; 38 e[e[i].rev].w+=d; 39 return d; 40 } 41 } 42 return 0; 43 } 44 45 int max_Flow(int s,int t){ 46 int ans=0; 47 while(true){ 48 memset(vis,false,sizeof(vis)); 49 int temp=DFS(s,t,INF); 50 if(temp==0) return ans; 51 ans=ans+temp; 52 } 53 } 54 55 void solve(){ 56 int s=0,t=F+2*N+D+1; 57 for(int i=1;i<=F;i++) addedge(s,i,1); 58 for(int i=1;i<=D;i++) addedge(F+2*N+i,t,1); 59 for(int i=1;i<=N;i++) addedge(F+i,F+i+N,1); 60 for(int i=1;i<=N;i++) { 61 int p,q,x; 62 cin>>p>>q; 63 for(int j=1;j<=p;j++){ cin>>x; addedge(x,F+i,1); } 64 for(int j=1;j<=q;j++){ cin>>x; addedge(F+N+i,F+2*N+x,1);} 65 } 66 printf("%d\n",max_Flow(s,t)); 67 } 68 69 int main() 70 { while(cin>>N>>F>>D){ Init(); solve(); } 71 return 0; 72 } 73 74 /*这种写法会超时,让我很无语 75 void Read(){ 76 cin>>N>>F>>D; 77 int p,q,x; 78 for(int i=0;i<N;i++){ 79 cin>>p>>q; 80 for(int j=0;j<p;j++){ cin>>x; likeF[i][x-1]=true; } 81 for(int j=0;j<q;j++){ cin>>x; likeD[i][x-1]=true; } 82 } 83 } 84 85 void solve(){ 86 int s=N*2+F+D; 87 int t=s+1; 88 for(int i=0;i<F;i++) addedge(s,N*2+i,1); 89 for(int i=0;i<D;i++) addedge(N*2+F+i,t,1); 90 for(int i=0;i<N;i++){ 91 addedge(i,N+i,1); 92 for(int j=0;j<F;j++) if(likeF[i][j]) addedge(N*2+j,i,1); 93 for(int j=0;j<D;j++) if(likeD[i][j]) addedge(N+i,N*2+F+j,1); 94 } 95 printf("%d\n",max_Flow(s,t)); 96 } 97 */

浙公网安备 33010602011771号