标签:
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
#include<stdio.h> #include<string.h> #include<iostream> #include<queue> #include<algorithm> using namespace std; int edge[405][405];//邻接矩阵 int dis[405];//距源点距离,分层图 int start,end; int m,n;//N:点数;M,边数 int bfs(){ memset(dis,-1,sizeof(dis));//以-1填充 dis[0]=0; queue<int>q; q.push(start); while(!q.empty()){ int u=q.front(); q.pop(); for(int i=0;i<=n;i++){ if(dis[i]<0&&edge[u][i]){ dis[i]=dis[u]+1; q.push(i); } } } if(dis[n]>0) return 1; else return 0;//汇点的DIS小于零,表明BFS不到汇点 } //Find代表一次增广,函数返回本次增广的流量,返回0表示无法增广 int find(int x,int low){//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量 int a=0; if(x==n) return low;//是汇点 for(int i=0;i<=n;i++){ if(edge[x][i]>0&&dis[i]==dis[x]+1&&//联通,,是分层图的下一层 (a=find(i,min(low,edge[x][i])))){//能到汇点(a <> 0) edge[x][i]-=a; edge[i][x]+=a; return a; } } return 0; } int main(){ int a,b,c; while(scanf("%d%d%d",&a,&b,&c)!=EOF){ n=a+a+b+c+1; memset(edge,0,sizeof(edge)); for(int i=1;i<=b;i++) edge[0][i]=1; for(int i=a+a+b+1;i<=a+a+b+c;i++) edge[i][n]=1; int u; int sum1,sum2; for(int i=1;i<=a;i++){ // int u,v,w; scanf("%d%d",&sum1,&sum2); for(int j=1;j<=sum1;j++){ scanf("%d",&u); edge[u][i+b]=1; } for(int j=1;j<=sum2;j++){ scanf("%d",&u); edge[b+a+i][a+a+b+u]=1; } } for(int i=1;i<=a;i++){ edge[i+b][i+b+a]=1; } start=0; end=n; int ans=0; while(bfs()){//要不停地建立分层图,如果BFS不到汇点才结束 ans+=find(0,0x7fffffff);//一次BFS要不停地找增广路,直到找不到为止 } printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/13224ACMer/p/5340458.html