标签:span follow input algorithm imu signed 分享 创建 hint
题目链接:http://poj.org/problem?id=3281
题目:
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
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
1 #include <queue> 2 #include <cstring> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N=555; 9 const int M=250010; 10 const int INF=0x3f3f3f3f; 11 int n,s,t,cnt; 12 int Head[N],Depth[N],Next[M],V[M],W[M]; 13 14 void init(){ 15 cnt=-1; 16 memset(Head,-1,sizeof(Head)); 17 memset(Next,-1,sizeof(Next)); 18 } 19 20 void add_edge(int u,int v,int w){ 21 cnt++; 22 Next[cnt]=Head[u]; 23 V[cnt]=v; 24 W[cnt]=w; 25 Head[u]=cnt; 26 cnt++; 27 Next[cnt]=Head[v]; 28 V[cnt]=u; 29 W[cnt]=0; 30 Head[v]=cnt; 31 } 32 33 bool bfs(){ 34 queue <int> Q; 35 while(!Q.empty()) Q.pop(); 36 memset(Depth,0,sizeof(Depth)); 37 Depth[s]=1; 38 Q.push(s); 39 while(!Q.empty()){ 40 int u=Q.front();Q.pop(); 41 for(int i=Head[u];i!=-1;i=Next[i]){ 42 if((W[i]>0)&&(Depth[V[i]]==0)){ 43 Depth[V[i]]=Depth[u]+1; 44 Q.push(V[i]); 45 } 46 } 47 } 48 if(Depth[t]==0) return 0; 49 return 1; 50 } 51 52 int dfs(int u,int dist){ 53 if(u==t) return dist; 54 for(int i=Head[u];i!=-1;i=Next[i]){ 55 if((Depth[V[i]]==Depth[u]+1)&&W[i]!=0){ 56 int di=dfs(V[i],min(dist,W[i])); 57 if(di>0){ 58 W[i]-=di; 59 W[i^1]+=di; 60 return di; 61 } 62 } 63 } 64 return 0; 65 } 66 67 int Dinic(){ 68 int ans=0; 69 while(bfs()){ 70 while(int d=dfs(s,INF)) 71 ans+=d; 72 } 73 return ans; 74 } 75 76 int main(){ 77 init(); 78 int F,D,a,b,tmp; 79 scanf("%d %d %d",&n,&F,&D); 80 s=0;t=2*n+F+D+1; 81 for(int i=1;i<=n;i++) add_edge(i,i+n,1); 82 for(int i=1;i<=n;i++){ 83 scanf("%d %d",&a,&b); 84 for(int j=1;j<=a;j++){ 85 scanf("%d",&tmp); 86 add_edge(2*n+tmp,i,1); 87 } 88 for(int j=1;j<=b;j++){ 89 scanf("%d",&tmp); 90 add_edge(i+n,2*n+F+tmp,1); 91 } 92 } 93 for(int i=1;i<=F;i++) add_edge(s,2*n+i,1); 94 for(int i=1;i<=D;i++) add_edge(2*n+F+i,t,1); 95 96 printf("%d",Dinic()); 97 return 0; 98 }
标签:span follow input algorithm imu signed 分享 创建 hint
原文地址:http://www.cnblogs.com/Leonard-/p/7868171.html