Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20928 | Accepted: 9279 |
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 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 1 1 3 3
Sample Output
3
Hint
#include#include #include #include #define INF 999999999using namespace std;int N,F,D;//N只cows,F种food,D种drinks int map[500][500],dis[500];int T;int bfs(){ memset(dis,-1,sizeof(dis)); dis[0]=0; queue q; q.push(0); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0;i<=T;i++) if(dis[i]==-1&&map[u][i]>0) { dis[i]=dis[u]+1; q.push(i); } } if(dis[T]>0) return 1; return 0;}int dfs(int cur,int m){ if(cur==T) return m; int f,res=0; for(int i=0;i<=T;i++) { if(dis[i]==dis[cur]+1&&map[cur][i]>0&&(f=dfs(i,min(m,map[cur][i])))) { map[cur][i]-=f; map[i][cur]+=f; res+=f; m-=f; if(!m) break; } } if(res) return res; dis[cur]=-1; return 0;}int main(){ scanf("%d%d%d",&N,&F,&D); //0为源点,1到F为food结点,F+1到F+2N为cow结点 //F+2N+1到F+2N+D为drinks结点,T=F+2N+D+1为汇点 //一头牛拆成两个点,两个点的边容量为1 T=F+N+N+D+1; for(int i=1;i<=N;i++) { int f,d,x; scanf("%d%d",&f,&d); while(f--) { scanf("%d",&x); map[x][F+i]=1;//food->cow } while(d--) { scanf("%d",&x); map[F+N+i][F+N+N+x]=1;//cow->drink } } for(int i=1;i<=F;i++) map[0][i]=1;//源点->food for(int i=1;i<=D;i++) map[F+N+N+i][T]=1;//drink->汇点 for(int i=1;i<=N;i++) map[F+i][F+i+N]=1;//cow->cow int ans=0,res; while(bfs()) { while(res=dfs(0,INF)) ans+=res; } printf("%d\n",ans); return 0;}