标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11844 | Accepted: 5444 |
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<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 #define mem(x,y) memset(x,y,sizeof(x)) 8 using namespace std; 9 const int INF=0x3f3f3f3f; 10 const int MAXN=410; 11 const int MAXM=50010<<1; 12 int head[MAXM]; 13 int vis[MAXN],dis[MAXN]; 14 int edgnum; 15 struct Node{ 16 int from,to,next,cup,flow; 17 }; 18 Node edg[MAXM]; 19 queue<int>dl; 20 void add(int u,int v,int w){ 21 Node E={u,v,head[u],w,0}; 22 edg[edgnum]=E; 23 head[u]=edgnum++; 24 E={v,u,head[v],0,0}; 25 edg[edgnum]=E; 26 head[v]=edgnum++; 27 } 28 void initial(){ 29 mem(head,-1);edgnum=0; 30 } 31 bool bfs(int s,int e){ 32 mem(vis,0);mem(dis,-1); 33 while(!dl.empty())dl.pop(); 34 dis[s]=0;vis[s]=1;dl.push(s); 35 while(!dl.empty()){ 36 int u=dl.front(); 37 dl.pop(); 38 for(int i=head[u];i!=-1;i=edg[i].next){ 39 Node v=edg[i]; 40 if(!vis[v.to]&&v.cup>v.flow){ 41 vis[v.to]=1;dl.push(v.to); 42 dis[v.to]=dis[u]+1; 43 if(v.to==e)return true; 44 } 45 } 46 } 47 return false; 48 } 49 int dfs(int x,int la,int e){ 50 if(x==e||la==0)return la; 51 int temp; 52 int flow=0; 53 for(int i=head[x];i!=-1;i=edg[i].next){ 54 Node &v=edg[i]; 55 if(dis[v.to]==dis[x]+1&&(temp=dfs(v.to,min(la,v.cup-v.flow),e))>0){ 56 v.flow+=temp; 57 edg[i^1].flow-=temp; 58 la-=temp; 59 flow+=temp; 60 if(la==0)break; 61 } 62 } 63 return flow; 64 } 65 int maxflow(int s,int e){ 66 int flow=0; 67 while(bfs(s,e)){ 68 flow+=dfs(s,INF,e); 69 } 70 return flow; 71 } 72 int main(){ 73 int n,F,D,f,d,a,b; 74 while(~scanf("%d%d%d",&n,&F,&D)){ 75 initial(); 76 for(int i=1;i<=n;i++){ 77 scanf("%d%d",&f,&d); 78 while(f--){ 79 scanf("%d",&a); 80 add(2*n+a,i,1); 81 } 82 while(d--){ 83 scanf("%d",&a); 84 add(n+i,2*n+F+a,1); 85 } 86 add(i,n+i,1); 87 } 88 for(int i=1;i<=F;i++)add(0,2*n+i,1); 89 for(int i=1;i<=D;i++)add(2*n+F+i,2*n+F+D+1,1); 90 printf("%d\n",maxflow(0,2*n+F+D+1));//醉了,应该从0开始,找了半天错。。。 91 } 92 return 0; 93 }
另一种解法超时:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 #define mem(x,y) memset(x,y,sizeof(x)) 8 using namespace std; 9 const int INF=0x3f3f3f3f; 10 const int MAXN=500; 11 const int MAXM=50010<<1; 12 queue<int>dl; 13 int vis[MAXN],map[MAXN][MAXN],pre[MAXN]; 14 bool bfs(int s,int e){ 15 mem(vis,0);mem(pre,-1); 16 vis[s]=1;dl.push(s); 17 int a; 18 while(!dl.empty()){ 19 a=dl.front(); 20 dl.pop();//忘写pop了。。。 21 if(a==e)return true; 22 for(int i=0;i<=e;i++){ 23 if(!vis[i]&&map[a][i]){ 24 vis[i]=1; 25 pre[i]=a; 26 dl.push(i); 27 //if(i==e)return true; 28 } 29 } 30 } 31 return false; 32 } 33 int maxflow(int s,int e){ 34 int flow=0; 35 while(bfs(s,e)){ 36 int temp=INF; 37 int r=e; 38 //puts("fasf"); 39 while(r!=s)temp=min(temp,map[pre[r]][r]),r=pre[r]; 40 r=e; 41 while(r!=s)map[pre[r]][r]-=temp,map[r][pre[r]]+=temp,r=pre[r]; 42 flow+=temp; 43 } 44 return flow; 45 } 46 int main(){ 47 int n,F,D,f,d,a,b; 48 while(~scanf("%d%d%d",&n,&F,&D)){ 49 mem(map,0); 50 for(int i=1;i<=n;i++){ 51 scanf("%d%d",&f,&d); 52 while(f--){ 53 scanf("%d",&a); 54 // add(2*n+a,i,1); 55 map[2*n+a][i]=1; 56 } 57 while(d--){ 58 scanf("%d",&a); 59 // add(n+i,2*n+F+a,1); 60 map[n+i][2*n+F+a]=1; 61 } 62 //add(i,n+i,1); 63 map[i][n+i]=1; 64 } 65 for(int i=1;i<=F;i++)map[0][2*n+i]=1;//add(0,2*n+i,1); 66 for(int i=1;i<=D;i++)map[2*n+F+i][2*n+F+D+1]=1;//add(2*n+F+i,2*n+F+D+1,1); 67 printf("%d\n",maxflow(0,2*n+F+D+1));//醉了,应该从0开始,找了半天错。。。 68 } 69 return 0; 70 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4937268.html